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"
64 static char **arg_property
= NULL
;
65 static bool arg_all
= false;
66 static bool arg_value
= false;
67 static bool arg_full
= false;
68 static bool arg_no_pager
= false;
69 static bool arg_legend
= true;
70 static const char *arg_kill_who
= NULL
;
71 static int arg_signal
= SIGTERM
;
72 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
73 static char *arg_host
= NULL
;
74 static bool arg_read_only
= false;
75 static bool arg_mkdir
= false;
76 static bool arg_quiet
= false;
77 static bool arg_ask_password
= true;
78 static unsigned arg_lines
= 10;
79 static OutputMode arg_output
= OUTPUT_SHORT
;
80 static bool arg_force
= false;
81 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
82 static const char* arg_format
= NULL
;
83 static const char *arg_uid
= NULL
;
84 static char **arg_setenv
= NULL
;
86 static void polkit_agent_open_if_enabled(void) {
88 /* Open the polkit agent as a child process if necessary */
90 if (!arg_ask_password
)
93 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
99 static OutputFlags
get_output_flags(void) {
101 arg_all
* OUTPUT_SHOW_ALL
|
102 arg_full
* OUTPUT_FULL_WIDTH
|
103 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH
|
104 colors_enabled() * OUTPUT_COLOR
|
105 !arg_quiet
* OUTPUT_WARN_CUTOFF
;
108 typedef struct MachineInfo
{
114 static int compare_machine_info(const void *a
, const void *b
) {
115 const MachineInfo
*x
= a
, *y
= b
;
117 return strcmp(x
->name
, y
->name
);
120 static int list_machines(int argc
, char *argv
[], void *userdata
) {
122 size_t max_name
= strlen("MACHINE"), max_class
= strlen("CLASS"), max_service
= strlen("SERVICE");
123 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
124 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
125 _cleanup_free_ MachineInfo
*machines
= NULL
;
126 const char *name
, *class, *service
, *object
;
127 size_t n_machines
= 0, n_allocated
= 0, j
;
128 sd_bus
*bus
= userdata
;
133 pager_open(arg_no_pager
, false);
135 r
= sd_bus_call_method(bus
,
136 "org.freedesktop.machine1",
137 "/org/freedesktop/machine1",
138 "org.freedesktop.machine1.Manager",
144 log_error("Could not get machines: %s", bus_error_message(&error
, -r
));
148 r
= sd_bus_message_enter_container(reply
, 'a', "(ssso)");
150 return bus_log_parse_error(r
);
152 while ((r
= sd_bus_message_read(reply
, "(ssso)", &name
, &class, &service
, &object
)) > 0) {
155 if (name
[0] == '.' && !arg_all
)
158 if (!GREEDY_REALLOC(machines
, n_allocated
, n_machines
+ 1))
161 machines
[n_machines
].name
= name
;
162 machines
[n_machines
].class = class;
163 machines
[n_machines
].service
= service
;
180 return bus_log_parse_error(r
);
182 r
= sd_bus_message_exit_container(reply
);
184 return bus_log_parse_error(r
);
186 qsort_safe(machines
, n_machines
, sizeof(MachineInfo
), compare_machine_info
);
189 printf("%-*s %-*s %-*s\n",
190 (int) max_name
, "MACHINE",
191 (int) max_class
, "CLASS",
192 (int) max_service
, "SERVICE");
194 for (j
= 0; j
< n_machines
; j
++)
195 printf("%-*s %-*s %-*s\n",
196 (int) max_name
, machines
[j
].name
,
197 (int) max_class
, machines
[j
].class,
198 (int) max_service
, machines
[j
].service
);
201 printf("\n%zu machines listed.\n", n_machines
);
206 typedef struct ImageInfo
{
215 static int compare_image_info(const void *a
, const void *b
) {
216 const ImageInfo
*x
= a
, *y
= b
;
218 return strcmp(x
->name
, y
->name
);
221 static int list_images(int argc
, char *argv
[], void *userdata
) {
223 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
224 size_t max_name
= strlen("NAME"), max_type
= strlen("TYPE"), max_size
= strlen("USAGE"), max_crtime
= strlen("CREATED"), max_mtime
= strlen("MODIFIED");
225 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
226 _cleanup_free_ ImageInfo
*images
= NULL
;
227 size_t n_images
= 0, n_allocated
= 0, j
;
228 const char *name
, *type
, *object
;
229 sd_bus
*bus
= userdata
;
230 uint64_t crtime
, mtime
, size
;
235 pager_open(arg_no_pager
, false);
237 r
= sd_bus_call_method(bus
,
238 "org.freedesktop.machine1",
239 "/org/freedesktop/machine1",
240 "org.freedesktop.machine1.Manager",
246 log_error("Could not get images: %s", bus_error_message(&error
, -r
));
250 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "(ssbttto)");
252 return bus_log_parse_error(r
);
254 while ((r
= sd_bus_message_read(reply
, "(ssbttto)", &name
, &type
, &read_only
, &crtime
, &mtime
, &size
, &object
)) > 0) {
255 char buf
[MAX(FORMAT_TIMESTAMP_MAX
, FORMAT_BYTES_MAX
)];
258 if (name
[0] == '.' && !arg_all
)
261 if (!GREEDY_REALLOC(images
, n_allocated
, n_images
+ 1))
264 images
[n_images
].name
= name
;
265 images
[n_images
].type
= type
;
266 images
[n_images
].read_only
= read_only
;
267 images
[n_images
].crtime
= crtime
;
268 images
[n_images
].mtime
= mtime
;
269 images
[n_images
].size
= size
;
280 l
= strlen(strna(format_timestamp(buf
, sizeof(buf
), crtime
)));
286 l
= strlen(strna(format_timestamp(buf
, sizeof(buf
), mtime
)));
291 if (size
!= (uint64_t) -1) {
292 l
= strlen(strna(format_bytes(buf
, sizeof(buf
), size
)));
300 return bus_log_parse_error(r
);
302 r
= sd_bus_message_exit_container(reply
);
304 return bus_log_parse_error(r
);
306 qsort_safe(images
, n_images
, sizeof(ImageInfo
), compare_image_info
);
309 printf("%-*s %-*s %-3s %-*s %-*s %-*s\n",
310 (int) max_name
, "NAME",
311 (int) max_type
, "TYPE",
313 (int) max_size
, "USAGE",
314 (int) max_crtime
, "CREATED",
315 (int) max_mtime
, "MODIFIED");
317 for (j
= 0; j
< n_images
; j
++) {
318 char crtime_buf
[FORMAT_TIMESTAMP_MAX
], mtime_buf
[FORMAT_TIMESTAMP_MAX
], size_buf
[FORMAT_BYTES_MAX
];
320 printf("%-*s %-*s %s%-3s%s %-*s %-*s %-*s\n",
321 (int) max_name
, images
[j
].name
,
322 (int) max_type
, images
[j
].type
,
323 images
[j
].read_only
? ansi_highlight_red() : "", yes_no(images
[j
].read_only
), images
[j
].read_only
? ansi_normal() : "",
324 (int) max_size
, strna(format_bytes(size_buf
, sizeof(size_buf
), images
[j
].size
)),
325 (int) max_crtime
, strna(format_timestamp(crtime_buf
, sizeof(crtime_buf
), images
[j
].crtime
)),
326 (int) max_mtime
, strna(format_timestamp(mtime_buf
, sizeof(mtime_buf
), images
[j
].mtime
)));
330 printf("\n%zu images listed.\n", n_images
);
335 static int show_unit_cgroup(sd_bus
*bus
, const char *unit
, pid_t leader
) {
336 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
337 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
338 _cleanup_free_
char *path
= NULL
;
346 path
= unit_dbus_path_from_name(unit
);
350 r
= sd_bus_get_property(
352 "org.freedesktop.systemd1",
354 unit_dbus_interface_from_name(unit
),
360 return log_error_errno(r
, "Failed to query ControlGroup: %s", bus_error_message(&error
, r
));
362 r
= sd_bus_message_read(reply
, "s", &cgroup
);
364 return bus_log_parse_error(r
);
375 r
= unit_show_processes(bus
, unit
, cgroup
, "\t\t ", c
, get_output_flags(), &error
);
378 if (arg_transport
== BUS_TRANSPORT_REMOTE
)
381 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
383 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, cgroup
) != 0 && leader
<= 0)
386 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "\t\t ", c
, &leader
, leader
> 0, get_output_flags());
388 return log_error_errno(r
, "Failed to dump process list: %s", bus_error_message(&error
, r
));
393 static int print_addresses(sd_bus
*bus
, const char *name
, int ifi
, const char *prefix
, const char *prefix2
) {
394 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
402 r
= sd_bus_call_method(bus
,
403 "org.freedesktop.machine1",
404 "/org/freedesktop/machine1",
405 "org.freedesktop.machine1.Manager",
406 "GetMachineAddresses",
413 r
= sd_bus_message_enter_container(reply
, 'a', "(iay)");
415 return bus_log_parse_error(r
);
417 while ((r
= sd_bus_message_enter_container(reply
, 'r', "iay")) > 0) {
421 char buffer
[MAX(INET6_ADDRSTRLEN
, INET_ADDRSTRLEN
)];
423 r
= sd_bus_message_read(reply
, "i", &family
);
425 return bus_log_parse_error(r
);
427 r
= sd_bus_message_read_array(reply
, 'y', &a
, &sz
);
429 return bus_log_parse_error(r
);
431 fputs(prefix
, stdout
);
432 fputs(inet_ntop(family
, a
, buffer
, sizeof(buffer
)), stdout
);
433 if (family
== AF_INET6
&& ifi
> 0)
437 r
= sd_bus_message_exit_container(reply
);
439 return bus_log_parse_error(r
);
441 if (prefix
!= prefix2
)
445 return bus_log_parse_error(r
);
447 r
= sd_bus_message_exit_container(reply
);
449 return bus_log_parse_error(r
);
454 static int print_os_release(sd_bus
*bus
, const char *name
, const char *prefix
) {
455 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
456 const char *k
, *v
, *pretty
= NULL
;
463 r
= sd_bus_call_method(bus
,
464 "org.freedesktop.machine1",
465 "/org/freedesktop/machine1",
466 "org.freedesktop.machine1.Manager",
467 "GetMachineOSRelease",
474 r
= sd_bus_message_enter_container(reply
, 'a', "{ss}");
476 return bus_log_parse_error(r
);
478 while ((r
= sd_bus_message_read(reply
, "{ss}", &k
, &v
)) > 0) {
479 if (streq(k
, "PRETTY_NAME"))
484 return bus_log_parse_error(r
);
486 r
= sd_bus_message_exit_container(reply
);
488 return bus_log_parse_error(r
);
491 printf("%s%s\n", prefix
, pretty
);
496 typedef struct MachineStatusInfo
{
502 char *root_directory
;
504 struct dual_timestamp timestamp
;
509 static void machine_status_info_clear(MachineStatusInfo
*info
) {
515 free(info
->root_directory
);
521 static void print_machine_status_info(sd_bus
*bus
, MachineStatusInfo
*i
) {
522 char since1
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
523 char since2
[FORMAT_TIMESTAMP_MAX
], *s2
;
529 fputs(strna(i
->name
), stdout
);
531 if (!sd_id128_is_null(i
->id
))
532 printf("(" SD_ID128_FORMAT_STR
")\n", SD_ID128_FORMAT_VAL(i
->id
));
536 s1
= format_timestamp_relative(since1
, sizeof(since1
), i
->timestamp
.realtime
);
537 s2
= format_timestamp(since2
, sizeof(since2
), i
->timestamp
.realtime
);
540 printf("\t Since: %s; %s\n", s2
, s1
);
542 printf("\t Since: %s\n", s2
);
545 _cleanup_free_
char *t
= NULL
;
547 printf("\t Leader: %u", (unsigned) i
->leader
);
549 get_process_comm(i
->leader
, &t
);
557 printf("\t Service: %s", i
->service
);
560 printf("; class %s", i
->class);
564 printf("\t Class: %s\n", i
->class);
566 if (i
->root_directory
)
567 printf("\t Root: %s\n", i
->root_directory
);
569 if (i
->n_netif
> 0) {
572 fputs("\t Iface:", stdout
);
574 for (c
= 0; c
< i
->n_netif
; c
++) {
575 char name
[IF_NAMESIZE
+1] = "";
577 if (if_indextoname(i
->netif
[c
], name
)) {
586 printf(" %i", i
->netif
[c
]);
592 print_addresses(bus
, i
->name
, ifi
,
596 print_os_release(bus
, i
->name
, "\t OS: ");
599 printf("\t Unit: %s\n", i
->unit
);
600 show_unit_cgroup(bus
, i
->unit
, i
->leader
);
602 if (arg_transport
== BUS_TRANSPORT_LOCAL
)
604 show_journal_by_unit(
609 i
->timestamp
.monotonic
,
612 get_output_flags() | OUTPUT_BEGIN_NEWLINE
,
613 SD_JOURNAL_LOCAL_ONLY
,
619 static int map_netif(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
620 MachineStatusInfo
*i
= userdata
;
625 assert_cc(sizeof(int32_t) == sizeof(int));
626 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_INT32
, &v
, &l
);
632 i
->n_netif
= l
/ sizeof(int32_t);
633 i
->netif
= memdup(v
, l
);
640 static int show_machine_info(const char *verb
, sd_bus
*bus
, const char *path
, bool *new_line
) {
642 static const struct bus_properties_map map
[] = {
643 { "Name", "s", NULL
, offsetof(MachineStatusInfo
, name
) },
644 { "Class", "s", NULL
, offsetof(MachineStatusInfo
, class) },
645 { "Service", "s", NULL
, offsetof(MachineStatusInfo
, service
) },
646 { "Unit", "s", NULL
, offsetof(MachineStatusInfo
, unit
) },
647 { "RootDirectory", "s", NULL
, offsetof(MachineStatusInfo
, root_directory
) },
648 { "Leader", "u", NULL
, offsetof(MachineStatusInfo
, leader
) },
649 { "Timestamp", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.realtime
) },
650 { "TimestampMonotonic", "t", NULL
, offsetof(MachineStatusInfo
, timestamp
.monotonic
) },
651 { "Id", "ay", bus_map_id128
, offsetof(MachineStatusInfo
, id
) },
652 { "NetworkInterfaces", "ai", map_netif
, 0 },
656 _cleanup_(machine_status_info_clear
) MachineStatusInfo info
= {};
664 r
= bus_map_all_properties(bus
,
665 "org.freedesktop.machine1",
670 return log_error_errno(r
, "Could not get properties: %m");
676 print_machine_status_info(bus
, &info
);
681 static int show_machine_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
693 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, arg_property
, arg_value
, arg_all
);
695 log_error_errno(r
, "Could not get properties: %m");
700 static int show_machine(int argc
, char *argv
[], void *userdata
) {
702 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
703 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
704 bool properties
, new_line
= false;
705 sd_bus
*bus
= userdata
;
710 properties
= !strstr(argv
[0], "status");
712 pager_open(arg_no_pager
, false);
714 if (properties
&& argc
<= 1) {
716 /* If no argument is specified, inspect the manager
718 r
= show_machine_properties(bus
, "/org/freedesktop/machine1", &new_line
);
723 for (i
= 1; i
< argc
; i
++) {
724 const char *path
= NULL
;
726 r
= sd_bus_call_method(bus
,
727 "org.freedesktop.machine1",
728 "/org/freedesktop/machine1",
729 "org.freedesktop.machine1.Manager",
735 log_error("Could not get path to machine: %s", bus_error_message(&error
, -r
));
739 r
= sd_bus_message_read(reply
, "o", &path
);
741 return bus_log_parse_error(r
);
744 r
= show_machine_properties(bus
, path
, &new_line
);
746 r
= show_machine_info(argv
[0], bus
, path
, &new_line
);
752 typedef struct ImageStatusInfo
{
761 uint64_t usage_exclusive
;
762 uint64_t limit_exclusive
;
765 static void image_status_info_clear(ImageStatusInfo
*info
) {
774 static void print_image_status_info(sd_bus
*bus
, ImageStatusInfo
*i
) {
775 char ts_relative
[FORMAT_TIMESTAMP_RELATIVE_MAX
], *s1
;
776 char ts_absolute
[FORMAT_TIMESTAMP_MAX
], *s2
;
777 char bs
[FORMAT_BYTES_MAX
], *s3
;
778 char bs_exclusive
[FORMAT_BYTES_MAX
], *s4
;
784 fputs(i
->name
, stdout
);
789 printf("\t Type: %s\n", i
->type
);
792 printf("\t Path: %s\n", i
->path
);
794 printf("\t RO: %s%s%s\n",
795 i
->read_only
? ansi_highlight_red() : "",
796 i
->read_only
? "read-only" : "writable",
797 i
->read_only
? ansi_normal() : "");
799 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->crtime
);
800 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->crtime
);
802 printf("\t Created: %s; %s\n", s2
, s1
);
804 printf("\t Created: %s\n", s2
);
806 s1
= format_timestamp_relative(ts_relative
, sizeof(ts_relative
), i
->mtime
);
807 s2
= format_timestamp(ts_absolute
, sizeof(ts_absolute
), i
->mtime
);
809 printf("\tModified: %s; %s\n", s2
, s1
);
811 printf("\tModified: %s\n", s2
);
813 s3
= format_bytes(bs
, sizeof(bs
), i
->usage
);
814 s4
= i
->usage_exclusive
!= i
->usage
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->usage_exclusive
) : NULL
;
816 printf("\t Usage: %s (exclusive: %s)\n", s3
, s4
);
818 printf("\t Usage: %s\n", s3
);
820 s3
= format_bytes(bs
, sizeof(bs
), i
->limit
);
821 s4
= i
->limit_exclusive
!= i
->limit
? format_bytes(bs_exclusive
, sizeof(bs_exclusive
), i
->limit_exclusive
) : NULL
;
823 printf("\t Limit: %s (exclusive: %s)\n", s3
, s4
);
825 printf("\t Limit: %s\n", s3
);
828 static int show_image_info(sd_bus
*bus
, const char *path
, bool *new_line
) {
830 static const struct bus_properties_map map
[] = {
831 { "Name", "s", NULL
, offsetof(ImageStatusInfo
, name
) },
832 { "Path", "s", NULL
, offsetof(ImageStatusInfo
, path
) },
833 { "Type", "s", NULL
, offsetof(ImageStatusInfo
, type
) },
834 { "ReadOnly", "b", NULL
, offsetof(ImageStatusInfo
, read_only
) },
835 { "CreationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, crtime
) },
836 { "ModificationTimestamp", "t", NULL
, offsetof(ImageStatusInfo
, mtime
) },
837 { "Usage", "t", NULL
, offsetof(ImageStatusInfo
, usage
) },
838 { "Limit", "t", NULL
, offsetof(ImageStatusInfo
, limit
) },
839 { "UsageExclusive", "t", NULL
, offsetof(ImageStatusInfo
, usage_exclusive
) },
840 { "LimitExclusive", "t", NULL
, offsetof(ImageStatusInfo
, limit_exclusive
) },
844 _cleanup_(image_status_info_clear
) ImageStatusInfo info
= {};
851 r
= bus_map_all_properties(bus
,
852 "org.freedesktop.machine1",
857 return log_error_errno(r
, "Could not get properties: %m");
863 print_image_status_info(bus
, &info
);
868 typedef struct PoolStatusInfo
{
874 static void pool_status_info_clear(PoolStatusInfo
*info
) {
883 static void print_pool_status_info(sd_bus
*bus
, PoolStatusInfo
*i
) {
884 char bs
[FORMAT_BYTES_MAX
], *s
;
887 printf("\t Path: %s\n", i
->path
);
889 s
= format_bytes(bs
, sizeof(bs
), i
->usage
);
891 printf("\t Usage: %s\n", s
);
893 s
= format_bytes(bs
, sizeof(bs
), i
->limit
);
895 printf("\t Limit: %s\n", s
);
898 static int show_pool_info(sd_bus
*bus
) {
900 static const struct bus_properties_map map
[] = {
901 { "PoolPath", "s", NULL
, offsetof(PoolStatusInfo
, path
) },
902 { "PoolUsage", "t", NULL
, offsetof(PoolStatusInfo
, usage
) },
903 { "PoolLimit", "t", NULL
, offsetof(PoolStatusInfo
, limit
) },
907 _cleanup_(pool_status_info_clear
) PoolStatusInfo info
= {
908 .usage
= (uint64_t) -1,
909 .limit
= (uint64_t) -1,
915 r
= bus_map_all_properties(bus
,
916 "org.freedesktop.machine1",
917 "/org/freedesktop/machine1",
921 return log_error_errno(r
, "Could not get properties: %m");
923 print_pool_status_info(bus
, &info
);
929 static int show_image_properties(sd_bus
*bus
, const char *path
, bool *new_line
) {
941 r
= bus_print_all_properties(bus
, "org.freedesktop.machine1", path
, arg_property
, arg_value
, arg_all
);
943 log_error_errno(r
, "Could not get properties: %m");
948 static int show_image(int argc
, char *argv
[], void *userdata
) {
950 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
951 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
952 bool properties
, new_line
= false;
953 sd_bus
*bus
= userdata
;
958 properties
= !strstr(argv
[0], "status");
960 pager_open(arg_no_pager
, false);
964 /* If no argument is specified, inspect the manager
968 r
= show_image_properties(bus
, "/org/freedesktop/machine1", &new_line
);
970 r
= show_pool_info(bus
);
975 for (i
= 1; i
< argc
; i
++) {
976 const char *path
= NULL
;
978 r
= sd_bus_call_method(
980 "org.freedesktop.machine1",
981 "/org/freedesktop/machine1",
982 "org.freedesktop.machine1.Manager",
988 log_error("Could not get path to image: %s", bus_error_message(&error
, -r
));
992 r
= sd_bus_message_read(reply
, "o", &path
);
994 return bus_log_parse_error(r
);
997 r
= show_image_properties(bus
, path
, &new_line
);
999 r
= show_image_info(bus
, path
, &new_line
);
1005 static int kill_machine(int argc
, char *argv
[], void *userdata
) {
1006 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1007 sd_bus
*bus
= userdata
;
1012 polkit_agent_open_if_enabled();
1015 arg_kill_who
= "all";
1017 for (i
= 1; i
< argc
; i
++) {
1018 r
= sd_bus_call_method(
1020 "org.freedesktop.machine1",
1021 "/org/freedesktop/machine1",
1022 "org.freedesktop.machine1.Manager",
1026 "ssi", argv
[i
], arg_kill_who
, arg_signal
);
1028 log_error("Could not kill machine: %s", bus_error_message(&error
, -r
));
1036 static int reboot_machine(int argc
, char *argv
[], void *userdata
) {
1037 arg_kill_who
= "leader";
1038 arg_signal
= SIGINT
; /* sysvinit + systemd */
1040 return kill_machine(argc
, argv
, userdata
);
1043 static int poweroff_machine(int argc
, char *argv
[], void *userdata
) {
1044 arg_kill_who
= "leader";
1045 arg_signal
= SIGRTMIN
+4; /* only systemd */
1047 return kill_machine(argc
, argv
, userdata
);
1050 static int terminate_machine(int argc
, char *argv
[], void *userdata
) {
1051 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1052 sd_bus
*bus
= userdata
;
1057 polkit_agent_open_if_enabled();
1059 for (i
= 1; i
< argc
; i
++) {
1060 r
= sd_bus_call_method(
1062 "org.freedesktop.machine1",
1063 "/org/freedesktop/machine1",
1064 "org.freedesktop.machine1.Manager",
1070 log_error("Could not terminate machine: %s", bus_error_message(&error
, -r
));
1078 static int copy_files(int argc
, char *argv
[], void *userdata
) {
1079 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1080 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1081 _cleanup_free_
char *abs_host_path
= NULL
;
1082 char *dest
, *host_path
, *container_path
;
1083 sd_bus
*bus
= userdata
;
1089 polkit_agent_open_if_enabled();
1091 copy_from
= streq(argv
[0], "copy-from");
1092 dest
= argv
[3] ?: argv
[2];
1093 host_path
= copy_from
? dest
: argv
[2];
1094 container_path
= copy_from
? argv
[2] : dest
;
1096 if (!path_is_absolute(host_path
)) {
1097 r
= path_make_absolute_cwd(host_path
, &abs_host_path
);
1099 return log_error_errno(r
, "Failed to make path absolute: %m");
1101 host_path
= abs_host_path
;
1104 r
= sd_bus_message_new_method_call(
1107 "org.freedesktop.machine1",
1108 "/org/freedesktop/machine1",
1109 "org.freedesktop.machine1.Manager",
1110 copy_from
? "CopyFromMachine" : "CopyToMachine");
1112 return bus_log_create_error(r
);
1114 r
= sd_bus_message_append(
1118 copy_from
? container_path
: host_path
,
1119 copy_from
? host_path
: container_path
);
1121 return bus_log_create_error(r
);
1123 /* This is a slow operation, hence turn off any method call timeouts */
1124 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1126 return log_error_errno(r
, "Failed to copy: %s", bus_error_message(&error
, r
));
1131 static int bind_mount(int argc
, char *argv
[], void *userdata
) {
1132 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1133 sd_bus
*bus
= userdata
;
1138 polkit_agent_open_if_enabled();
1140 r
= sd_bus_call_method(
1142 "org.freedesktop.machine1",
1143 "/org/freedesktop/machine1",
1144 "org.freedesktop.machine1.Manager",
1155 log_error("Failed to bind mount: %s", bus_error_message(&error
, -r
));
1162 static int on_machine_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
1163 PTYForward
** forward
= (PTYForward
**) userdata
;
1170 /* If the forwarder is already initialized, tell it to
1171 * exit on the next vhangup(), so that we still flush
1172 * out what might be queued and exit then. */
1174 r
= pty_forward_set_ignore_vhangup(*forward
, false);
1178 log_error_errno(r
, "Failed to set ignore_vhangup flag: %m");
1181 /* On error, or when the forwarder is not initialized yet, quit immediately */
1182 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), EXIT_FAILURE
);
1186 static int process_forward(sd_event
*event
, PTYForward
**forward
, int master
, PTYForwardFlags flags
, const char *name
) {
1192 assert(master
>= 0);
1195 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1197 if (streq(name
, ".host"))
1198 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1200 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name
);
1202 sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1203 sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1205 r
= pty_forward_new(event
, master
, flags
, forward
);
1207 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1209 r
= sd_event_loop(event
);
1211 return log_error_errno(r
, "Failed to run event loop: %m");
1213 pty_forward_get_last_char(*forward
, &last_char
);
1216 (flags
& PTY_FORWARD_IGNORE_VHANGUP
) &&
1217 pty_forward_get_ignore_vhangup(*forward
) == 0;
1219 *forward
= pty_forward_free(*forward
);
1221 if (last_char
!= '\n')
1222 fputc('\n', stdout
);
1225 log_info("Machine %s terminated.", name
);
1226 else if (streq(name
, ".host"))
1227 log_info("Connection to the local host terminated.");
1229 log_info("Connection to machine %s terminated.", name
);
1231 sd_event_get_exit_code(event
, &ret
);
1235 static int login_machine(int argc
, char *argv
[], void *userdata
) {
1236 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1237 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1238 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1239 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1240 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1242 sd_bus
*bus
= userdata
;
1243 const char *pty
, *match
, *machine
;
1247 if (!strv_isempty(arg_setenv
) || arg_uid
) {
1248 log_error("--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1252 if (arg_transport
!= BUS_TRANSPORT_LOCAL
&&
1253 arg_transport
!= BUS_TRANSPORT_MACHINE
) {
1254 log_error("Login only supported on local machines.");
1258 polkit_agent_open_if_enabled();
1260 r
= sd_event_default(&event
);
1262 return log_error_errno(r
, "Failed to get event loop: %m");
1264 r
= sd_bus_attach_event(bus
, event
, 0);
1266 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1268 machine
= argc
< 2 || isempty(argv
[1]) ? ".host" : argv
[1];
1270 match
= strjoina("type='signal',"
1271 "sender='org.freedesktop.machine1',"
1272 "path='/org/freedesktop/machine1',",
1273 "interface='org.freedesktop.machine1.Manager',"
1274 "member='MachineRemoved',"
1275 "arg0='", machine
, "'");
1277 r
= sd_bus_add_match(bus
, &slot
, match
, on_machine_removed
, &forward
);
1279 return log_error_errno(r
, "Failed to add machine removal match: %m");
1281 r
= sd_bus_call_method(
1283 "org.freedesktop.machine1",
1284 "/org/freedesktop/machine1",
1285 "org.freedesktop.machine1.Manager",
1291 log_error("Failed to get login PTY: %s", bus_error_message(&error
, -r
));
1295 r
= sd_bus_message_read(reply
, "hs", &master
, &pty
);
1297 return bus_log_parse_error(r
);
1299 return process_forward(event
, &forward
, master
, PTY_FORWARD_IGNORE_VHANGUP
, machine
);
1302 static int shell_machine(int argc
, char *argv
[], void *userdata
) {
1303 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *m
= NULL
;
1304 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1305 _cleanup_(pty_forward_freep
) PTYForward
*forward
= NULL
;
1306 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot
= NULL
;
1307 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1309 sd_bus
*bus
= userdata
;
1310 const char *pty
, *match
, *machine
, *path
, *uid
= NULL
;
1314 if (arg_transport
!= BUS_TRANSPORT_LOCAL
&&
1315 arg_transport
!= BUS_TRANSPORT_MACHINE
) {
1316 log_error("Shell only supported on local machines.");
1320 /* Pass $TERM to shell session, if not explicitly specified. */
1321 if (!strv_find_prefix(arg_setenv
, "TERM=")) {
1324 t
= strv_find_prefix(environ
, "TERM=");
1326 if (strv_extend(&arg_setenv
, t
) < 0)
1331 polkit_agent_open_if_enabled();
1333 r
= sd_event_default(&event
);
1335 return log_error_errno(r
, "Failed to get event loop: %m");
1337 r
= sd_bus_attach_event(bus
, event
, 0);
1339 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1341 machine
= argc
< 2 || isempty(argv
[1]) ? NULL
: argv
[1];
1348 at
= strchr(machine
, '@');
1350 uid
= strndupa(machine
, at
- machine
);
1355 if (isempty(machine
))
1358 match
= strjoina("type='signal',"
1359 "sender='org.freedesktop.machine1',"
1360 "path='/org/freedesktop/machine1',",
1361 "interface='org.freedesktop.machine1.Manager',"
1362 "member='MachineRemoved',"
1363 "arg0='", machine
, "'");
1365 r
= sd_bus_add_match(bus
, &slot
, match
, on_machine_removed
, &forward
);
1367 return log_error_errno(r
, "Failed to add machine removal match: %m");
1369 r
= sd_bus_message_new_method_call(
1372 "org.freedesktop.machine1",
1373 "/org/freedesktop/machine1",
1374 "org.freedesktop.machine1.Manager",
1375 "OpenMachineShell");
1377 return bus_log_create_error(r
);
1379 path
= argc
< 3 || isempty(argv
[2]) ? NULL
: argv
[2];
1381 r
= sd_bus_message_append(m
, "sss", machine
, uid
, path
);
1383 return bus_log_create_error(r
);
1385 r
= sd_bus_message_append_strv(m
, strv_length(argv
) <= 3 ? NULL
: argv
+ 2);
1387 return bus_log_create_error(r
);
1389 r
= sd_bus_message_append_strv(m
, arg_setenv
);
1391 return bus_log_create_error(r
);
1393 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1395 log_error("Failed to get shell PTY: %s", bus_error_message(&error
, -r
));
1399 r
= sd_bus_message_read(reply
, "hs", &master
, &pty
);
1401 return bus_log_parse_error(r
);
1403 return process_forward(event
, &forward
, master
, 0, machine
);
1406 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1407 sd_bus
*bus
= userdata
;
1412 polkit_agent_open_if_enabled();
1414 for (i
= 1; i
< argc
; i
++) {
1415 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1416 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1418 r
= sd_bus_message_new_method_call(
1421 "org.freedesktop.machine1",
1422 "/org/freedesktop/machine1",
1423 "org.freedesktop.machine1.Manager",
1426 return bus_log_create_error(r
);
1428 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1430 return bus_log_create_error(r
);
1432 /* This is a slow operation, hence turn off any method call timeouts */
1433 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1435 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1441 static int rename_image(int argc
, char *argv
[], void *userdata
) {
1442 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1443 sd_bus
*bus
= userdata
;
1446 polkit_agent_open_if_enabled();
1448 r
= sd_bus_call_method(
1450 "org.freedesktop.machine1",
1451 "/org/freedesktop/machine1",
1452 "org.freedesktop.machine1.Manager",
1456 "ss", argv
[1], argv
[2]);
1458 log_error("Could not rename image: %s", bus_error_message(&error
, -r
));
1465 static int clone_image(int argc
, char *argv
[], void *userdata
) {
1466 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1467 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1468 sd_bus
*bus
= userdata
;
1471 polkit_agent_open_if_enabled();
1473 r
= sd_bus_message_new_method_call(
1476 "org.freedesktop.machine1",
1477 "/org/freedesktop/machine1",
1478 "org.freedesktop.machine1.Manager",
1481 return bus_log_create_error(r
);
1483 r
= sd_bus_message_append(m
, "ssb", argv
[1], argv
[2], arg_read_only
);
1485 return bus_log_create_error(r
);
1487 /* This is a slow operation, hence turn off any method call timeouts */
1488 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1490 return log_error_errno(r
, "Could not clone image: %s", bus_error_message(&error
, r
));
1495 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1496 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1497 sd_bus
*bus
= userdata
;
1501 b
= parse_boolean(argv
[2]);
1503 log_error("Failed to parse boolean argument: %s", argv
[2]);
1508 polkit_agent_open_if_enabled();
1510 r
= sd_bus_call_method(
1512 "org.freedesktop.machine1",
1513 "/org/freedesktop/machine1",
1514 "org.freedesktop.machine1.Manager",
1515 "MarkImageReadOnly",
1520 log_error("Could not mark image read-only: %s", bus_error_message(&error
, -r
));
1527 static int image_exists(sd_bus
*bus
, const char *name
) {
1528 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1534 r
= sd_bus_call_method(
1536 "org.freedesktop.machine1",
1537 "/org/freedesktop/machine1",
1538 "org.freedesktop.machine1.Manager",
1544 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_IMAGE
))
1547 return log_error_errno(r
, "Failed to check whether image %s exists: %s", name
, bus_error_message(&error
, -r
));
1553 static int make_service_name(const char *name
, char **ret
) {
1559 if (!machine_name_is_valid(name
)) {
1560 log_error("Invalid machine name %s.", name
);
1564 r
= unit_name_build("systemd-nspawn", name
, ".service", ret
);
1566 return log_error_errno(r
, "Failed to build unit name: %m");
1571 static int start_machine(int argc
, char *argv
[], void *userdata
) {
1572 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1573 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1574 sd_bus
*bus
= userdata
;
1579 polkit_agent_open_if_enabled();
1581 r
= bus_wait_for_jobs_new(bus
, &w
);
1585 for (i
= 1; i
< argc
; i
++) {
1586 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1587 _cleanup_free_
char *unit
= NULL
;
1590 r
= make_service_name(argv
[i
], &unit
);
1594 r
= image_exists(bus
, argv
[i
]);
1598 log_error("Machine image '%s' does not exist.", argv
[1]);
1602 r
= sd_bus_call_method(
1604 "org.freedesktop.systemd1",
1605 "/org/freedesktop/systemd1",
1606 "org.freedesktop.systemd1.Manager",
1610 "ss", unit
, "fail");
1612 log_error("Failed to start unit: %s", bus_error_message(&error
, -r
));
1616 r
= sd_bus_message_read(reply
, "o", &object
);
1618 return bus_log_parse_error(r
);
1620 r
= bus_wait_for_jobs_add(w
, object
);
1625 r
= bus_wait_for_jobs(w
, arg_quiet
, NULL
);
1632 static int enable_machine(int argc
, char *argv
[], void *userdata
) {
1633 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1634 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1635 UnitFileChange
*changes
= NULL
;
1636 unsigned n_changes
= 0;
1637 int carries_install_info
= 0;
1638 const char *method
= NULL
;
1639 sd_bus
*bus
= userdata
;
1644 polkit_agent_open_if_enabled();
1646 method
= streq(argv
[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1648 r
= sd_bus_message_new_method_call(
1651 "org.freedesktop.systemd1",
1652 "/org/freedesktop/systemd1",
1653 "org.freedesktop.systemd1.Manager",
1656 return bus_log_create_error(r
);
1658 r
= sd_bus_message_open_container(m
, 'a', "s");
1660 return bus_log_create_error(r
);
1662 for (i
= 1; i
< argc
; i
++) {
1663 _cleanup_free_
char *unit
= NULL
;
1665 r
= make_service_name(argv
[i
], &unit
);
1669 r
= image_exists(bus
, argv
[i
]);
1673 log_error("Machine image '%s' does not exist.", argv
[1]);
1677 r
= sd_bus_message_append(m
, "s", unit
);
1679 return bus_log_create_error(r
);
1682 r
= sd_bus_message_close_container(m
);
1684 return bus_log_create_error(r
);
1686 if (streq(argv
[0], "enable"))
1687 r
= sd_bus_message_append(m
, "bb", false, false);
1689 r
= sd_bus_message_append(m
, "b", false);
1691 return bus_log_create_error(r
);
1693 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1695 log_error("Failed to enable or disable unit: %s", bus_error_message(&error
, -r
));
1699 if (streq(argv
[0], "enable")) {
1700 r
= sd_bus_message_read(reply
, "b", carries_install_info
);
1702 return bus_log_parse_error(r
);
1705 r
= bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
1709 r
= sd_bus_call_method(
1711 "org.freedesktop.systemd1",
1712 "/org/freedesktop/systemd1",
1713 "org.freedesktop.systemd1.Manager",
1719 log_error("Failed to reload daemon: %s", bus_error_message(&error
, -r
));
1726 unit_file_changes_free(changes
, n_changes
);
1731 static int match_log_message(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1732 const char **our_path
= userdata
, *line
;
1739 r
= sd_bus_message_read(m
, "us", &priority
, &line
);
1741 bus_log_parse_error(r
);
1745 if (!streq_ptr(*our_path
, sd_bus_message_get_path(m
)))
1748 if (arg_quiet
&& LOG_PRI(priority
) >= LOG_INFO
)
1751 log_full(priority
, "%s", line
);
1755 static int match_transfer_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1756 const char **our_path
= userdata
, *path
, *result
;
1763 r
= sd_bus_message_read(m
, "uos", &id
, &path
, &result
);
1765 bus_log_parse_error(r
);
1769 if (!streq_ptr(*our_path
, path
))
1772 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m
)), !streq_ptr(result
, "done"));
1776 static int transfer_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1781 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32
"\" to abort transfer.", PTR_TO_UINT32(userdata
));
1783 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
1787 static int transfer_image_common(sd_bus
*bus
, sd_bus_message
*m
) {
1788 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*slot_job_removed
= NULL
, *slot_log_message
= NULL
;
1789 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1790 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1791 _cleanup_(sd_event_unrefp
) sd_event
* event
= NULL
;
1792 const char *path
= NULL
;
1799 polkit_agent_open_if_enabled();
1801 r
= sd_event_default(&event
);
1803 return log_error_errno(r
, "Failed to get event loop: %m");
1805 r
= sd_bus_attach_event(bus
, event
, 0);
1807 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1809 r
= sd_bus_add_match(
1813 "sender='org.freedesktop.import1',"
1814 "interface='org.freedesktop.import1.Manager',"
1815 "member='TransferRemoved',"
1816 "path='/org/freedesktop/import1'",
1817 match_transfer_removed
, &path
);
1819 return log_error_errno(r
, "Failed to install match: %m");
1821 r
= sd_bus_add_match(
1825 "sender='org.freedesktop.import1',"
1826 "interface='org.freedesktop.import1.Transfer',"
1827 "member='LogMessage'",
1828 match_log_message
, &path
);
1830 return log_error_errno(r
, "Failed to install match: %m");
1832 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1834 log_error("Failed to transfer image: %s", bus_error_message(&error
, -r
));
1838 r
= sd_bus_message_read(reply
, "uo", &id
, &path
);
1840 return bus_log_parse_error(r
);
1842 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1845 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id
);
1847 sd_event_add_signal(event
, NULL
, SIGINT
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1848 sd_event_add_signal(event
, NULL
, SIGTERM
, transfer_signal_handler
, UINT32_TO_PTR(id
));
1850 r
= sd_event_loop(event
);
1852 return log_error_errno(r
, "Failed to run event loop: %m");
1857 static int import_tar(int argc
, char *argv
[], void *userdata
) {
1858 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1859 _cleanup_free_
char *ll
= NULL
;
1860 _cleanup_close_
int fd
= -1;
1861 const char *local
= NULL
, *path
= NULL
;
1862 sd_bus
*bus
= userdata
;
1869 if (isempty(path
) || streq(path
, "-"))
1875 local
= basename(path
);
1876 if (isempty(local
) || streq(local
, "-"))
1880 log_error("Need either path or local name.");
1884 r
= tar_strip_suffixes(local
, &ll
);
1890 if (!machine_name_is_valid(local
)) {
1891 log_error("Local name %s is not a suitable machine name.", local
);
1896 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1898 return log_error_errno(errno
, "Failed to open %s: %m", path
);
1901 r
= sd_bus_message_new_method_call(
1904 "org.freedesktop.import1",
1905 "/org/freedesktop/import1",
1906 "org.freedesktop.import1.Manager",
1909 return bus_log_create_error(r
);
1911 r
= sd_bus_message_append(
1914 fd
>= 0 ? fd
: STDIN_FILENO
,
1919 return bus_log_create_error(r
);
1921 return transfer_image_common(bus
, m
);
1924 static int import_raw(int argc
, char *argv
[], void *userdata
) {
1925 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1926 _cleanup_free_
char *ll
= NULL
;
1927 _cleanup_close_
int fd
= -1;
1928 const char *local
= NULL
, *path
= NULL
;
1929 sd_bus
*bus
= userdata
;
1936 if (isempty(path
) || streq(path
, "-"))
1942 local
= basename(path
);
1943 if (isempty(local
) || streq(local
, "-"))
1947 log_error("Need either path or local name.");
1951 r
= raw_strip_suffixes(local
, &ll
);
1957 if (!machine_name_is_valid(local
)) {
1958 log_error("Local name %s is not a suitable machine name.", local
);
1963 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1965 return log_error_errno(errno
, "Failed to open %s: %m", path
);
1968 r
= sd_bus_message_new_method_call(
1971 "org.freedesktop.import1",
1972 "/org/freedesktop/import1",
1973 "org.freedesktop.import1.Manager",
1976 return bus_log_create_error(r
);
1978 r
= sd_bus_message_append(
1981 fd
>= 0 ? fd
: STDIN_FILENO
,
1986 return bus_log_create_error(r
);
1988 return transfer_image_common(bus
, m
);
1991 static void determine_compression_from_filename(const char *p
) {
1998 if (endswith(p
, ".xz"))
2000 else if (endswith(p
, ".gz"))
2001 arg_format
= "gzip";
2002 else if (endswith(p
, ".bz2"))
2003 arg_format
= "bzip2";
2006 static int export_tar(int argc
, char *argv
[], void *userdata
) {
2007 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2008 _cleanup_close_
int fd
= -1;
2009 const char *local
= NULL
, *path
= NULL
;
2010 sd_bus
*bus
= userdata
;
2016 if (!machine_name_is_valid(local
)) {
2017 log_error("Machine name %s is not valid.", local
);
2023 if (isempty(path
) || streq(path
, "-"))
2027 determine_compression_from_filename(path
);
2029 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2031 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2034 r
= sd_bus_message_new_method_call(
2037 "org.freedesktop.import1",
2038 "/org/freedesktop/import1",
2039 "org.freedesktop.import1.Manager",
2042 return bus_log_create_error(r
);
2044 r
= sd_bus_message_append(
2048 fd
>= 0 ? fd
: STDOUT_FILENO
,
2051 return bus_log_create_error(r
);
2053 return transfer_image_common(bus
, m
);
2056 static int export_raw(int argc
, char *argv
[], void *userdata
) {
2057 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2058 _cleanup_close_
int fd
= -1;
2059 const char *local
= NULL
, *path
= NULL
;
2060 sd_bus
*bus
= userdata
;
2066 if (!machine_name_is_valid(local
)) {
2067 log_error("Machine name %s is not valid.", local
);
2073 if (isempty(path
) || streq(path
, "-"))
2077 determine_compression_from_filename(path
);
2079 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
2081 return log_error_errno(errno
, "Failed to open %s: %m", path
);
2084 r
= sd_bus_message_new_method_call(
2087 "org.freedesktop.import1",
2088 "/org/freedesktop/import1",
2089 "org.freedesktop.import1.Manager",
2092 return bus_log_create_error(r
);
2094 r
= sd_bus_message_append(
2098 fd
>= 0 ? fd
: STDOUT_FILENO
,
2101 return bus_log_create_error(r
);
2103 return transfer_image_common(bus
, m
);
2106 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
2107 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2108 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2109 const char *local
, *remote
;
2110 sd_bus
*bus
= userdata
;
2116 if (!http_url_is_valid(remote
)) {
2117 log_error("URL '%s' is not valid.", remote
);
2124 r
= import_url_last_component(remote
, &l
);
2126 return log_error_errno(r
, "Failed to get final component of URL: %m");
2131 if (isempty(local
) || streq(local
, "-"))
2135 r
= tar_strip_suffixes(local
, &ll
);
2141 if (!machine_name_is_valid(local
)) {
2142 log_error("Local name %s is not a suitable machine name.", local
);
2147 r
= sd_bus_message_new_method_call(
2150 "org.freedesktop.import1",
2151 "/org/freedesktop/import1",
2152 "org.freedesktop.import1.Manager",
2155 return bus_log_create_error(r
);
2157 r
= sd_bus_message_append(
2162 import_verify_to_string(arg_verify
),
2165 return bus_log_create_error(r
);
2167 return transfer_image_common(bus
, m
);
2170 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
2171 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2172 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
2173 const char *local
, *remote
;
2174 sd_bus
*bus
= userdata
;
2180 if (!http_url_is_valid(remote
)) {
2181 log_error("URL '%s' is not valid.", remote
);
2188 r
= import_url_last_component(remote
, &l
);
2190 return log_error_errno(r
, "Failed to get final component of URL: %m");
2195 if (isempty(local
) || streq(local
, "-"))
2199 r
= raw_strip_suffixes(local
, &ll
);
2205 if (!machine_name_is_valid(local
)) {
2206 log_error("Local name %s is not a suitable machine name.", local
);
2211 r
= sd_bus_message_new_method_call(
2214 "org.freedesktop.import1",
2215 "/org/freedesktop/import1",
2216 "org.freedesktop.import1.Manager",
2219 return bus_log_create_error(r
);
2221 r
= sd_bus_message_append(
2226 import_verify_to_string(arg_verify
),
2229 return bus_log_create_error(r
);
2231 return transfer_image_common(bus
, m
);
2234 typedef struct TransferInfo
{
2242 static int compare_transfer_info(const void *a
, const void *b
) {
2243 const TransferInfo
*x
= a
, *y
= b
;
2245 return strcmp(x
->local
, y
->local
);
2248 static int list_transfers(int argc
, char *argv
[], void *userdata
) {
2249 size_t max_type
= strlen("TYPE"), max_local
= strlen("LOCAL"), max_remote
= strlen("REMOTE");
2250 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2251 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2252 _cleanup_free_ TransferInfo
*transfers
= NULL
;
2253 size_t n_transfers
= 0, n_allocated
= 0, j
;
2254 const char *type
, *remote
, *local
, *object
;
2255 sd_bus
*bus
= userdata
;
2256 uint32_t id
, max_id
= 0;
2260 pager_open(arg_no_pager
, false);
2262 r
= sd_bus_call_method(bus
,
2263 "org.freedesktop.import1",
2264 "/org/freedesktop/import1",
2265 "org.freedesktop.import1.Manager",
2271 log_error("Could not get transfers: %s", bus_error_message(&error
, -r
));
2275 r
= sd_bus_message_enter_container(reply
, 'a', "(usssdo)");
2277 return bus_log_parse_error(r
);
2279 while ((r
= sd_bus_message_read(reply
, "(usssdo)", &id
, &type
, &remote
, &local
, &progress
, &object
)) > 0) {
2282 if (!GREEDY_REALLOC(transfers
, n_allocated
, n_transfers
+ 1))
2285 transfers
[n_transfers
].id
= id
;
2286 transfers
[n_transfers
].type
= type
;
2287 transfers
[n_transfers
].remote
= remote
;
2288 transfers
[n_transfers
].local
= local
;
2289 transfers
[n_transfers
].progress
= progress
;
2309 return bus_log_parse_error(r
);
2311 r
= sd_bus_message_exit_container(reply
);
2313 return bus_log_parse_error(r
);
2315 qsort_safe(transfers
, n_transfers
, sizeof(TransferInfo
), compare_transfer_info
);
2318 printf("%-*s %-*s %-*s %-*s %-*s\n",
2319 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), "ID",
2321 (int) max_type
, "TYPE",
2322 (int) max_local
, "LOCAL",
2323 (int) max_remote
, "REMOTE");
2325 for (j
= 0; j
< n_transfers
; j
++)
2326 printf("%*" PRIu32
" %*u%% %-*s %-*s %-*s\n",
2327 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id
)), transfers
[j
].id
,
2328 (int) 6, (unsigned) (transfers
[j
].progress
* 100),
2329 (int) max_type
, transfers
[j
].type
,
2330 (int) max_local
, transfers
[j
].local
,
2331 (int) max_remote
, transfers
[j
].remote
);
2334 printf("\n%zu transfers listed.\n", n_transfers
);
2339 static int cancel_transfer(int argc
, char *argv
[], void *userdata
) {
2340 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2341 sd_bus
*bus
= userdata
;
2346 polkit_agent_open_if_enabled();
2348 for (i
= 1; i
< argc
; i
++) {
2351 r
= safe_atou32(argv
[i
], &id
);
2353 return log_error_errno(r
, "Failed to parse transfer id: %s", argv
[i
]);
2355 r
= sd_bus_call_method(
2357 "org.freedesktop.import1",
2358 "/org/freedesktop/import1",
2359 "org.freedesktop.import1.Manager",
2365 log_error("Could not cancel transfer: %s", bus_error_message(&error
, -r
));
2373 static int set_limit(int argc
, char *argv
[], void *userdata
) {
2374 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2375 sd_bus
*bus
= userdata
;
2379 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
2380 limit
= (uint64_t) -1;
2382 r
= parse_size(argv
[argc
-1], 1024, &limit
);
2384 return log_error("Failed to parse size: %s", argv
[argc
-1]);
2388 /* With two arguments changes the quota limit of the
2389 * specified image */
2390 r
= sd_bus_call_method(
2392 "org.freedesktop.machine1",
2393 "/org/freedesktop/machine1",
2394 "org.freedesktop.machine1.Manager",
2398 "st", argv
[1], limit
);
2400 /* With one argument changes the pool quota limit */
2401 r
= sd_bus_call_method(
2403 "org.freedesktop.machine1",
2404 "/org/freedesktop/machine1",
2405 "org.freedesktop.machine1.Manager",
2412 log_error("Could not set limit: %s", bus_error_message(&error
, -r
));
2419 static int clean_images(int argc
, char *argv
[], void *userdata
) {
2420 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2421 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2422 uint64_t usage
, total
= 0;
2423 char fb
[FORMAT_BYTES_MAX
];
2424 sd_bus
*bus
= userdata
;
2429 r
= sd_bus_message_new_method_call(
2432 "org.freedesktop.machine1",
2433 "/org/freedesktop/machine1",
2434 "org.freedesktop.machine1.Manager",
2437 return bus_log_create_error(r
);
2439 r
= sd_bus_message_append(m
, "s", arg_all
? "all" : "hidden");
2441 return bus_log_create_error(r
);
2443 /* This is a slow operation, hence permit a longer time for completion. */
2444 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, &reply
);
2446 return log_error_errno(r
, "Could not clean pool: %s", bus_error_message(&error
, r
));
2448 r
= sd_bus_message_enter_container(reply
, 'a', "(st)");
2450 return bus_log_parse_error(r
);
2452 while ((r
= sd_bus_message_read(reply
, "(st)", &name
, &usage
)) > 0) {
2453 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2454 name
, format_bytes(fb
, sizeof(fb
), usage
));
2460 r
= sd_bus_message_exit_container(reply
);
2462 return bus_log_parse_error(r
);
2464 log_info("Removed %u images in total. Total freed exclusive disk space %s.",
2465 c
, format_bytes(fb
, sizeof(fb
), total
));
2470 static int help(int argc
, char *argv
[], void *userdata
) {
2472 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2473 "Send control commands to or query the virtual machine and container\n"
2474 "registration manager.\n\n"
2475 " -h --help Show this help\n"
2476 " --version Show package version\n"
2477 " --no-pager Do not pipe output into a pager\n"
2478 " --no-legend Do not show the headers and footers\n"
2479 " --no-ask-password Do not ask for system passwords\n"
2480 " -H --host=[USER@]HOST Operate on remote host\n"
2481 " -M --machine=CONTAINER Operate on local container\n"
2482 " -p --property=NAME Show only properties by this name\n"
2483 " -q --quiet Suppress output\n"
2484 " -a --all Show all properties, including empty ones\n"
2485 " --value When showing properties, only print the value\n"
2486 " -l --full Do not ellipsize output\n"
2487 " --kill-who=WHO Who to send signal to\n"
2488 " -s --signal=SIGNAL Which signal to send\n"
2489 " --uid=USER Specify user ID to invoke shell as\n"
2490 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
2491 " --read-only Create read-only bind mount\n"
2492 " --mkdir Create directory before bind mounting, if missing\n"
2493 " -n --lines=INTEGER Number of journal entries to show\n"
2494 " -o --output=STRING Change journal output mode (short,\n"
2495 " short-monotonic, verbose, export, json,\n"
2496 " json-pretty, json-sse, cat)\n"
2497 " --verify=MODE Verification mode for downloaded images (no,\n"
2498 " checksum, signature)\n"
2499 " --force Download image even if already exists\n\n"
2500 "Machine Commands:\n"
2501 " list List running VMs and containers\n"
2502 " status NAME... Show VM/container details\n"
2503 " show [NAME...] Show properties of one or more VMs/containers\n"
2504 " start NAME... Start container as a service\n"
2505 " login [NAME] Get a login prompt in a container or on the\n"
2507 " shell [[USER@]NAME [COMMAND...]]\n"
2508 " Invoke a shell (or other command) in a container\n"
2509 " or on the local host\n"
2510 " enable NAME... Enable automatic container start at boot\n"
2511 " disable NAME... Disable automatic container start at boot\n"
2512 " poweroff NAME... Power off one or more containers\n"
2513 " reboot NAME... Reboot one or more containers\n"
2514 " terminate NAME... Terminate one or more VMs/containers\n"
2515 " kill NAME... Send signal to processes of a VM/container\n"
2516 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
2517 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2518 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
2520 " list-images Show available container and VM images\n"
2521 " image-status [NAME...] Show image details\n"
2522 " show-image [NAME...] Show properties of image\n"
2523 " clone NAME NAME Clone an image\n"
2524 " rename NAME NAME Rename an image\n"
2525 " read-only NAME [BOOL] Mark or unmark image read-only\n"
2526 " remove NAME... Remove an image\n"
2527 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2528 " clean Remove hidden (or all) images\n\n"
2529 "Image Transfer Commands:\n"
2530 " pull-tar URL [NAME] Download a TAR container image\n"
2531 " pull-raw URL [NAME] Download a RAW container or VM image\n"
2532 " import-tar FILE [NAME] Import a local TAR container image\n"
2533 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
2534 " export-tar NAME [FILE] Export a TAR container image locally\n"
2535 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
2536 " list-transfers Show list of downloads in progress\n"
2537 " cancel-transfer Cancel a download\n"
2538 , program_invocation_short_name
);
2543 static int parse_argv(int argc
, char *argv
[]) {
2546 ARG_VERSION
= 0x100,
2553 ARG_NO_ASK_PASSWORD
,
2560 static const struct option options
[] = {
2561 { "help", no_argument
, NULL
, 'h' },
2562 { "version", no_argument
, NULL
, ARG_VERSION
},
2563 { "property", required_argument
, NULL
, 'p' },
2564 { "all", no_argument
, NULL
, 'a' },
2565 { "value", no_argument
, NULL
, ARG_VALUE
},
2566 { "full", no_argument
, NULL
, 'l' },
2567 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
2568 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
2569 { "kill-who", required_argument
, NULL
, ARG_KILL_WHO
},
2570 { "signal", required_argument
, NULL
, 's' },
2571 { "host", required_argument
, NULL
, 'H' },
2572 { "machine", required_argument
, NULL
, 'M' },
2573 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
2574 { "mkdir", no_argument
, NULL
, ARG_MKDIR
},
2575 { "quiet", no_argument
, NULL
, 'q' },
2576 { "lines", required_argument
, NULL
, 'n' },
2577 { "output", required_argument
, NULL
, 'o' },
2578 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
2579 { "verify", required_argument
, NULL
, ARG_VERIFY
},
2580 { "force", no_argument
, NULL
, ARG_FORCE
},
2581 { "format", required_argument
, NULL
, ARG_FORMAT
},
2582 { "uid", required_argument
, NULL
, ARG_UID
},
2583 { "setenv", required_argument
, NULL
, 'E' },
2587 bool reorder
= false;
2588 int c
, r
, shell
= -1;
2594 static const char option_string
[] = "-hp:als:H:M:qn:o:";
2596 c
= getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2602 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2603 * non-option argument was discovered. */
2607 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2608 * for switches after the main verb. However, for "shell" we really don't want that, since we
2609 * want that switches specified after the machine name are passed to the program to execute,
2610 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2611 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2612 * non-option parameter. If it's the verb "shell" we remember its position and continue
2613 * processing options. In this case, as soon as we hit the next non-option argument we found
2614 * the machine name, and stop further processing. If the first non-option argument is any other
2615 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2619 /* If we already found the "shell" verb on the command line, and now found the next
2620 * non-option argument, then this is the machine name and we should stop processing
2621 * further arguments. */
2622 optind
--; /* don't process this argument, go one step back */
2625 if (streq(optarg
, "shell"))
2626 /* Remember the position of the "shell" verb, and continue processing normally. */
2631 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2632 * processing normally. */
2635 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2636 * at least once with a reset option index. Hence, let's reset the option index here,
2637 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2638 * likely already processed it), and the bump the option index so that we read the
2639 * intended argument again. */
2640 saved_optind
= optind
;
2642 (void) getopt_long(argc
, argv
, option_string
+ reorder
, options
, NULL
);
2643 optind
= saved_optind
- 1; /* go one step back, process this argument again */
2649 return help(0, NULL
, NULL
);
2655 r
= strv_extend(&arg_property
, optarg
);
2659 /* If the user asked for a particular
2660 * property, show it to him, even if it is
2678 if (safe_atou(optarg
, &arg_lines
) < 0) {
2679 log_error("Failed to parse lines '%s'", optarg
);
2685 arg_output
= output_mode_from_string(optarg
);
2686 if (arg_output
< 0) {
2687 log_error("Unknown output '%s'.", optarg
);
2693 arg_no_pager
= true;
2701 arg_kill_who
= optarg
;
2705 arg_signal
= signal_from_string_try_harder(optarg
);
2706 if (arg_signal
< 0) {
2707 log_error("Failed to parse signal string %s.", optarg
);
2712 case ARG_NO_ASK_PASSWORD
:
2713 arg_ask_password
= false;
2717 arg_transport
= BUS_TRANSPORT_REMOTE
;
2722 arg_transport
= BUS_TRANSPORT_MACHINE
;
2727 arg_read_only
= true;
2739 arg_verify
= import_verify_from_string(optarg
);
2740 if (arg_verify
< 0) {
2741 log_error("Failed to parse --verify= setting: %s", optarg
);
2751 if (!STR_IN_SET(optarg
, "uncompressed", "xz", "gzip", "bzip2")) {
2752 log_error("Unknown format: %s", optarg
);
2756 arg_format
= optarg
;
2764 if (!env_assignment_is_valid(optarg
)) {
2765 log_error("Environment assignment invalid: %s", optarg
);
2769 r
= strv_extend(&arg_setenv
, optarg
);
2778 assert_not_reached("Unhandled option");
2787 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
2788 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
2790 optind
-= 1; /* place the option index where the "shell" verb will be placed */
2793 for (i
= shell
; i
< optind
; i
++)
2794 argv
[i
] = argv
[i
+1];
2801 static int machinectl_main(int argc
, char *argv
[], sd_bus
*bus
) {
2803 static const Verb verbs
[] = {
2804 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
2805 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_machines
},
2806 { "list-images", VERB_ANY
, 1, 0, list_images
},
2807 { "status", 2, VERB_ANY
, 0, show_machine
},
2808 { "image-status", VERB_ANY
, VERB_ANY
, 0, show_image
},
2809 { "show", VERB_ANY
, VERB_ANY
, 0, show_machine
},
2810 { "show-image", VERB_ANY
, VERB_ANY
, 0, show_image
},
2811 { "terminate", 2, VERB_ANY
, 0, terminate_machine
},
2812 { "reboot", 2, VERB_ANY
, 0, reboot_machine
},
2813 { "poweroff", 2, VERB_ANY
, 0, poweroff_machine
},
2814 { "stop", 2, VERB_ANY
, 0, poweroff_machine
}, /* Convenience alias */
2815 { "kill", 2, VERB_ANY
, 0, kill_machine
},
2816 { "login", VERB_ANY
, 2, 0, login_machine
},
2817 { "shell", VERB_ANY
, VERB_ANY
, 0, shell_machine
},
2818 { "bind", 3, 4, 0, bind_mount
},
2819 { "copy-to", 3, 4, 0, copy_files
},
2820 { "copy-from", 3, 4, 0, copy_files
},
2821 { "remove", 2, VERB_ANY
, 0, remove_image
},
2822 { "rename", 3, 3, 0, rename_image
},
2823 { "clone", 3, 3, 0, clone_image
},
2824 { "read-only", 2, 3, 0, read_only_image
},
2825 { "start", 2, VERB_ANY
, 0, start_machine
},
2826 { "enable", 2, VERB_ANY
, 0, enable_machine
},
2827 { "disable", 2, VERB_ANY
, 0, enable_machine
},
2828 { "import-tar", 2, 3, 0, import_tar
},
2829 { "import-raw", 2, 3, 0, import_raw
},
2830 { "export-tar", 2, 3, 0, export_tar
},
2831 { "export-raw", 2, 3, 0, export_raw
},
2832 { "pull-tar", 2, 3, 0, pull_tar
},
2833 { "pull-raw", 2, 3, 0, pull_raw
},
2834 { "list-transfers", VERB_ANY
, 1, 0, list_transfers
},
2835 { "cancel-transfer", 2, VERB_ANY
, 0, cancel_transfer
},
2836 { "set-limit", 2, 3, 0, set_limit
},
2837 { "clean", VERB_ANY
, 1, 0, clean_images
},
2841 return dispatch_verb(argc
, argv
, verbs
, bus
);
2844 int main(int argc
, char*argv
[]) {
2848 setlocale(LC_ALL
, "");
2849 log_parse_environment();
2852 r
= parse_argv(argc
, argv
);
2856 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
2858 log_error_errno(r
, "Failed to create bus connection: %m");
2862 sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
2864 r
= machinectl_main(argc
, argv
, bus
);
2867 sd_bus_flush_close_unref(bus
);
2869 polkit_agent_close();
2871 strv_free(arg_property
);
2872 strv_free(arg_setenv
);
2874 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;