]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine/machinectl.c
man: systemd.offline-updates: remove link to the doc it obsoletes (#7189)
[thirdparty/systemd.git] / src / machine / machinectl.c
CommitLineData
1ee306e1
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
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.
10
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.
15
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/>.
18***/
19
3f6fd1ba 20#include <arpa/inet.h>
1ee306e1 21#include <errno.h>
3f6fd1ba 22#include <fcntl.h>
1ee306e1 23#include <getopt.h>
1ee306e1 24#include <locale.h>
f48e75cb 25#include <net/if.h>
3f6fd1ba
LP
26#include <netinet/in.h>
27#include <string.h>
785890ac 28#include <sys/mount.h>
3f6fd1ba
LP
29#include <sys/socket.h>
30#include <unistd.h>
eef46c37 31
a1da8583 32#include "sd-bus.h"
3f6fd1ba 33
b5efdb8a 34#include "alloc-util.h"
68ce459f 35#include "bus-common-errors.h"
a1da8583 36#include "bus-error.h"
a0e27019 37#include "bus-unit-util.h"
3f6fd1ba 38#include "bus-util.h"
1ee306e1 39#include "cgroup-show.h"
9d127096 40#include "cgroup-util.h"
f2cbe59e 41#include "copy.h"
3f6fd1ba 42#include "env-util.h"
3ffd4af2 43#include "fd-util.h"
3f6fd1ba 44#include "hostname-util.h"
3d7415f4 45#include "import-util.h"
3f6fd1ba
LP
46#include "log.h"
47#include "logs-show.h"
48#include "macro.h"
49#include "mkdir.h"
50#include "pager.h"
6bedfcbb 51#include "parse-util.h"
3f6fd1ba 52#include "path-util.h"
0b452006 53#include "process-util.h"
3f6fd1ba 54#include "ptyfwd.h"
24882e06 55#include "signal-util.h"
3f6fd1ba
LP
56#include "spawn-polkit-agent.h"
57#include "strv.h"
58#include "terminal-util.h"
59#include "unit-name.h"
60#include "util.h"
61#include "verbs.h"
49cf4170 62#include "web-util.h"
07b0b339
SK
63#include "stdio-util.h"
64
65#define ALL_IP_ADDRESSES -1
1ee306e1
LP
66
67static char **arg_property = NULL;
68static bool arg_all = false;
85500523 69static bool arg_value = false;
1ee306e1
LP
70static bool arg_full = false;
71static bool arg_no_pager = false;
e56056e9 72static bool arg_legend = true;
1ee306e1
LP
73static const char *arg_kill_who = NULL;
74static int arg_signal = SIGTERM;
d21ed1ea 75static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
1ee306e1 76static char *arg_host = NULL;
785890ac
LP
77static bool arg_read_only = false;
78static bool arg_mkdir = false;
d8f52ed2 79static bool arg_quiet = false;
acf97e21 80static bool arg_ask_password = true;
8b0cc9a3
LP
81static unsigned arg_lines = 10;
82static OutputMode arg_output = OUTPUT_SHORT;
3d7415f4 83static bool arg_force = false;
6e18cc9f 84static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE;
587fec42 85static const char* arg_format = NULL;
c454426c
LP
86static const char *arg_uid = NULL;
87static char **arg_setenv = NULL;
07b0b339
SK
88static int arg_addrs = 1;
89
90static int print_addresses(sd_bus *bus, const char *name, int, const char *pr1, const char *pr2, int n_addr);
1ee306e1 91
acf97e21
LP
92static void polkit_agent_open_if_enabled(void) {
93
94 /* Open the polkit agent as a child process if necessary */
95
96 if (!arg_ask_password)
97 return;
98
99 if (arg_transport != BUS_TRANSPORT_LOCAL)
100 return;
101
102 polkit_agent_open();
103}
104
8b0cc9a3
LP
105static OutputFlags get_output_flags(void) {
106 return
107 arg_all * OUTPUT_SHOW_ALL |
108 arg_full * OUTPUT_FULL_WIDTH |
109 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
40c9fe4c 110 colors_enabled() * OUTPUT_COLOR |
8b0cc9a3
LP
111 !arg_quiet * OUTPUT_WARN_CUTOFF;
112}
113
0b63e278
LP
114typedef struct MachineInfo {
115 const char *name;
116 const char *class;
117 const char *service;
e3fc2b02 118 char *os;
07b0b339 119 char *version_id;
0b63e278
LP
120} MachineInfo;
121
122static int compare_machine_info(const void *a, const void *b) {
123 const MachineInfo *x = a, *y = b;
124
125 return strcmp(x->name, y->name);
126}
127
e3fc2b02
SK
128static void clean_machine_info(MachineInfo *machines, size_t n_machines) {
129 size_t i;
130
131 if (!machines || n_machines == 0)
132 return;
133
07b0b339 134 for (i = 0; i < n_machines; i++) {
e3fc2b02 135 free(machines[i].os);
07b0b339
SK
136 free(machines[i].version_id);
137 }
e3fc2b02
SK
138 free(machines);
139}
140
9153b02b 141static int call_get_os_release(sd_bus *bus, const char *method, const char *name, const char *query, ...) {
e3fc2b02 142 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
07b0b339
SK
143 const char *k, *v, *iter, **query_res = NULL;
144 size_t count = 0, awaited_args = 0;
145 va_list ap;
e3fc2b02
SK
146 int r;
147
148 assert(bus);
149 assert(name);
07b0b339
SK
150 assert(query);
151
152 NULSTR_FOREACH(iter, query)
153 awaited_args++;
154 query_res = newa0(const char *, awaited_args);
e3fc2b02 155
9153b02b
LP
156 r = sd_bus_call_method(
157 bus,
158 "org.freedesktop.machine1",
159 "/org/freedesktop/machine1",
160 "org.freedesktop.machine1.Manager",
161 method,
162 NULL, &reply, "s", name);
e3fc2b02
SK
163 if (r < 0)
164 return r;
165
166 r = sd_bus_message_enter_container(reply, 'a', "{ss}");
167 if (r < 0)
168 return bus_log_parse_error(r);
169
170 while ((r = sd_bus_message_read(reply, "{ss}", &k, &v)) > 0) {
07b0b339
SK
171 count = 0;
172 NULSTR_FOREACH(iter, query) {
173 if (streq(k, iter)) {
174 query_res[count] = v;
175 break;
176 }
177 count++;
178 }
e3fc2b02
SK
179 }
180 if (r < 0)
181 return bus_log_parse_error(r);
182
183 r = sd_bus_message_exit_container(reply);
184 if (r < 0)
185 return bus_log_parse_error(r);
186
07b0b339
SK
187 va_start(ap, query);
188 for (count = 0; count < awaited_args; count++) {
189 char *val, **out;
190
191 out = va_arg(ap, char **);
192 assert(out);
193 if (query_res[count]) {
194 val = strdup(query_res[count]);
195 if (!val) {
196 va_end(ap);
9153b02b 197 return -ENOMEM;
07b0b339
SK
198 }
199 *out = val;
200 }
201 }
202 va_end(ap);
e3fc2b02
SK
203
204 return 0;
205}
206
56159e0d
LP
207static int list_machines(int argc, char *argv[], void *userdata) {
208
e3fc2b02 209 size_t max_name = strlen("MACHINE"), max_class = strlen("CLASS"),
07b0b339 210 max_service = strlen("SERVICE"), max_os = strlen("OS"), max_version_id = strlen("VERSION");
4afd3348
LP
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;
07b0b339 213 _cleanup_free_ char *prefix = NULL;
e3fc2b02 214 MachineInfo *machines = NULL;
a1da8583 215 const char *name, *class, *service, *object;
0b63e278 216 size_t n_machines = 0, n_allocated = 0, j;
56159e0d 217 sd_bus *bus = userdata;
1ee306e1
LP
218 int r;
219
56159e0d
LP
220 assert(bus);
221
ea4b98e6 222 pager_open(arg_no_pager, false);
1ee306e1 223
e138e7d7
ZJS
224 r = sd_bus_call_method(bus,
225 "org.freedesktop.machine1",
226 "/org/freedesktop/machine1",
227 "org.freedesktop.machine1.Manager",
228 "ListMachines",
229 &error,
230 &reply,
231 NULL);
a1da8583
TG
232 if (r < 0) {
233 log_error("Could not get machines: %s", bus_error_message(&error, -r));
1ee306e1 234 return r;
1ee306e1
LP
235 }
236
3d7415f4 237 r = sd_bus_message_enter_container(reply, 'a', "(ssso)");
a1da8583 238 if (r < 0)
5b30bef8 239 return bus_log_parse_error(r);
a1da8583 240 while ((r = sd_bus_message_read(reply, "(ssso)", &name, &class, &service, &object)) > 0) {
0b63e278
LP
241 size_t l;
242
fee6d013
LP
243 if (name[0] == '.' && !arg_all)
244 continue;
245
4b241916 246 if (!GREEDY_REALLOC0(machines, n_allocated, n_machines + 1)) {
e3fc2b02
SK
247 r = log_oom();
248 goto out;
249 }
250
4b241916
LP
251 machines[n_machines].name = name;
252 machines[n_machines].class = class;
253 machines[n_machines].service = service;
254
255 (void) call_get_os_release(
9153b02b
LP
256 bus,
257 "GetMachineOSRelease",
258 name,
259 "ID\0"
260 "VERSION_ID\0",
07b0b339
SK
261 &machines[n_machines].os,
262 &machines[n_machines].version_id);
0b63e278
LP
263
264 l = strlen(name);
265 if (l > max_name)
266 max_name = l;
267
268 l = strlen(class);
269 if (l > max_class)
270 max_class = l;
271
272 l = strlen(service);
273 if (l > max_service)
274 max_service = l;
275
07b0b339 276 l = machines[n_machines].os ? strlen(machines[n_machines].os) : 1;
e3fc2b02
SK
277 if (l > max_os)
278 max_os = l;
279
07b0b339
SK
280 l = machines[n_machines].version_id ? strlen(machines[n_machines].version_id) : 1;
281 if (l > max_version_id)
282 max_version_id = l;
283
313cefa1 284 n_machines++;
1ee306e1 285 }
e3fc2b02
SK
286 if (r < 0) {
287 r = bus_log_parse_error(r);
288 goto out;
289 }
a1da8583
TG
290
291 r = sd_bus_message_exit_container(reply);
e3fc2b02
SK
292 if (r < 0) {
293 r = bus_log_parse_error(r);
294 goto out;
295 }
1ee306e1 296
0b63e278
LP
297 qsort_safe(machines, n_machines, sizeof(MachineInfo), compare_machine_info);
298
07b0b339
SK
299 /* Allocate for prefix max characters for all fields + spaces between them + strlen(",\n") */
300 r = asprintf(&prefix, "%-*s",
301 (int) (max_name +
302 max_class +
303 max_service +
304 max_os +
305 max_version_id + 5 + strlen(",\n")),
306 ",\n");
307 if (r < 0) {
308 r = log_oom();
309 goto out;
310 }
311
a912ab04 312 if (arg_legend && n_machines > 0)
07b0b339 313 printf("%-*s %-*s %-*s %-*s %-*s %s\n",
0b63e278
LP
314 (int) max_name, "MACHINE",
315 (int) max_class, "CLASS",
e3fc2b02 316 (int) max_service, "SERVICE",
07b0b339
SK
317 (int) max_os, "OS",
318 (int) max_version_id, "VERSION",
319 "ADDRESSES");
0b63e278 320
07b0b339
SK
321 for (j = 0; j < n_machines; j++) {
322 printf("%-*s %-*s %-*s %-*s %-*s ",
0b63e278
LP
323 (int) max_name, machines[j].name,
324 (int) max_class, machines[j].class,
07b0b339
SK
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));
328
329 r = print_addresses(bus, machines[j].name, 0, "", prefix, arg_addrs);
7026a775
LP
330 if (r <= 0) /* error or no addresses defined? */
331 fputs("-\n", stdout);
332 else
333 fputc('\n', stdout);
07b0b339 334 }
0b63e278 335
f9b1947f
VV
336 if (arg_legend) {
337 if (n_machines > 0)
338 printf("\n%zu machines listed.\n", n_machines);
339 else
340 printf("No machines.\n");
341 }
1ee306e1 342
3c302cdd 343 r = 0;
e3fc2b02
SK
344out:
345 clean_machine_info(machines, n_machines);
346 return r;
1ee306e1
LP
347}
348
cd61c3bf
LP
349typedef struct ImageInfo {
350 const char *name;
351 const char *type;
352 bool read_only;
10f9c755
LP
353 usec_t crtime;
354 usec_t mtime;
b6b18498 355 uint64_t size;
cd61c3bf
LP
356} ImageInfo;
357
358static int compare_image_info(const void *a, const void *b) {
359 const ImageInfo *x = a, *y = b;
360
361 return strcmp(x->name, y->name);
362}
363
56159e0d 364static int list_images(int argc, char *argv[], void *userdata) {
cd61c3bf 365
4afd3348 366 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
c19de711 367 size_t max_name = strlen("NAME"), max_type = strlen("TYPE"), max_size = strlen("USAGE"), max_crtime = strlen("CREATED"), max_mtime = strlen("MODIFIED");
4afd3348 368 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
cd61c3bf
LP
369 _cleanup_free_ ImageInfo *images = NULL;
370 size_t n_images = 0, n_allocated = 0, j;
371 const char *name, *type, *object;
56159e0d 372 sd_bus *bus = userdata;
b6b18498 373 uint64_t crtime, mtime, size;
10f9c755 374 int read_only, r;
cd61c3bf 375
56159e0d
LP
376 assert(bus);
377
ea4b98e6 378 pager_open(arg_no_pager, false);
cd61c3bf 379
e138e7d7
ZJS
380 r = sd_bus_call_method(bus,
381 "org.freedesktop.machine1",
382 "/org/freedesktop/machine1",
383 "org.freedesktop.machine1.Manager",
384 "ListImages",
385 &error,
386 &reply,
387 "");
cd61c3bf
LP
388 if (r < 0) {
389 log_error("Could not get images: %s", bus_error_message(&error, -r));
390 return r;
391 }
392
b6b18498 393 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbttto)");
cd61c3bf
LP
394 if (r < 0)
395 return bus_log_parse_error(r);
396
b6b18498
LP
397 while ((r = sd_bus_message_read(reply, "(ssbttto)", &name, &type, &read_only, &crtime, &mtime, &size, &object)) > 0) {
398 char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_BYTES_MAX)];
10f9c755 399 size_t l;
cd61c3bf
LP
400
401 if (name[0] == '.' && !arg_all)
402 continue;
403
404 if (!GREEDY_REALLOC(images, n_allocated, n_images + 1))
405 return log_oom();
406
407 images[n_images].name = name;
408 images[n_images].type = type;
409 images[n_images].read_only = read_only;
10f9c755
LP
410 images[n_images].crtime = crtime;
411 images[n_images].mtime = mtime;
b6b18498 412 images[n_images].size = size;
10f9c755
LP
413
414 l = strlen(name);
415 if (l > max_name)
416 max_name = l;
cd61c3bf 417
10f9c755
LP
418 l = strlen(type);
419 if (l > max_type)
420 max_type = l;
cd61c3bf 421
10f9c755 422 if (crtime != 0) {
b6b18498 423 l = strlen(strna(format_timestamp(buf, sizeof(buf), crtime)));
10f9c755
LP
424 if (l > max_crtime)
425 max_crtime = l;
426 }
427
428 if (mtime != 0) {
b6b18498 429 l = strlen(strna(format_timestamp(buf, sizeof(buf), mtime)));
10f9c755
LP
430 if (l > max_mtime)
431 max_mtime = l;
432 }
cd61c3bf 433
b6b18498
LP
434 if (size != (uint64_t) -1) {
435 l = strlen(strna(format_bytes(buf, sizeof(buf), size)));
436 if (l > max_size)
437 max_size = l;
438 }
439
cd61c3bf
LP
440 n_images++;
441 }
442 if (r < 0)
443 return bus_log_parse_error(r);
444
445 r = sd_bus_message_exit_container(reply);
446 if (r < 0)
447 return bus_log_parse_error(r);
448
449 qsort_safe(images, n_images, sizeof(ImageInfo), compare_image_info);
450
a912ab04 451 if (arg_legend && n_images > 0)
b6b18498 452 printf("%-*s %-*s %-3s %-*s %-*s %-*s\n",
10f9c755
LP
453 (int) max_name, "NAME",
454 (int) max_type, "TYPE",
455 "RO",
c19de711 456 (int) max_size, "USAGE",
10f9c755
LP
457 (int) max_crtime, "CREATED",
458 (int) max_mtime, "MODIFIED");
cd61c3bf
LP
459
460 for (j = 0; j < n_images; j++) {
b6b18498 461 char crtime_buf[FORMAT_TIMESTAMP_MAX], mtime_buf[FORMAT_TIMESTAMP_MAX], size_buf[FORMAT_BYTES_MAX];
10f9c755 462
b6b18498 463 printf("%-*s %-*s %s%-3s%s %-*s %-*s %-*s\n",
cd61c3bf
LP
464 (int) max_name, images[j].name,
465 (int) max_type, images[j].type,
1fc464f6 466 images[j].read_only ? ansi_highlight_red() : "", yes_no(images[j].read_only), images[j].read_only ? ansi_normal() : "",
b6b18498
LP
467 (int) max_size, strna(format_bytes(size_buf, sizeof(size_buf), images[j].size)),
468 (int) max_crtime, strna(format_timestamp(crtime_buf, sizeof(crtime_buf), images[j].crtime)),
469 (int) max_mtime, strna(format_timestamp(mtime_buf, sizeof(mtime_buf), images[j].mtime)));
cd61c3bf
LP
470 }
471
f9b1947f
VV
472 if (arg_legend) {
473 if (n_images > 0)
474 printf("\n%zu images listed.\n", n_images);
475 else
476 printf("No images.\n");
477 }
cd61c3bf
LP
478
479 return 0;
480}
481
89f7c846 482static int show_unit_cgroup(sd_bus *bus, const char *unit, pid_t leader) {
bc06be75 483 _cleanup_free_ char *cgroup = NULL;
4afd3348 484 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
8b0cc9a3 485 int r;
aa1936ea
LP
486 unsigned c;
487
488 assert(bus);
489 assert(unit);
490
bc06be75 491 r = show_cgroup_get_unit_path_and_warn(bus, unit, &cgroup);
a0e27019 492 if (r < 0)
bc06be75 493 return r;
aa1936ea 494
a0e27019 495 if (isempty(cgroup))
9d127096
LP
496 return 0;
497
aa1936ea
LP
498 c = columns();
499 if (c > 18)
500 c -= 18;
501 else
502 c = 0;
503
a0e27019
LP
504 r = unit_show_processes(bus, unit, cgroup, "\t\t ", c, get_output_flags(), &error);
505 if (r == -EBADR) {
506
507 if (arg_transport == BUS_TRANSPORT_REMOTE)
508 return 0;
509
510 /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */
511
512 if (cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, cgroup) != 0 && leader <= 0)
513 return 0;
514
0ff308c8 515 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, cgroup, "\t\t ", c, &leader, leader > 0, get_output_flags());
a0e27019
LP
516 } else if (r < 0)
517 return log_error_errno(r, "Failed to dump process list: %s", bus_error_message(&error, r));
518
aa1936ea
LP
519 return 0;
520}
521
07b0b339 522static int print_addresses(sd_bus *bus, const char *name, int ifi, const char *prefix, const char *prefix2, int n_addr) {
4afd3348 523 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
07b0b339
SK
524 _cleanup_free_ char *addresses = NULL;
525 bool truncate = false;
7026a775 526 unsigned n = 0;
878cd7e9
LP
527 int r;
528
529 assert(bus);
530 assert(name);
531 assert(prefix);
532 assert(prefix2);
533
534 r = sd_bus_call_method(bus,
535 "org.freedesktop.machine1",
536 "/org/freedesktop/machine1",
537 "org.freedesktop.machine1.Manager",
538 "GetMachineAddresses",
539 NULL,
540 &reply,
541 "s", name);
542 if (r < 0)
543 return r;
544
07b0b339
SK
545 addresses = strdup(prefix);
546 if (!addresses)
547 return log_oom();
548 prefix = "";
549
0dd25fb9 550 r = sd_bus_message_enter_container(reply, 'a', "(iay)");
878cd7e9
LP
551 if (r < 0)
552 return bus_log_parse_error(r);
553
0dd25fb9
LP
554 while ((r = sd_bus_message_enter_container(reply, 'r', "iay")) > 0) {
555 int family;
878cd7e9
LP
556 const void *a;
557 size_t sz;
07b0b339 558 char buf_ifi[DECIMAL_STR_MAX(int) + 2], buffer[MAX(INET6_ADDRSTRLEN, INET_ADDRSTRLEN)];
878cd7e9 559
0dd25fb9 560 r = sd_bus_message_read(reply, "i", &family);
878cd7e9
LP
561 if (r < 0)
562 return bus_log_parse_error(r);
563
564 r = sd_bus_message_read_array(reply, 'y', &a, &sz);
565 if (r < 0)
566 return bus_log_parse_error(r);
567
07b0b339
SK
568 if (n_addr != 0) {
569 if (family == AF_INET6 && ifi > 0)
570 xsprintf(buf_ifi, "%%%i", ifi);
571 else
572 strcpy(buf_ifi, "");
573
7026a775 574 if (!strextend(&addresses, prefix, inet_ntop(family, a, buffer, sizeof(buffer)), buf_ifi, NULL))
07b0b339
SK
575 return log_oom();
576 } else
577 truncate = true;
878cd7e9
LP
578
579 r = sd_bus_message_exit_container(reply);
580 if (r < 0)
581 return bus_log_parse_error(r);
582
583 if (prefix != prefix2)
584 prefix = prefix2;
07b0b339
SK
585
586 if (n_addr > 0)
587 n_addr -= 1;
7026a775
LP
588
589 n++;
878cd7e9
LP
590 }
591 if (r < 0)
592 return bus_log_parse_error(r);
593
594 r = sd_bus_message_exit_container(reply);
595 if (r < 0)
596 return bus_log_parse_error(r);
597
7026a775
LP
598 if (n > 0)
599 fprintf(stdout, "%s%s", addresses, truncate ? "..." : "");
600
601 return (int) n;
878cd7e9
LP
602}
603
9153b02b 604static int print_os_release(sd_bus *bus, const char *method, const char *name, const char *prefix) {
e3fc2b02 605 _cleanup_free_ char *pretty = NULL;
717603e3
LP
606 int r;
607
608 assert(bus);
609 assert(name);
610 assert(prefix);
611
9153b02b 612 r = call_get_os_release(bus, method, name, "PRETTY_NAME\0", &pretty, NULL);
717603e3
LP
613 if (r < 0)
614 return r;
615
717603e3
LP
616 if (pretty)
617 printf("%s%s\n", prefix, pretty);
618
619 return 0;
620}
621
3401419b
LP
622static int print_uid_shift(sd_bus *bus, const char *name) {
623 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
624 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
625 uint32_t shift;
626 int r;
627
628 assert(bus);
629 assert(name);
630
631 r = sd_bus_call_method(bus,
632 "org.freedesktop.machine1",
633 "/org/freedesktop/machine1",
634 "org.freedesktop.machine1.Manager",
635 "GetMachineUIDShift",
636 &error,
637 &reply,
638 "s", name);
639 if (r < 0)
640 return log_debug_errno(r, "Failed to query UID/GID shift: %s", bus_error_message(&error, r));
641
642 r = sd_bus_message_read(reply, "u", &shift);
643 if (r < 0)
644 return r;
645
646 if (shift == 0) /* Don't show trivial mappings */
647 return 0;
648
649 printf(" UID Shift: %" PRIu32 "\n", shift);
650 return 0;
651}
652
1ee306e1 653typedef struct MachineStatusInfo {
9f6eb1cd 654 char *name;
1ee306e1 655 sd_id128_t id;
9f6eb1cd
KS
656 char *class;
657 char *service;
89f7c846 658 char *unit;
9f6eb1cd 659 char *root_directory;
1ee306e1 660 pid_t leader;
8b0cc9a3 661 struct dual_timestamp timestamp;
f48e75cb
LP
662 int *netif;
663 unsigned n_netif;
1ee306e1
LP
664} MachineStatusInfo;
665
e7e55dbd
DH
666static void machine_status_info_clear(MachineStatusInfo *info) {
667 if (info) {
668 free(info->name);
669 free(info->class);
670 free(info->service);
671 free(info->unit);
672 free(info->root_directory);
673 free(info->netif);
674 zero(*info);
675 }
676}
677
a1da8583 678static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
1ee306e1
LP
679 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
680 char since2[FORMAT_TIMESTAMP_MAX], *s2;
f48e75cb
LP
681 int ifi = -1;
682
56159e0d 683 assert(bus);
1ee306e1
LP
684 assert(i);
685
686 fputs(strna(i->name), stdout);
687
3bbaff3e 688 if (!sd_id128_is_null(i->id))
1ee306e1
LP
689 printf("(" SD_ID128_FORMAT_STR ")\n", SD_ID128_FORMAT_VAL(i->id));
690 else
691 putchar('\n');
692
8b0cc9a3
LP
693 s1 = format_timestamp_relative(since1, sizeof(since1), i->timestamp.realtime);
694 s2 = format_timestamp(since2, sizeof(since2), i->timestamp.realtime);
1ee306e1
LP
695
696 if (s1)
697 printf("\t Since: %s; %s\n", s2, s1);
698 else if (s2)
699 printf("\t Since: %s\n", s2);
700
701 if (i->leader > 0) {
702 _cleanup_free_ char *t = NULL;
703
704 printf("\t Leader: %u", (unsigned) i->leader);
705
706 get_process_comm(i->leader, &t);
707 if (t)
708 printf(" (%s)", t);
709
710 putchar('\n');
711 }
712
713 if (i->service) {
714 printf("\t Service: %s", i->service);
715
716 if (i->class)
717 printf("; class %s", i->class);
718
719 putchar('\n');
720 } else if (i->class)
721 printf("\t Class: %s\n", i->class);
722
1ee306e1
LP
723 if (i->root_directory)
724 printf("\t Root: %s\n", i->root_directory);
725
f48e75cb
LP
726 if (i->n_netif > 0) {
727 unsigned c;
728
729 fputs("\t Iface:", stdout);
730
731 for (c = 0; c < i->n_netif; c++) {
732 char name[IF_NAMESIZE+1] = "";
733
734 if (if_indextoname(i->netif[c], name)) {
735 fputc(' ', stdout);
736 fputs(name, stdout);
737
738 if (ifi < 0)
739 ifi = i->netif[c];
740 else
741 ifi = 0;
742 } else
743 printf(" %i", i->netif[c]);
744 }
745
746 fputc('\n', stdout);
747 }
748
7026a775
LP
749 if (print_addresses(bus, i->name, ifi,
750 "\t Address: ",
751 "\n\t ",
752 ALL_IP_ADDRESSES) > 0)
753 fputc('\n', stdout);
878cd7e9 754
9153b02b 755 print_os_release(bus, "GetMachineOSRelease", i->name, "\t OS: ");
717603e3 756
3401419b
LP
757 print_uid_shift(bus, i->name);
758
89f7c846
LP
759 if (i->unit) {
760 printf("\t Unit: %s\n", i->unit);
761 show_unit_cgroup(bus, i->unit, i->leader);
8b0cc9a3 762
ece174c5 763 if (arg_transport == BUS_TRANSPORT_LOCAL)
8b0cc9a3
LP
764
765 show_journal_by_unit(
766 stdout,
767 i->unit,
768 arg_output,
769 0,
770 i->timestamp.monotonic,
771 arg_lines,
772 0,
773 get_output_flags() | OUTPUT_BEGIN_NEWLINE,
774 SD_JOURNAL_LOCAL_ONLY,
775 true,
776 NULL);
1ee306e1
LP
777 }
778}
779
f48e75cb
LP
780static int map_netif(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
781 MachineStatusInfo *i = userdata;
782 size_t l;
783 const void *v;
784 int r;
785
786 assert_cc(sizeof(int32_t) == sizeof(int));
787 r = sd_bus_message_read_array(m, SD_BUS_TYPE_INT32, &v, &l);
788 if (r < 0)
789 return r;
e7e9b6bb
ZJS
790 if (r == 0)
791 return -EBADMSG;
f48e75cb
LP
792
793 i->n_netif = l / sizeof(int32_t);
794 i->netif = memdup(v, l);
795 if (!i->netif)
796 return -ENOMEM;
797
798 return 0;
799}
800
fefdc04b 801static int show_machine_info(const char *verb, sd_bus *bus, const char *path, bool *new_line) {
a6c61602 802
9f6eb1cd 803 static const struct bus_properties_map map[] = {
8b0cc9a3
LP
804 { "Name", "s", NULL, offsetof(MachineStatusInfo, name) },
805 { "Class", "s", NULL, offsetof(MachineStatusInfo, class) },
806 { "Service", "s", NULL, offsetof(MachineStatusInfo, service) },
807 { "Unit", "s", NULL, offsetof(MachineStatusInfo, unit) },
808 { "RootDirectory", "s", NULL, offsetof(MachineStatusInfo, root_directory) },
809 { "Leader", "u", NULL, offsetof(MachineStatusInfo, leader) },
810 { "Timestamp", "t", NULL, offsetof(MachineStatusInfo, timestamp.realtime) },
811 { "TimestampMonotonic", "t", NULL, offsetof(MachineStatusInfo, timestamp.monotonic) },
812 { "Id", "ay", bus_map_id128, offsetof(MachineStatusInfo, id) },
813 { "NetworkInterfaces", "ai", map_netif, 0 },
9f6eb1cd
KS
814 {}
815 };
a6c61602 816
f9e0eefc 817 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
e7e55dbd 818 _cleanup_(machine_status_info_clear) MachineStatusInfo info = {};
a1da8583
TG
819 int r;
820
56159e0d
LP
821 assert(verb);
822 assert(bus);
1ee306e1
LP
823 assert(path);
824 assert(new_line);
825
9f6eb1cd
KS
826 r = bus_map_all_properties(bus,
827 "org.freedesktop.machine1",
828 path,
829 map,
f9e0eefc 830 &error,
9f6eb1cd 831 &info);
f647962d 832 if (r < 0)
f9e0eefc 833 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
1ee306e1 834
1ee306e1
LP
835 if (*new_line)
836 printf("\n");
1ee306e1
LP
837 *new_line = true;
838
9f6eb1cd 839 print_machine_status_info(bus, &info);
1ee306e1 840
9f6eb1cd
KS
841 return r;
842}
1ee306e1 843
fefdc04b 844static int show_machine_properties(sd_bus *bus, const char *path, bool *new_line) {
9f6eb1cd 845 int r;
1ee306e1 846
56159e0d
LP
847 assert(bus);
848 assert(path);
849 assert(new_line);
850
9f6eb1cd
KS
851 if (*new_line)
852 printf("\n");
1ee306e1 853
9f6eb1cd 854 *new_line = true;
a1da8583 855
85500523 856 r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_value, arg_all);
a1da8583 857 if (r < 0)
da927ba9 858 log_error_errno(r, "Could not get properties: %m");
1ee306e1 859
a7893c6b 860 return r;
1ee306e1
LP
861}
862
fefdc04b 863static int show_machine(int argc, char *argv[], void *userdata) {
56159e0d 864
4afd3348
LP
865 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
866 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
9f6eb1cd 867 bool properties, new_line = false;
56159e0d
LP
868 sd_bus *bus = userdata;
869 int r = 0, i;
1ee306e1
LP
870
871 assert(bus);
1ee306e1 872
56159e0d 873 properties = !strstr(argv[0], "status");
1ee306e1 874
ea4b98e6 875 pager_open(arg_no_pager, false);
1ee306e1 876
56159e0d 877 if (properties && argc <= 1) {
a1da8583 878
9f6eb1cd 879 /* If no argument is specified, inspect the manager
1ee306e1 880 * itself */
fefdc04b 881 r = show_machine_properties(bus, "/org/freedesktop/machine1", &new_line);
8c841f21 882 if (r < 0)
9f6eb1cd 883 return r;
1ee306e1
LP
884 }
885
56159e0d 886 for (i = 1; i < argc; i++) {
1ee306e1
LP
887 const char *path = NULL;
888
e138e7d7
ZJS
889 r = sd_bus_call_method(bus,
890 "org.freedesktop.machine1",
891 "/org/freedesktop/machine1",
892 "org.freedesktop.machine1.Manager",
893 "GetMachine",
894 &error,
895 &reply,
896 "s", argv[i]);
a1da8583
TG
897 if (r < 0) {
898 log_error("Could not get path to machine: %s", bus_error_message(&error, -r));
899 return r;
900 }
901
902 r = sd_bus_message_read(reply, "o", &path);
5b30bef8
LP
903 if (r < 0)
904 return bus_log_parse_error(r);
1ee306e1 905
9f6eb1cd 906 if (properties)
fefdc04b 907 r = show_machine_properties(bus, path, &new_line);
9f6eb1cd 908 else
fefdc04b
LP
909 r = show_machine_info(argv[0], bus, path, &new_line);
910 }
911
912 return r;
913}
914
915typedef struct ImageStatusInfo {
916 char *name;
917 char *path;
918 char *type;
919 int read_only;
920 usec_t crtime;
921 usec_t mtime;
c19de711 922 uint64_t usage;
b6b18498 923 uint64_t limit;
c19de711 924 uint64_t usage_exclusive;
b6b18498 925 uint64_t limit_exclusive;
fefdc04b
LP
926} ImageStatusInfo;
927
e7e55dbd
DH
928static void image_status_info_clear(ImageStatusInfo *info) {
929 if (info) {
930 free(info->name);
931 free(info->path);
932 free(info->type);
933 zero(*info);
934 }
935}
936
fefdc04b
LP
937static void print_image_status_info(sd_bus *bus, ImageStatusInfo *i) {
938 char ts_relative[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
939 char ts_absolute[FORMAT_TIMESTAMP_MAX], *s2;
b6b18498
LP
940 char bs[FORMAT_BYTES_MAX], *s3;
941 char bs_exclusive[FORMAT_BYTES_MAX], *s4;
fefdc04b
LP
942
943 assert(bus);
944 assert(i);
945
946 if (i->name) {
947 fputs(i->name, stdout);
948 putchar('\n');
949 }
950
9a14fb62 951 if (i->type)
fefdc04b
LP
952 printf("\t Type: %s\n", i->type);
953
954 if (i->path)
955 printf("\t Path: %s\n", i->path);
956
9153b02b
LP
957 print_os_release(bus, "GetImageOSRelease", i->name, "\t OS: ");
958
fefdc04b
LP
959 printf("\t RO: %s%s%s\n",
960 i->read_only ? ansi_highlight_red() : "",
961 i->read_only ? "read-only" : "writable",
1fc464f6 962 i->read_only ? ansi_normal() : "");
fefdc04b
LP
963
964 s1 = format_timestamp_relative(ts_relative, sizeof(ts_relative), i->crtime);
965 s2 = format_timestamp(ts_absolute, sizeof(ts_absolute), i->crtime);
b6b18498 966 if (s1 && s2)
fefdc04b
LP
967 printf("\t Created: %s; %s\n", s2, s1);
968 else if (s2)
969 printf("\t Created: %s\n", s2);
970
971 s1 = format_timestamp_relative(ts_relative, sizeof(ts_relative), i->mtime);
972 s2 = format_timestamp(ts_absolute, sizeof(ts_absolute), i->mtime);
b6b18498 973 if (s1 && s2)
fefdc04b
LP
974 printf("\tModified: %s; %s\n", s2, s1);
975 else if (s2)
976 printf("\tModified: %s\n", s2);
b6b18498 977
c19de711
LP
978 s3 = format_bytes(bs, sizeof(bs), i->usage);
979 s4 = i->usage_exclusive != i->usage ? format_bytes(bs_exclusive, sizeof(bs_exclusive), i->usage_exclusive) : NULL;
b6b18498 980 if (s3 && s4)
c19de711 981 printf("\t Usage: %s (exclusive: %s)\n", s3, s4);
b6b18498 982 else if (s3)
c19de711 983 printf("\t Usage: %s\n", s3);
b6b18498
LP
984
985 s3 = format_bytes(bs, sizeof(bs), i->limit);
986 s4 = i->limit_exclusive != i->limit ? format_bytes(bs_exclusive, sizeof(bs_exclusive), i->limit_exclusive) : NULL;
987 if (s3 && s4)
988 printf("\t Limit: %s (exclusive: %s)\n", s3, s4);
989 else if (s3)
990 printf("\t Limit: %s\n", s3);
fefdc04b
LP
991}
992
160e3793 993static int show_image_info(sd_bus *bus, const char *path, bool *new_line) {
fefdc04b
LP
994
995 static const struct bus_properties_map map[] = {
b6b18498
LP
996 { "Name", "s", NULL, offsetof(ImageStatusInfo, name) },
997 { "Path", "s", NULL, offsetof(ImageStatusInfo, path) },
998 { "Type", "s", NULL, offsetof(ImageStatusInfo, type) },
999 { "ReadOnly", "b", NULL, offsetof(ImageStatusInfo, read_only) },
1000 { "CreationTimestamp", "t", NULL, offsetof(ImageStatusInfo, crtime) },
1001 { "ModificationTimestamp", "t", NULL, offsetof(ImageStatusInfo, mtime) },
c19de711 1002 { "Usage", "t", NULL, offsetof(ImageStatusInfo, usage) },
b6b18498 1003 { "Limit", "t", NULL, offsetof(ImageStatusInfo, limit) },
c19de711 1004 { "UsageExclusive", "t", NULL, offsetof(ImageStatusInfo, usage_exclusive) },
b6b18498 1005 { "LimitExclusive", "t", NULL, offsetof(ImageStatusInfo, limit_exclusive) },
fefdc04b
LP
1006 {}
1007 };
1008
f9e0eefc 1009 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
e7e55dbd 1010 _cleanup_(image_status_info_clear) ImageStatusInfo info = {};
fefdc04b
LP
1011 int r;
1012
fefdc04b
LP
1013 assert(bus);
1014 assert(path);
1015 assert(new_line);
1016
1017 r = bus_map_all_properties(bus,
1018 "org.freedesktop.machine1",
1019 path,
1020 map,
f9e0eefc 1021 &error,
fefdc04b
LP
1022 &info);
1023 if (r < 0)
f9e0eefc 1024 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
fefdc04b
LP
1025
1026 if (*new_line)
1027 printf("\n");
1028 *new_line = true;
1029
1030 print_image_status_info(bus, &info);
1031
fefdc04b
LP
1032 return r;
1033}
1034
160e3793
LP
1035typedef struct PoolStatusInfo {
1036 char *path;
1037 uint64_t usage;
1038 uint64_t limit;
1039} PoolStatusInfo;
1040
e7e55dbd
DH
1041static void pool_status_info_clear(PoolStatusInfo *info) {
1042 if (info) {
1043 free(info->path);
1044 zero(*info);
1045 info->usage = -1;
1046 info->limit = -1;
1047 }
1048}
1049
160e3793
LP
1050static void print_pool_status_info(sd_bus *bus, PoolStatusInfo *i) {
1051 char bs[FORMAT_BYTES_MAX], *s;
1052
1053 if (i->path)
1054 printf("\t Path: %s\n", i->path);
1055
1056 s = format_bytes(bs, sizeof(bs), i->usage);
1057 if (s)
1058 printf("\t Usage: %s\n", s);
1059
1060 s = format_bytes(bs, sizeof(bs), i->limit);
1061 if (s)
1062 printf("\t Limit: %s\n", s);
1063}
1064
1065static int show_pool_info(sd_bus *bus) {
1066
1067 static const struct bus_properties_map map[] = {
1068 { "PoolPath", "s", NULL, offsetof(PoolStatusInfo, path) },
1069 { "PoolUsage", "t", NULL, offsetof(PoolStatusInfo, usage) },
1070 { "PoolLimit", "t", NULL, offsetof(PoolStatusInfo, limit) },
1071 {}
1072 };
1073
e7e55dbd 1074 _cleanup_(pool_status_info_clear) PoolStatusInfo info = {
160e3793
LP
1075 .usage = (uint64_t) -1,
1076 .limit = (uint64_t) -1,
1077 };
f9e0eefc
LP
1078
1079 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
160e3793
LP
1080 int r;
1081
1082 assert(bus);
1083
1084 r = bus_map_all_properties(bus,
1085 "org.freedesktop.machine1",
1086 "/org/freedesktop/machine1",
1087 map,
f9e0eefc 1088 &error,
160e3793
LP
1089 &info);
1090 if (r < 0)
f9e0eefc 1091 return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
160e3793
LP
1092
1093 print_pool_status_info(bus, &info);
1094
160e3793
LP
1095 return 0;
1096}
1097
1098
fefdc04b
LP
1099static int show_image_properties(sd_bus *bus, const char *path, bool *new_line) {
1100 int r;
1101
1102 assert(bus);
1103 assert(path);
1104 assert(new_line);
1105
1106 if (*new_line)
1107 printf("\n");
1108
1109 *new_line = true;
1110
85500523 1111 r = bus_print_all_properties(bus, "org.freedesktop.machine1", path, arg_property, arg_value, arg_all);
fefdc04b
LP
1112 if (r < 0)
1113 log_error_errno(r, "Could not get properties: %m");
1114
1115 return r;
1116}
1117
1118static int show_image(int argc, char *argv[], void *userdata) {
1119
4afd3348
LP
1120 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1121 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
fefdc04b
LP
1122 bool properties, new_line = false;
1123 sd_bus *bus = userdata;
1124 int r = 0, i;
1125
1126 assert(bus);
1127
1128 properties = !strstr(argv[0], "status");
1129
ea4b98e6 1130 pager_open(arg_no_pager, false);
fefdc04b 1131
160e3793 1132 if (argc <= 1) {
fefdc04b
LP
1133
1134 /* If no argument is specified, inspect the manager
1135 * itself */
160e3793
LP
1136
1137 if (properties)
1138 r = show_image_properties(bus, "/org/freedesktop/machine1", &new_line);
1139 else
1140 r = show_pool_info(bus);
fefdc04b
LP
1141 if (r < 0)
1142 return r;
1143 }
1144
1145 for (i = 1; i < argc; i++) {
1146 const char *path = NULL;
1147
1148 r = sd_bus_call_method(
90adaa25
LP
1149 bus,
1150 "org.freedesktop.machine1",
1151 "/org/freedesktop/machine1",
1152 "org.freedesktop.machine1.Manager",
1153 "GetImage",
1154 &error,
1155 &reply,
1156 "s", argv[i]);
fefdc04b
LP
1157 if (r < 0) {
1158 log_error("Could not get path to image: %s", bus_error_message(&error, -r));
1159 return r;
1160 }
1161
1162 r = sd_bus_message_read(reply, "o", &path);
1163 if (r < 0)
1164 return bus_log_parse_error(r);
1165
1166 if (properties)
1167 r = show_image_properties(bus, path, &new_line);
1168 else
160e3793 1169 r = show_image_info(bus, path, &new_line);
1ee306e1
LP
1170 }
1171
9f6eb1cd 1172 return r;
1ee306e1
LP
1173}
1174
56159e0d 1175static int kill_machine(int argc, char *argv[], void *userdata) {
4afd3348 1176 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
56159e0d 1177 sd_bus *bus = userdata;
90adaa25 1178 int r, i;
1ee306e1 1179
56159e0d 1180 assert(bus);
1ee306e1 1181
acf97e21
LP
1182 polkit_agent_open_if_enabled();
1183
1ee306e1
LP
1184 if (!arg_kill_who)
1185 arg_kill_who = "all";
1186
56159e0d 1187 for (i = 1; i < argc; i++) {
a1da8583 1188 r = sd_bus_call_method(
56159e0d
LP
1189 bus,
1190 "org.freedesktop.machine1",
1191 "/org/freedesktop/machine1",
1192 "org.freedesktop.machine1.Manager",
1193 "KillMachine",
1194 &error,
1195 NULL,
1196 "ssi", argv[i], arg_kill_who, arg_signal);
a1da8583
TG
1197 if (r < 0) {
1198 log_error("Could not kill machine: %s", bus_error_message(&error, -r));
1ee306e1 1199 return r;
a1da8583 1200 }
1ee306e1
LP
1201 }
1202
1203 return 0;
1204}
1205
56159e0d 1206static int reboot_machine(int argc, char *argv[], void *userdata) {
1dba654b
LP
1207 arg_kill_who = "leader";
1208 arg_signal = SIGINT; /* sysvinit + systemd */
1ee306e1 1209
56159e0d 1210 return kill_machine(argc, argv, userdata);
1dba654b 1211}
1ee306e1 1212
56159e0d 1213static int poweroff_machine(int argc, char *argv[], void *userdata) {
1dba654b
LP
1214 arg_kill_who = "leader";
1215 arg_signal = SIGRTMIN+4; /* only systemd */
1ee306e1 1216
56159e0d 1217 return kill_machine(argc, argv, userdata);
1ee306e1
LP
1218}
1219
56159e0d 1220static int terminate_machine(int argc, char *argv[], void *userdata) {
4afd3348 1221 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
56159e0d 1222 sd_bus *bus = userdata;
2723b3b5 1223 int r, i;
923d8fd3 1224
56159e0d 1225 assert(bus);
923d8fd3 1226
acf97e21
LP
1227 polkit_agent_open_if_enabled();
1228
56159e0d 1229 for (i = 1; i < argc; i++) {
923d8fd3
LP
1230 r = sd_bus_call_method(
1231 bus,
1232 "org.freedesktop.machine1",
1233 "/org/freedesktop/machine1",
1234 "org.freedesktop.machine1.Manager",
1dba654b 1235 "TerminateMachine",
923d8fd3 1236 &error,
1dba654b 1237 NULL,
56159e0d 1238 "s", argv[i]);
923d8fd3 1239 if (r < 0) {
1dba654b 1240 log_error("Could not terminate machine: %s", bus_error_message(&error, -r));
923d8fd3
LP
1241 return r;
1242 }
923d8fd3
LP
1243 }
1244
1245 return 0;
1246}
1247
0370612e 1248static int copy_files(int argc, char *argv[], void *userdata) {
4afd3348 1249 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3d87174d 1250 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1fe6fa16
RM
1251 _cleanup_free_ char *abs_host_path = NULL;
1252 char *dest, *host_path, *container_path;
0370612e
LP
1253 sd_bus *bus = userdata;
1254 bool copy_from;
785890ac
LP
1255 int r;
1256
1257 assert(bus);
0370612e 1258
2723b3b5
LP
1259 polkit_agent_open_if_enabled();
1260
0370612e 1261 copy_from = streq(argv[0], "copy-from");
1fe6fa16
RM
1262 dest = argv[3] ?: argv[2];
1263 host_path = copy_from ? dest : argv[2];
1264 container_path = copy_from ? argv[2] : dest;
1265
1266 if (!path_is_absolute(host_path)) {
0f474365
LP
1267 r = path_make_absolute_cwd(host_path, &abs_host_path);
1268 if (r < 0)
1269 return log_error_errno(r, "Failed to make path absolute: %m");
1270
1fe6fa16
RM
1271 host_path = abs_host_path;
1272 }
785890ac 1273
3d87174d 1274 r = sd_bus_message_new_method_call(
785890ac 1275 bus,
3d87174d 1276 &m,
785890ac
LP
1277 "org.freedesktop.machine1",
1278 "/org/freedesktop/machine1",
1279 "org.freedesktop.machine1.Manager",
3d87174d
LP
1280 copy_from ? "CopyFromMachine" : "CopyToMachine");
1281 if (r < 0)
1282 return bus_log_create_error(r);
1283
1284 r = sd_bus_message_append(
1285 m,
0370612e
LP
1286 "sss",
1287 argv[1],
1fe6fa16
RM
1288 copy_from ? container_path : host_path,
1289 copy_from ? host_path : container_path);
3d87174d
LP
1290 if (r < 0)
1291 return bus_log_create_error(r);
1292
1293 /* This is a slow operation, hence turn off any method call timeouts */
1294 r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL);
0f474365
LP
1295 if (r < 0)
1296 return log_error_errno(r, "Failed to copy: %s", bus_error_message(&error, r));
f2cbe59e
LP
1297
1298 return 0;
1299}
1300
56159e0d 1301static int bind_mount(int argc, char *argv[], void *userdata) {
4afd3348 1302 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
56159e0d 1303 sd_bus *bus = userdata;
785890ac
LP
1304 int r;
1305
56159e0d
LP
1306 assert(bus);
1307
2723b3b5
LP
1308 polkit_agent_open_if_enabled();
1309
90adaa25
LP
1310 r = sd_bus_call_method(
1311 bus,
1312 "org.freedesktop.machine1",
1313 "/org/freedesktop/machine1",
1314 "org.freedesktop.machine1.Manager",
1315 "BindMountMachine",
1316 &error,
1317 NULL,
1318 "sssbb",
1319 argv[1],
1320 argv[2],
1321 argv[3],
1322 arg_read_only,
1323 arg_mkdir);
785890ac 1324 if (r < 0) {
90adaa25
LP
1325 log_error("Failed to bind mount: %s", bus_error_message(&error, -r));
1326 return r;
785890ac
LP
1327 }
1328
90adaa25 1329 return 0;
785890ac
LP
1330}
1331
19070062 1332static int on_machine_removed(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
0ec5543c
LP
1333 PTYForward ** forward = (PTYForward**) userdata;
1334 int r;
1335
0ec5543c
LP
1336 assert(m);
1337 assert(forward);
1338
1339 if (*forward) {
1340 /* If the forwarder is already initialized, tell it to
da054c37
LP
1341 * exit on the next vhangup(), so that we still flush
1342 * out what might be queued and exit then. */
0ec5543c 1343
da054c37 1344 r = pty_forward_set_ignore_vhangup(*forward, false);
0ec5543c
LP
1345 if (r >= 0)
1346 return 0;
1347
da054c37 1348 log_error_errno(r, "Failed to set ignore_vhangup flag: %m");
0ec5543c
LP
1349 }
1350
1351 /* On error, or when the forwarder is not initialized yet, quit immediately */
19070062 1352 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), EXIT_FAILURE);
0ec5543c
LP
1353 return 0;
1354}
1355
ae3dde80 1356static int process_forward(sd_event *event, PTYForward **forward, int master, PTYForwardFlags flags, const char *name) {
c454426c
LP
1357 char last_char = 0;
1358 bool machine_died;
1359 int ret = 0, r;
1360
1361 assert(event);
1362 assert(master >= 0);
1363 assert(name);
1364
1365 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
1366
fbdec792
WS
1367 if (!arg_quiet) {
1368 if (streq(name, ".host"))
1369 log_info("Connected to the local host. Press ^] three times within 1s to exit session.");
1370 else
1371 log_info("Connected to machine %s. Press ^] three times within 1s to exit session.", name);
1372 }
c454426c
LP
1373
1374 sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
1375 sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
1376
ae3dde80 1377 r = pty_forward_new(event, master, flags, forward);
c454426c
LP
1378 if (r < 0)
1379 return log_error_errno(r, "Failed to create PTY forwarder: %m");
1380
1381 r = sd_event_loop(event);
1382 if (r < 0)
1383 return log_error_errno(r, "Failed to run event loop: %m");
1384
1385 pty_forward_get_last_char(*forward, &last_char);
1386
1387 machine_died =
ae3dde80 1388 (flags & PTY_FORWARD_IGNORE_VHANGUP) &&
c454426c
LP
1389 pty_forward_get_ignore_vhangup(*forward) == 0;
1390
1391 *forward = pty_forward_free(*forward);
1392
1393 if (last_char != '\n')
1394 fputc('\n', stdout);
1395
fbdec792
WS
1396 if (!arg_quiet) {
1397 if (machine_died)
1398 log_info("Machine %s terminated.", name);
1399 else if (streq(name, ".host"))
1400 log_info("Connection to the local host terminated.");
1401 else
1402 log_info("Connection to machine %s terminated.", name);
1403 }
c454426c
LP
1404
1405 sd_event_get_exit_code(event, &ret);
1406 return ret;
1407}
1408
bc3bb330
ZJS
1409static int parse_machine_uid(const char *spec, const char **machine, char **uid) {
1410 /*
1411 * Whatever is specified in the spec takes priority over global arguments.
1412 */
1413 char *_uid = NULL;
1414 const char *_machine = NULL;
1415
1416 if (spec) {
1417 const char *at;
1418
1419 at = strchr(spec, '@');
1420 if (at) {
1421 if (at == spec)
1422 /* Do the same as ssh and refuse "@host". */
1423 return -EINVAL;
1424
1425 _machine = at + 1;
1426 _uid = strndup(spec, at - spec);
1427 if (!_uid)
1428 return -ENOMEM;
1429 } else
1430 _machine = spec;
1431 };
1432
1433 if (arg_uid && !_uid) {
1434 _uid = strdup(arg_uid);
1435 if (!_uid)
1436 return -ENOMEM;
1437 }
1438
1439 *uid = _uid;
1440 *machine = isempty(_machine) ? ".host" : _machine;
1441 return 0;
1442}
1443
56159e0d 1444static int login_machine(int argc, char *argv[], void *userdata) {
4afd3348
LP
1445 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1446 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
023fb90b 1447 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
4afd3348
LP
1448 _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL;
1449 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
c454426c 1450 int master = -1, r;
56159e0d 1451 sd_bus *bus = userdata;
91913f58 1452 const char *pty, *match, *machine;
04d39279
LP
1453
1454 assert(bus);
04d39279 1455
c454426c
LP
1456 if (!strv_isempty(arg_setenv) || arg_uid) {
1457 log_error("--setenv= and --uid= are not supported for 'login'. Use 'shell' instead.");
1458 return -EINVAL;
1459 }
1460
4c701096 1461 if (!IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) {
923d8fd3 1462 log_error("Login only supported on local machines.");
15411c0c 1463 return -EOPNOTSUPP;
04d39279
LP
1464 }
1465
acf97e21
LP
1466 polkit_agent_open_if_enabled();
1467
023fb90b 1468 r = sd_event_default(&event);
f647962d
MS
1469 if (r < 0)
1470 return log_error_errno(r, "Failed to get event loop: %m");
023fb90b
LP
1471
1472 r = sd_bus_attach_event(bus, event, 0);
f647962d
MS
1473 if (r < 0)
1474 return log_error_errno(r, "Failed to attach bus to event loop: %m");
023fb90b 1475
91913f58
LP
1476 machine = argc < 2 || isempty(argv[1]) ? ".host" : argv[1];
1477
63c372cb 1478 match = strjoina("type='signal',"
c454426c
LP
1479 "sender='org.freedesktop.machine1',"
1480 "path='/org/freedesktop/machine1',",
1481 "interface='org.freedesktop.machine1.Manager',"
1482 "member='MachineRemoved',"
91913f58 1483 "arg0='", machine, "'");
0ec5543c
LP
1484
1485 r = sd_bus_add_match(bus, &slot, match, on_machine_removed, &forward);
1486 if (r < 0)
1487 return log_error_errno(r, "Failed to add machine removal match: %m");
1488
2723b3b5
LP
1489 r = sd_bus_call_method(
1490 bus,
1491 "org.freedesktop.machine1",
1492 "/org/freedesktop/machine1",
1493 "org.freedesktop.machine1.Manager",
1494 "OpenMachineLogin",
1495 &error,
1496 &reply,
91913f58 1497 "s", machine);
40205d70 1498 if (r < 0) {
c454426c 1499 log_error("Failed to get login PTY: %s", bus_error_message(&error, -r));
785890ac 1500 return r;
40205d70 1501 }
04d39279 1502
40205d70
LP
1503 r = sd_bus_message_read(reply, "hs", &master, &pty);
1504 if (r < 0)
ee451d76 1505 return bus_log_parse_error(r);
04d39279 1506
ae3dde80 1507 return process_forward(event, &forward, master, PTY_FORWARD_IGNORE_VHANGUP, machine);
c454426c 1508}
04d39279 1509
c454426c 1510static int shell_machine(int argc, char *argv[], void *userdata) {
4afd3348
LP
1511 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
1512 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
c454426c 1513 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
4afd3348
LP
1514 _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot = NULL;
1515 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
c454426c
LP
1516 int master = -1, r;
1517 sd_bus *bus = userdata;
bc3bb330
ZJS
1518 const char *pty, *match, *machine, *path;
1519 _cleanup_free_ char *uid = NULL;
04d39279 1520
c454426c
LP
1521 assert(bus);
1522
4c701096 1523 if (!IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) {
c454426c
LP
1524 log_error("Shell only supported on local machines.");
1525 return -EOPNOTSUPP;
1526 }
1527
89fec318
LP
1528 /* Pass $TERM to shell session, if not explicitly specified. */
1529 if (!strv_find_prefix(arg_setenv, "TERM=")) {
1530 const char *t;
1531
1532 t = strv_find_prefix(environ, "TERM=");
1533 if (t) {
1534 if (strv_extend(&arg_setenv, t) < 0)
1535 return log_oom();
1536 }
1537 }
1538
c454426c 1539 polkit_agent_open_if_enabled();
023fb90b 1540
c454426c 1541 r = sd_event_default(&event);
f647962d 1542 if (r < 0)
c454426c 1543 return log_error_errno(r, "Failed to get event loop: %m");
023fb90b 1544
c454426c 1545 r = sd_bus_attach_event(bus, event, 0);
f647962d 1546 if (r < 0)
c454426c 1547 return log_error_errno(r, "Failed to attach bus to event loop: %m");
04d39279 1548
bc3bb330
ZJS
1549 r = parse_machine_uid(argc >= 2 ? argv[1] : NULL, &machine, &uid);
1550 if (r < 0)
1551 return log_error_errno(r, "Failed to parse machine specification: %m");
91913f58 1552
c454426c
LP
1553 match = strjoina("type='signal',"
1554 "sender='org.freedesktop.machine1',"
1555 "path='/org/freedesktop/machine1',",
1556 "interface='org.freedesktop.machine1.Manager',"
1557 "member='MachineRemoved',"
91913f58 1558 "arg0='", machine, "'");
c7b7d449 1559
c454426c
LP
1560 r = sd_bus_add_match(bus, &slot, match, on_machine_removed, &forward);
1561 if (r < 0)
1562 return log_error_errno(r, "Failed to add machine removal match: %m");
023fb90b 1563
c454426c
LP
1564 r = sd_bus_message_new_method_call(
1565 bus,
1566 &m,
1567 "org.freedesktop.machine1",
1568 "/org/freedesktop/machine1",
1569 "org.freedesktop.machine1.Manager",
1570 "OpenMachineShell");
1571 if (r < 0)
1572 return bus_log_create_error(r);
04d39279 1573
91913f58
LP
1574 path = argc < 3 || isempty(argv[2]) ? NULL : argv[2];
1575
ef3100e9 1576 r = sd_bus_message_append(m, "sss", machine, uid, path);
c454426c
LP
1577 if (r < 0)
1578 return bus_log_create_error(r);
04d39279 1579
91913f58 1580 r = sd_bus_message_append_strv(m, strv_length(argv) <= 3 ? NULL : argv + 2);
c454426c
LP
1581 if (r < 0)
1582 return bus_log_create_error(r);
1583
1584 r = sd_bus_message_append_strv(m, arg_setenv);
1585 if (r < 0)
1586 return bus_log_create_error(r);
1587
1588 r = sd_bus_call(bus, m, 0, &error, &reply);
1589 if (r < 0) {
1590 log_error("Failed to get shell PTY: %s", bus_error_message(&error, -r));
1591 return r;
1592 }
1593
1594 r = sd_bus_message_read(reply, "hs", &master, &pty);
1595 if (r < 0)
1596 return bus_log_parse_error(r);
1597
40e1f4ea 1598 return process_forward(event, &forward, master, 0, machine);
04d39279
LP
1599}
1600
08682124 1601static int remove_image(int argc, char *argv[], void *userdata) {
08682124 1602 sd_bus *bus = userdata;
ebd93cb6 1603 int r, i;
08682124
LP
1604
1605 assert(bus);
1606
acf97e21
LP
1607 polkit_agent_open_if_enabled();
1608
08682124 1609 for (i = 1; i < argc; i++) {
3d87174d
LP
1610 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1611 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1612
1613 r = sd_bus_message_new_method_call(
08682124 1614 bus,
3d87174d 1615 &m,
08682124
LP
1616 "org.freedesktop.machine1",
1617 "/org/freedesktop/machine1",
1618 "org.freedesktop.machine1.Manager",
3d87174d
LP
1619 "RemoveImage");
1620 if (r < 0)
1621 return bus_log_create_error(r);
1622
1623 r = sd_bus_message_append(m, "s", argv[i]);
1624 if (r < 0)
1625 return bus_log_create_error(r);
1626
1627 /* This is a slow operation, hence turn off any method call timeouts */
1628 r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL);
1629 if (r < 0)
1630 return log_error_errno(r, "Could not remove image: %s", bus_error_message(&error, r));
08682124
LP
1631 }
1632
1633 return 0;
1634}
1635
ebd93cb6 1636static int rename_image(int argc, char *argv[], void *userdata) {
4afd3348 1637 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
ebd93cb6
LP
1638 sd_bus *bus = userdata;
1639 int r;
1640
acf97e21
LP
1641 polkit_agent_open_if_enabled();
1642
ebd93cb6
LP
1643 r = sd_bus_call_method(
1644 bus,
1645 "org.freedesktop.machine1",
1646 "/org/freedesktop/machine1",
1647 "org.freedesktop.machine1.Manager",
1648 "RenameImage",
1649 &error,
1650 NULL,
1651 "ss", argv[1], argv[2]);
1652 if (r < 0) {
1653 log_error("Could not rename image: %s", bus_error_message(&error, -r));
1654 return r;
1655 }
1656
1657 return 0;
1658}
1659
1660static int clone_image(int argc, char *argv[], void *userdata) {
4afd3348 1661 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3d87174d 1662 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
ebd93cb6
LP
1663 sd_bus *bus = userdata;
1664 int r;
1665
acf97e21
LP
1666 polkit_agent_open_if_enabled();
1667
3d87174d 1668 r = sd_bus_message_new_method_call(
ebd93cb6 1669 bus,
3d87174d 1670 &m,
ebd93cb6
LP
1671 "org.freedesktop.machine1",
1672 "/org/freedesktop/machine1",
1673 "org.freedesktop.machine1.Manager",
3d87174d
LP
1674 "CloneImage");
1675 if (r < 0)
1676 return bus_log_create_error(r);
1677
1678 r = sd_bus_message_append(m, "ssb", argv[1], argv[2], arg_read_only);
1679 if (r < 0)
1680 return bus_log_create_error(r);
1681
1682 /* This is a slow operation, hence turn off any method call timeouts */
1683 r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL);
1684 if (r < 0)
1685 return log_error_errno(r, "Could not clone image: %s", bus_error_message(&error, r));
ebd93cb6
LP
1686
1687 return 0;
1688}
1689
1690static int read_only_image(int argc, char *argv[], void *userdata) {
4afd3348 1691 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
ebd93cb6
LP
1692 sd_bus *bus = userdata;
1693 int b = true, r;
1694
1695 if (argc > 2) {
1696 b = parse_boolean(argv[2]);
1697 if (b < 0) {
1698 log_error("Failed to parse boolean argument: %s", argv[2]);
1699 return -EINVAL;
1700 }
1701 }
1702
acf97e21
LP
1703 polkit_agent_open_if_enabled();
1704
ebd93cb6
LP
1705 r = sd_bus_call_method(
1706 bus,
1707 "org.freedesktop.machine1",
1708 "/org/freedesktop/machine1",
1709 "org.freedesktop.machine1.Manager",
1710 "MarkImageReadOnly",
1711 &error,
1712 NULL,
1713 "sb", argv[1], b);
1714 if (r < 0) {
1715 log_error("Could not mark image read-only: %s", bus_error_message(&error, -r));
1716 return r;
1717 }
1718
1719 return 0;
1720}
1721
68ce459f
LP
1722static int image_exists(sd_bus *bus, const char *name) {
1723 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1724 int r;
1725
1726 assert(bus);
1727 assert(name);
1728
1729 r = sd_bus_call_method(
1730 bus,
1731 "org.freedesktop.machine1",
1732 "/org/freedesktop/machine1",
1733 "org.freedesktop.machine1.Manager",
1734 "GetImage",
1735 &error,
1736 NULL,
1737 "s", name);
1738 if (r < 0) {
1739 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_IMAGE))
1740 return 0;
1741
1742 return log_error_errno(r, "Failed to check whether image %s exists: %s", name, bus_error_message(&error, -r));
1743 }
1744
1745 return 1;
1746}
1747
7410616c 1748static int make_service_name(const char *name, char **ret) {
7410616c
LP
1749 int r;
1750
1751 assert(name);
1752 assert(ret);
1753
1754 if (!machine_name_is_valid(name)) {
1755 log_error("Invalid machine name %s.", name);
1756 return -EINVAL;
1757 }
1758
1e9707d4 1759 r = unit_name_build("systemd-nspawn", name, ".service", ret);
7410616c
LP
1760 if (r < 0)
1761 return log_error_errno(r, "Failed to build unit name: %m");
1762
1763 return 0;
1764}
1765
ebd011d9 1766static int start_machine(int argc, char *argv[], void *userdata) {
4afd3348 1767 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
ebd011d9
LP
1768 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
1769 sd_bus *bus = userdata;
1770 int r, i;
1771
1772 assert(bus);
1773
acf97e21
LP
1774 polkit_agent_open_if_enabled();
1775
ebd011d9
LP
1776 r = bus_wait_for_jobs_new(bus, &w);
1777 if (r < 0)
1778 return log_oom();
1779
1780 for (i = 1; i < argc; i++) {
4afd3348 1781 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
7410616c 1782 _cleanup_free_ char *unit = NULL;
ebd011d9
LP
1783 const char *object;
1784
7410616c
LP
1785 r = make_service_name(argv[i], &unit);
1786 if (r < 0)
1787 return r;
ebd011d9 1788
68ce459f
LP
1789 r = image_exists(bus, argv[i]);
1790 if (r < 0)
1791 return r;
1792 if (r == 0) {
1793 log_error("Machine image '%s' does not exist.", argv[1]);
1794 return -ENXIO;
1795 }
1796
2723b3b5 1797 r = sd_bus_call_method(
ebd011d9 1798 bus,
ebd011d9
LP
1799 "org.freedesktop.systemd1",
1800 "/org/freedesktop/systemd1",
1801 "org.freedesktop.systemd1.Manager",
2723b3b5
LP
1802 "StartUnit",
1803 &error,
1804 &reply,
1805 "ss", unit, "fail");
ebd011d9
LP
1806 if (r < 0) {
1807 log_error("Failed to start unit: %s", bus_error_message(&error, -r));
1808 return r;
1809 }
1810
1811 r = sd_bus_message_read(reply, "o", &object);
1812 if (r < 0)
1813 return bus_log_parse_error(r);
1814
1815 r = bus_wait_for_jobs_add(w, object);
1816 if (r < 0)
1817 return log_oom();
1818 }
1819
10ba4835 1820 r = bus_wait_for_jobs(w, arg_quiet, NULL);
ebd011d9
LP
1821 if (r < 0)
1822 return r;
1823
1824 return 0;
1825}
1826
d8f52ed2 1827static int enable_machine(int argc, char *argv[], void *userdata) {
4afd3348
LP
1828 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
1829 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
acc0269c
CH
1830 UnitFileChange *changes = NULL;
1831 unsigned n_changes = 0;
d8f52ed2
LP
1832 int carries_install_info = 0;
1833 const char *method = NULL;
1834 sd_bus *bus = userdata;
1835 int r, i;
1836
1837 assert(bus);
1838
acf97e21
LP
1839 polkit_agent_open_if_enabled();
1840
d8f52ed2
LP
1841 method = streq(argv[0], "enable") ? "EnableUnitFiles" : "DisableUnitFiles";
1842
1843 r = sd_bus_message_new_method_call(
1844 bus,
1845 &m,
1846 "org.freedesktop.systemd1",
1847 "/org/freedesktop/systemd1",
1848 "org.freedesktop.systemd1.Manager",
1849 method);
1850 if (r < 0)
1851 return bus_log_create_error(r);
1852
d8f52ed2
LP
1853 r = sd_bus_message_open_container(m, 'a', "s");
1854 if (r < 0)
1855 return bus_log_create_error(r);
1856
1857 for (i = 1; i < argc; i++) {
90615ad7 1858 _cleanup_free_ char *unit = NULL;
d8f52ed2 1859
7410616c
LP
1860 r = make_service_name(argv[i], &unit);
1861 if (r < 0)
1862 return r;
d8f52ed2 1863
68ce459f
LP
1864 r = image_exists(bus, argv[i]);
1865 if (r < 0)
1866 return r;
1867 if (r == 0) {
1868 log_error("Machine image '%s' does not exist.", argv[1]);
1869 return -ENXIO;
1870 }
1871
d8f52ed2
LP
1872 r = sd_bus_message_append(m, "s", unit);
1873 if (r < 0)
1874 return bus_log_create_error(r);
1875 }
1876
1877 r = sd_bus_message_close_container(m);
1878 if (r < 0)
1879 return bus_log_create_error(r);
1880
1881 if (streq(argv[0], "enable"))
1882 r = sd_bus_message_append(m, "bb", false, false);
1883 else
1884 r = sd_bus_message_append(m, "b", false);
1885 if (r < 0)
1886 return bus_log_create_error(r);
1887
1888 r = sd_bus_call(bus, m, 0, &error, &reply);
1889 if (r < 0) {
1890 log_error("Failed to enable or disable unit: %s", bus_error_message(&error, -r));
1891 return r;
1892 }
1893
1894 if (streq(argv[0], "enable")) {
1895 r = sd_bus_message_read(reply, "b", carries_install_info);
1896 if (r < 0)
1897 return bus_log_parse_error(r);
1898 }
1899
acc0269c 1900 r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
d8f52ed2 1901 if (r < 0)
acc0269c 1902 goto finish;
d8f52ed2 1903
2723b3b5 1904 r = sd_bus_call_method(
d8f52ed2 1905 bus,
d8f52ed2
LP
1906 "org.freedesktop.systemd1",
1907 "/org/freedesktop/systemd1",
1908 "org.freedesktop.systemd1.Manager",
2723b3b5
LP
1909 "Reload",
1910 &error,
1911 NULL,
1912 NULL);
d8f52ed2
LP
1913 if (r < 0) {
1914 log_error("Failed to reload daemon: %s", bus_error_message(&error, -r));
acc0269c 1915 goto finish;
d8f52ed2
LP
1916 }
1917
acc0269c
CH
1918 r = 0;
1919
1920finish:
1921 unit_file_changes_free(changes, n_changes);
1922
1923 return r;
d8f52ed2
LP
1924}
1925
19070062 1926static int match_log_message(sd_bus_message *m, void *userdata, sd_bus_error *error) {
6adf7b5e 1927 const char **our_path = userdata, *line;
3d7415f4
LP
1928 unsigned priority;
1929 int r;
1930
3d7415f4 1931 assert(m);
6adf7b5e 1932 assert(our_path);
3d7415f4
LP
1933
1934 r = sd_bus_message_read(m, "us", &priority, &line);
1935 if (r < 0) {
1936 bus_log_parse_error(r);
1937 return 0;
1938 }
1939
6adf7b5e 1940 if (!streq_ptr(*our_path, sd_bus_message_get_path(m)))
3d7415f4
LP
1941 return 0;
1942
1943 if (arg_quiet && LOG_PRI(priority) >= LOG_INFO)
1944 return 0;
1945
1946 log_full(priority, "%s", line);
1947 return 0;
1948}
1949
19070062 1950static int match_transfer_removed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
6adf7b5e 1951 const char **our_path = userdata, *path, *result;
3d7415f4
LP
1952 uint32_t id;
1953 int r;
1954
3d7415f4 1955 assert(m);
6adf7b5e 1956 assert(our_path);
3d7415f4
LP
1957
1958 r = sd_bus_message_read(m, "uos", &id, &path, &result);
1959 if (r < 0) {
1960 bus_log_parse_error(r);
1961 return 0;
1962 }
1963
6adf7b5e 1964 if (!streq_ptr(*our_path, path))
3d7415f4
LP
1965 return 0;
1966
19070062 1967 sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), !streq_ptr(result, "done"));
6adf7b5e
LP
1968 return 0;
1969}
1970
1971static int transfer_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1972 assert(s);
1973 assert(si);
1974
1975 if (!arg_quiet)
cc98b302 1976 log_info("Continuing download in the background. Use \"machinectl cancel-transfer %" PRIu32 "\" to abort transfer.", PTR_TO_UINT32(userdata));
6adf7b5e
LP
1977
1978 sd_event_exit(sd_event_source_get_event(s), EINTR);
3d7415f4
LP
1979 return 0;
1980}
1981
587fec42 1982static int transfer_image_common(sd_bus *bus, sd_bus_message *m) {
4afd3348
LP
1983 _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot_job_removed = NULL, *slot_log_message = NULL;
1984 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1985 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1986 _cleanup_(sd_event_unrefp) sd_event* event = NULL;
6adf7b5e 1987 const char *path = NULL;
3d7415f4
LP
1988 uint32_t id;
1989 int r;
1990
1991 assert(bus);
1992 assert(m);
1993
1994 polkit_agent_open_if_enabled();
1995
6adf7b5e
LP
1996 r = sd_event_default(&event);
1997 if (r < 0)
1998 return log_error_errno(r, "Failed to get event loop: %m");
1999
2000 r = sd_bus_attach_event(bus, event, 0);
2001 if (r < 0)
2002 return log_error_errno(r, "Failed to attach bus to event loop: %m");
2003
3d7415f4
LP
2004 r = sd_bus_add_match(
2005 bus,
2006 &slot_job_removed,
2007 "type='signal',"
2008 "sender='org.freedesktop.import1',"
2009 "interface='org.freedesktop.import1.Manager',"
2010 "member='TransferRemoved',"
2011 "path='/org/freedesktop/import1'",
6adf7b5e 2012 match_transfer_removed, &path);
3d7415f4
LP
2013 if (r < 0)
2014 return log_error_errno(r, "Failed to install match: %m");
2015
2016 r = sd_bus_add_match(
2017 bus,
2018 &slot_log_message,
2019 "type='signal',"
2020 "sender='org.freedesktop.import1',"
2021 "interface='org.freedesktop.import1.Transfer',"
2022 "member='LogMessage'",
6adf7b5e 2023 match_log_message, &path);
3d7415f4
LP
2024 if (r < 0)
2025 return log_error_errno(r, "Failed to install match: %m");
2026
2027 r = sd_bus_call(bus, m, 0, &error, &reply);
2028 if (r < 0) {
953d28cc 2029 log_error("Failed to transfer image: %s", bus_error_message(&error, -r));
3d7415f4
LP
2030 return r;
2031 }
2032
6adf7b5e 2033 r = sd_bus_message_read(reply, "uo", &id, &path);
3d7415f4
LP
2034 if (r < 0)
2035 return bus_log_parse_error(r);
2036
72c0a2c2 2037 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
3d7415f4 2038
6adf7b5e
LP
2039 if (!arg_quiet)
2040 log_info("Enqueued transfer job %u. Press C-c to continue download in background.", id);
3d7415f4 2041
6adf7b5e
LP
2042 sd_event_add_signal(event, NULL, SIGINT, transfer_signal_handler, UINT32_TO_PTR(id));
2043 sd_event_add_signal(event, NULL, SIGTERM, transfer_signal_handler, UINT32_TO_PTR(id));
2044
2045 r = sd_event_loop(event);
2046 if (r < 0)
2047 return log_error_errno(r, "Failed to run event loop: %m");
3d7415f4 2048
6adf7b5e 2049 return -r;
3d7415f4
LP
2050}
2051
b6e676ce 2052static int import_tar(int argc, char *argv[], void *userdata) {
4afd3348 2053 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
b6e676ce
LP
2054 _cleanup_free_ char *ll = NULL;
2055 _cleanup_close_ int fd = -1;
2056 const char *local = NULL, *path = NULL;
2057 sd_bus *bus = userdata;
2058 int r;
2059
2060 assert(bus);
2061
2062 if (argc >= 2)
2063 path = argv[1];
2064 if (isempty(path) || streq(path, "-"))
2065 path = NULL;
2066
2067 if (argc >= 3)
2068 local = argv[2];
2069 else if (path)
2070 local = basename(path);
2071 if (isempty(local) || streq(local, "-"))
2072 local = NULL;
2073
2074 if (!local) {
2075 log_error("Need either path or local name.");
2076 return -EINVAL;
2077 }
2078
2079 r = tar_strip_suffixes(local, &ll);
2080 if (r < 0)
2081 return log_oom();
2082
2083 local = ll;
2084
2085 if (!machine_name_is_valid(local)) {
2086 log_error("Local name %s is not a suitable machine name.", local);
2087 return -EINVAL;
2088 }
2089
2090 if (path) {
2091 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
2092 if (fd < 0)
2093 return log_error_errno(errno, "Failed to open %s: %m", path);
2094 }
2095
2096 r = sd_bus_message_new_method_call(
2097 bus,
2098 &m,
2099 "org.freedesktop.import1",
2100 "/org/freedesktop/import1",
2101 "org.freedesktop.import1.Manager",
2102 "ImportTar");
2103 if (r < 0)
2104 return bus_log_create_error(r);
2105
2106 r = sd_bus_message_append(
2107 m,
2108 "hsbb",
2109 fd >= 0 ? fd : STDIN_FILENO,
2110 local,
2111 arg_force,
2112 arg_read_only);
2113 if (r < 0)
2114 return bus_log_create_error(r);
2115
587fec42 2116 return transfer_image_common(bus, m);
b6e676ce
LP
2117}
2118
2119static int import_raw(int argc, char *argv[], void *userdata) {
4afd3348 2120 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
b6e676ce
LP
2121 _cleanup_free_ char *ll = NULL;
2122 _cleanup_close_ int fd = -1;
2123 const char *local = NULL, *path = NULL;
2124 sd_bus *bus = userdata;
2125 int r;
2126
2127 assert(bus);
2128
2129 if (argc >= 2)
2130 path = argv[1];
2131 if (isempty(path) || streq(path, "-"))
2132 path = NULL;
2133
2134 if (argc >= 3)
2135 local = argv[2];
2136 else if (path)
2137 local = basename(path);
2138 if (isempty(local) || streq(local, "-"))
2139 local = NULL;
2140
2141 if (!local) {
2142 log_error("Need either path or local name.");
2143 return -EINVAL;
2144 }
2145
2146 r = raw_strip_suffixes(local, &ll);
2147 if (r < 0)
2148 return log_oom();
2149
2150 local = ll;
2151
2152 if (!machine_name_is_valid(local)) {
2153 log_error("Local name %s is not a suitable machine name.", local);
2154 return -EINVAL;
2155 }
2156
2157 if (path) {
2158 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
2159 if (fd < 0)
2160 return log_error_errno(errno, "Failed to open %s: %m", path);
2161 }
2162
2163 r = sd_bus_message_new_method_call(
2164 bus,
2165 &m,
2166 "org.freedesktop.import1",
2167 "/org/freedesktop/import1",
2168 "org.freedesktop.import1.Manager",
2169 "ImportRaw");
2170 if (r < 0)
2171 return bus_log_create_error(r);
2172
2173 r = sd_bus_message_append(
2174 m,
2175 "hsbb",
2176 fd >= 0 ? fd : STDIN_FILENO,
2177 local,
2178 arg_force,
2179 arg_read_only);
2180 if (r < 0)
2181 return bus_log_create_error(r);
2182
587fec42
LP
2183 return transfer_image_common(bus, m);
2184}
2185
2186static void determine_compression_from_filename(const char *p) {
2187 if (arg_format)
2188 return;
2189
2190 if (!p)
2191 return;
2192
2193 if (endswith(p, ".xz"))
2194 arg_format = "xz";
2195 else if (endswith(p, ".gz"))
2196 arg_format = "gzip";
2197 else if (endswith(p, ".bz2"))
2198 arg_format = "bzip2";
2199}
2200
2201static int export_tar(int argc, char *argv[], void *userdata) {
4afd3348 2202 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
587fec42
LP
2203 _cleanup_close_ int fd = -1;
2204 const char *local = NULL, *path = NULL;
2205 sd_bus *bus = userdata;
2206 int r;
2207
2208 assert(bus);
2209
2210 local = argv[1];
2211 if (!machine_name_is_valid(local)) {
2212 log_error("Machine name %s is not valid.", local);
2213 return -EINVAL;
2214 }
2215
2216 if (argc >= 3)
2217 path = argv[2];
2218 if (isempty(path) || streq(path, "-"))
2219 path = NULL;
2220
2221 if (path) {
2222 determine_compression_from_filename(path);
2223
2224 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
2225 if (fd < 0)
2226 return log_error_errno(errno, "Failed to open %s: %m", path);
2227 }
2228
2229 r = sd_bus_message_new_method_call(
2230 bus,
2231 &m,
2232 "org.freedesktop.import1",
2233 "/org/freedesktop/import1",
2234 "org.freedesktop.import1.Manager",
2235 "ExportTar");
2236 if (r < 0)
2237 return bus_log_create_error(r);
2238
2239 r = sd_bus_message_append(
2240 m,
2241 "shs",
2242 local,
2243 fd >= 0 ? fd : STDOUT_FILENO,
2244 arg_format);
2245 if (r < 0)
2246 return bus_log_create_error(r);
2247
2248 return transfer_image_common(bus, m);
2249}
2250
2251static int export_raw(int argc, char *argv[], void *userdata) {
4afd3348 2252 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
587fec42
LP
2253 _cleanup_close_ int fd = -1;
2254 const char *local = NULL, *path = NULL;
2255 sd_bus *bus = userdata;
2256 int r;
2257
2258 assert(bus);
2259
2260 local = argv[1];
2261 if (!machine_name_is_valid(local)) {
2262 log_error("Machine name %s is not valid.", local);
2263 return -EINVAL;
2264 }
2265
2266 if (argc >= 3)
2267 path = argv[2];
2268 if (isempty(path) || streq(path, "-"))
2269 path = NULL;
2270
2271 if (path) {
2272 determine_compression_from_filename(path);
2273
2274 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
2275 if (fd < 0)
2276 return log_error_errno(errno, "Failed to open %s: %m", path);
2277 }
2278
2279 r = sd_bus_message_new_method_call(
2280 bus,
2281 &m,
2282 "org.freedesktop.import1",
2283 "/org/freedesktop/import1",
2284 "org.freedesktop.import1.Manager",
2285 "ExportRaw");
2286 if (r < 0)
2287 return bus_log_create_error(r);
2288
2289 r = sd_bus_message_append(
2290 m,
2291 "shs",
2292 local,
2293 fd >= 0 ? fd : STDOUT_FILENO,
2294 arg_format);
2295 if (r < 0)
2296 return bus_log_create_error(r);
2297
2298 return transfer_image_common(bus, m);
b6e676ce
LP
2299}
2300
3d7415f4 2301static int pull_tar(int argc, char *argv[], void *userdata) {
4afd3348 2302 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
3d7415f4
LP
2303 _cleanup_free_ char *l = NULL, *ll = NULL;
2304 const char *local, *remote;
2305 sd_bus *bus = userdata;
2306 int r;
2307
2308 assert(bus);
2309
2310 remote = argv[1];
2311 if (!http_url_is_valid(remote)) {
2312 log_error("URL '%s' is not valid.", remote);
2313 return -EINVAL;
2314 }
2315
2316 if (argc >= 3)
2317 local = argv[2];
2318 else {
2319 r = import_url_last_component(remote, &l);
2320 if (r < 0)
2321 return log_error_errno(r, "Failed to get final component of URL: %m");
2322
2323 local = l;
2324 }
2325
2326 if (isempty(local) || streq(local, "-"))
2327 local = NULL;
2328
2329 if (local) {
2330 r = tar_strip_suffixes(local, &ll);
2331 if (r < 0)
b6e676ce 2332 return log_oom();
3d7415f4
LP
2333
2334 local = ll;
2335
2336 if (!machine_name_is_valid(local)) {
2337 log_error("Local name %s is not a suitable machine name.", local);
2338 return -EINVAL;
2339 }
2340 }
2341
2342 r = sd_bus_message_new_method_call(
2343 bus,
2344 &m,
2345 "org.freedesktop.import1",
2346 "/org/freedesktop/import1",
2347 "org.freedesktop.import1.Manager",
2348 "PullTar");
2349 if (r < 0)
2350 return bus_log_create_error(r);
2351
2352 r = sd_bus_message_append(
2353 m,
2354 "sssb",
2355 remote,
2356 local,
6e18cc9f 2357 import_verify_to_string(arg_verify),
3d7415f4
LP
2358 arg_force);
2359 if (r < 0)
2360 return bus_log_create_error(r);
2361
587fec42 2362 return transfer_image_common(bus, m);
3d7415f4
LP
2363}
2364
2365static int pull_raw(int argc, char *argv[], void *userdata) {
4afd3348 2366 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
3d7415f4
LP
2367 _cleanup_free_ char *l = NULL, *ll = NULL;
2368 const char *local, *remote;
2369 sd_bus *bus = userdata;
2370 int r;
2371
2372 assert(bus);
2373
2374 remote = argv[1];
2375 if (!http_url_is_valid(remote)) {
2376 log_error("URL '%s' is not valid.", remote);
2377 return -EINVAL;
2378 }
2379
2380 if (argc >= 3)
2381 local = argv[2];
2382 else {
2383 r = import_url_last_component(remote, &l);
2384 if (r < 0)
2385 return log_error_errno(r, "Failed to get final component of URL: %m");
2386
2387 local = l;
2388 }
2389
2390 if (isempty(local) || streq(local, "-"))
2391 local = NULL;
2392
2393 if (local) {
2394 r = raw_strip_suffixes(local, &ll);
2395 if (r < 0)
b6e676ce 2396 return log_oom();
3d7415f4
LP
2397
2398 local = ll;
2399
2400 if (!machine_name_is_valid(local)) {
2401 log_error("Local name %s is not a suitable machine name.", local);
2402 return -EINVAL;
2403 }
2404 }
2405
2406 r = sd_bus_message_new_method_call(
2407 bus,
2408 &m,
2409 "org.freedesktop.import1",
2410 "/org/freedesktop/import1",
2411 "org.freedesktop.import1.Manager",
2412 "PullRaw");
2413 if (r < 0)
2414 return bus_log_create_error(r);
2415
2416 r = sd_bus_message_append(
2417 m,
2418 "sssb",
2419 remote,
2420 local,
6e18cc9f 2421 import_verify_to_string(arg_verify),
3d7415f4
LP
2422 arg_force);
2423 if (r < 0)
2424 return bus_log_create_error(r);
2425
587fec42 2426 return transfer_image_common(bus, m);
3d7415f4
LP
2427}
2428
3d7415f4
LP
2429typedef struct TransferInfo {
2430 uint32_t id;
2431 const char *type;
2432 const char *remote;
2433 const char *local;
7079cfef 2434 double progress;
3d7415f4
LP
2435} TransferInfo;
2436
2437static int compare_transfer_info(const void *a, const void *b) {
2438 const TransferInfo *x = a, *y = b;
2439
2440 return strcmp(x->local, y->local);
2441}
2442
2443static int list_transfers(int argc, char *argv[], void *userdata) {
2444 size_t max_type = strlen("TYPE"), max_local = strlen("LOCAL"), max_remote = strlen("REMOTE");
4afd3348
LP
2445 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2446 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3d7415f4
LP
2447 _cleanup_free_ TransferInfo *transfers = NULL;
2448 size_t n_transfers = 0, n_allocated = 0, j;
2449 const char *type, *remote, *local, *object;
2450 sd_bus *bus = userdata;
2451 uint32_t id, max_id = 0;
7079cfef 2452 double progress;
3d7415f4
LP
2453 int r;
2454
ea4b98e6 2455 pager_open(arg_no_pager, false);
3d7415f4 2456
e138e7d7
ZJS
2457 r = sd_bus_call_method(bus,
2458 "org.freedesktop.import1",
2459 "/org/freedesktop/import1",
2460 "org.freedesktop.import1.Manager",
2461 "ListTransfers",
2462 &error,
2463 &reply,
2464 NULL);
3d7415f4
LP
2465 if (r < 0) {
2466 log_error("Could not get transfers: %s", bus_error_message(&error, -r));
2467 return r;
2468 }
2469
7079cfef 2470 r = sd_bus_message_enter_container(reply, 'a', "(usssdo)");
3d7415f4
LP
2471 if (r < 0)
2472 return bus_log_parse_error(r);
2473
7079cfef 2474 while ((r = sd_bus_message_read(reply, "(usssdo)", &id, &type, &remote, &local, &progress, &object)) > 0) {
3d7415f4
LP
2475 size_t l;
2476
2477 if (!GREEDY_REALLOC(transfers, n_allocated, n_transfers + 1))
2478 return log_oom();
2479
2480 transfers[n_transfers].id = id;
2481 transfers[n_transfers].type = type;
2482 transfers[n_transfers].remote = remote;
2483 transfers[n_transfers].local = local;
7079cfef 2484 transfers[n_transfers].progress = progress;
3d7415f4
LP
2485
2486 l = strlen(type);
2487 if (l > max_type)
2488 max_type = l;
2489
2490 l = strlen(remote);
2491 if (l > max_remote)
2492 max_remote = l;
2493
2494 l = strlen(local);
2495 if (l > max_local)
2496 max_local = l;
2497
2498 if (id > max_id)
2499 max_id = id;
2500
313cefa1 2501 n_transfers++;
3d7415f4
LP
2502 }
2503 if (r < 0)
2504 return bus_log_parse_error(r);
2505
2506 r = sd_bus_message_exit_container(reply);
2507 if (r < 0)
2508 return bus_log_parse_error(r);
2509
2510 qsort_safe(transfers, n_transfers, sizeof(TransferInfo), compare_transfer_info);
2511
a912ab04 2512 if (arg_legend && n_transfers > 0)
7079cfef 2513 printf("%-*s %-*s %-*s %-*s %-*s\n",
3d7415f4 2514 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id)), "ID",
7079cfef 2515 (int) 7, "PERCENT",
3d7415f4
LP
2516 (int) max_type, "TYPE",
2517 (int) max_local, "LOCAL",
2518 (int) max_remote, "REMOTE");
2519
2520 for (j = 0; j < n_transfers; j++)
7079cfef 2521 printf("%*" PRIu32 " %*u%% %-*s %-*s %-*s\n",
3d7415f4 2522 (int) MAX(2U, DECIMAL_STR_WIDTH(max_id)), transfers[j].id,
7079cfef 2523 (int) 6, (unsigned) (transfers[j].progress * 100),
3d7415f4
LP
2524 (int) max_type, transfers[j].type,
2525 (int) max_local, transfers[j].local,
2526 (int) max_remote, transfers[j].remote);
2527
f9b1947f
VV
2528 if (arg_legend) {
2529 if (n_transfers > 0)
2530 printf("\n%zu transfers listed.\n", n_transfers);
2531 else
2532 printf("No transfers.\n");
2533 }
3d7415f4
LP
2534
2535 return 0;
2536}
2537
2538static int cancel_transfer(int argc, char *argv[], void *userdata) {
4afd3348 2539 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3d7415f4
LP
2540 sd_bus *bus = userdata;
2541 int r, i;
2542
2543 assert(bus);
2544
2545 polkit_agent_open_if_enabled();
2546
2547 for (i = 1; i < argc; i++) {
2548 uint32_t id;
2549
2550 r = safe_atou32(argv[i], &id);
2551 if (r < 0)
2552 return log_error_errno(r, "Failed to parse transfer id: %s", argv[i]);
2553
2554 r = sd_bus_call_method(
2555 bus,
2556 "org.freedesktop.import1",
2557 "/org/freedesktop/import1",
2558 "org.freedesktop.import1.Manager",
2559 "CancelTransfer",
2560 &error,
2561 NULL,
2562 "u", id);
2563 if (r < 0) {
2564 log_error("Could not cancel transfer: %s", bus_error_message(&error, -r));
2565 return r;
2566 }
2567 }
2568
2569 return 0;
2570}
2571
d6ce17c7 2572static int set_limit(int argc, char *argv[], void *userdata) {
4afd3348 2573 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
d6ce17c7
LP
2574 sd_bus *bus = userdata;
2575 uint64_t limit;
2576 int r;
2577
7705a405 2578 if (STR_IN_SET(argv[argc-1], "-", "none", "infinity"))
d6ce17c7
LP
2579 limit = (uint64_t) -1;
2580 else {
59f448cf 2581 r = parse_size(argv[argc-1], 1024, &limit);
d6ce17c7
LP
2582 if (r < 0)
2583 return log_error("Failed to parse size: %s", argv[argc-1]);
d6ce17c7
LP
2584 }
2585
2586 if (argc > 2)
2587 /* With two arguments changes the quota limit of the
2588 * specified image */
2589 r = sd_bus_call_method(
2590 bus,
2591 "org.freedesktop.machine1",
2592 "/org/freedesktop/machine1",
2593 "org.freedesktop.machine1.Manager",
2594 "SetImageLimit",
2595 &error,
2596 NULL,
2597 "st", argv[1], limit);
2598 else
2599 /* With one argument changes the pool quota limit */
2600 r = sd_bus_call_method(
2601 bus,
2602 "org.freedesktop.machine1",
2603 "/org/freedesktop/machine1",
2604 "org.freedesktop.machine1.Manager",
2605 "SetPoolLimit",
2606 &error,
2607 NULL,
2608 "t", limit);
2609
2610 if (r < 0) {
2611 log_error("Could not set limit: %s", bus_error_message(&error, -r));
2612 return r;
2613 }
2614
2615 return 0;
2616}
2617
d94c2b06 2618static int clean_images(int argc, char *argv[], void *userdata) {
03c2b288 2619 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
d94c2b06
LP
2620 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2621 uint64_t usage, total = 0;
2622 char fb[FORMAT_BYTES_MAX];
2623 sd_bus *bus = userdata;
2624 const char *name;
2625 unsigned c = 0;
2626 int r;
2627
03c2b288 2628 r = sd_bus_message_new_method_call(
d94c2b06 2629 bus,
03c2b288 2630 &m,
d94c2b06
LP
2631 "org.freedesktop.machine1",
2632 "/org/freedesktop/machine1",
2633 "org.freedesktop.machine1.Manager",
03c2b288
LP
2634 "CleanPool");
2635 if (r < 0)
2636 return bus_log_create_error(r);
2637
2638 r = sd_bus_message_append(m, "s", arg_all ? "all" : "hidden");
2639 if (r < 0)
2640 return bus_log_create_error(r);
2641
2642 /* This is a slow operation, hence permit a longer time for completion. */
2643 r = sd_bus_call(bus, m, USEC_INFINITY, &error, &reply);
d94c2b06
LP
2644 if (r < 0)
2645 return log_error_errno(r, "Could not clean pool: %s", bus_error_message(&error, r));
2646
2647 r = sd_bus_message_enter_container(reply, 'a', "(st)");
2648 if (r < 0)
2649 return bus_log_parse_error(r);
2650
2651 while ((r = sd_bus_message_read(reply, "(st)", &name, &usage)) > 0) {
2652 log_info("Removed image '%s'. Freed exclusive disk space: %s",
2653 name, format_bytes(fb, sizeof(fb), usage));
2654
2655 total += usage;
2656 c++;
2657 }
2658
2659 r = sd_bus_message_exit_container(reply);
2660 if (r < 0)
2661 return bus_log_parse_error(r);
2662
2663 log_info("Removed %u images in total. Total freed exclusive disk space %s.",
2664 c, format_bytes(fb, sizeof(fb), total));
2665
2666 return 0;
2667}
2668
56159e0d 2669static int help(int argc, char *argv[], void *userdata) {
084f5805 2670 pager_open(arg_no_pager, false);
56159e0d 2671
1ee306e1 2672 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
f2cbe59e
LP
2673 "Send control commands to or query the virtual machine and container\n"
2674 "registration manager.\n\n"
2675 " -h --help Show this help\n"
2676 " --version Show package version\n"
2677 " --no-pager Do not pipe output into a pager\n"
2678 " --no-legend Do not show the headers and footers\n"
acf97e21 2679 " --no-ask-password Do not ask for system passwords\n"
f2cbe59e
LP
2680 " -H --host=[USER@]HOST Operate on remote host\n"
2681 " -M --machine=CONTAINER Operate on local container\n"
2682 " -p --property=NAME Show only properties by this name\n"
d8f52ed2 2683 " -q --quiet Suppress output\n"
f2cbe59e 2684 " -a --all Show all properties, including empty ones\n"
85500523 2685 " --value When showing properties, only print the value\n"
f2cbe59e
LP
2686 " -l --full Do not ellipsize output\n"
2687 " --kill-who=WHO Who to send signal to\n"
2688 " -s --signal=SIGNAL Which signal to send\n"
c454426c 2689 " --uid=USER Specify user ID to invoke shell as\n"
4d46e5db 2690 " -E --setenv=VAR=VALUE Add an environment variable for shell\n"
f2cbe59e 2691 " --read-only Create read-only bind mount\n"
8b0cc9a3
LP
2692 " --mkdir Create directory before bind mounting, if missing\n"
2693 " -n --lines=INTEGER Number of journal entries to show\n"
07b0b339 2694 " --max-addresses=INTEGER Number of internet addresses to show at most\n"
7e563bfc
IW
2695 " -o --output=STRING Change journal output mode (short, short-precise,\n"
2696 " short-iso, short-iso-precise, short-full,\n"
2697 " short-monotonic, short-unix, verbose, export,\n"
2698 " json, json-pretty, json-sse, cat)\n"
fc2288f0 2699 " --verify=MODE Verification mode for downloaded images (no,\n"
7f444afa 2700 " checksum, signature)\n"
fc2288f0 2701 " --force Download image even if already exists\n\n"
cd61c3bf 2702 "Machine Commands:\n"
f2cbe59e 2703 " list List running VMs and containers\n"
fefdc04b 2704 " status NAME... Show VM/container details\n"
91913f58 2705 " show [NAME...] Show properties of one or more VMs/containers\n"
ebd011d9 2706 " start NAME... Start container as a service\n"
91913f58
LP
2707 " login [NAME] Get a login prompt in a container or on the\n"
2708 " local host\n"
ef3100e9
LP
2709 " shell [[USER@]NAME [COMMAND...]]\n"
2710 " Invoke a shell (or other command) in a container\n"
2711 " or on the local host\n"
d8f52ed2
LP
2712 " enable NAME... Enable automatic container start at boot\n"
2713 " disable NAME... Disable automatic container start at boot\n"
f2cbe59e
LP
2714 " poweroff NAME... Power off one or more containers\n"
2715 " reboot NAME... Reboot one or more containers\n"
f2cbe59e 2716 " terminate NAME... Terminate one or more VMs/containers\n"
ebd93cb6 2717 " kill NAME... Send signal to processes of a VM/container\n"
f2cbe59e 2718 " copy-to NAME PATH [PATH] Copy files from the host to a container\n"
ebd93cb6
LP
2719 " copy-from NAME PATH [PATH] Copy files from a container to the host\n"
2720 " bind NAME PATH [PATH] Bind mount a path from the host into a container\n\n"
fefdc04b 2721 "Image Commands:\n"
56b921c3 2722 " list-images Show available container and VM images\n"
91913f58
LP
2723 " image-status [NAME...] Show image details\n"
2724 " show-image [NAME...] Show properties of image\n"
ebd93cb6
LP
2725 " clone NAME NAME Clone an image\n"
2726 " rename NAME NAME Rename an image\n"
2727 " read-only NAME [BOOL] Mark or unmark image read-only\n"
d6ce17c7 2728 " remove NAME... Remove an image\n"
5bda1f47
LP
2729 " set-limit [NAME] BYTES Set image or pool size limit (disk quota)\n"
2730 " clean Remove hidden (or all) images\n\n"
b5b38b41
LP
2731 "Image Transfer Commands:\n"
2732 " pull-tar URL [NAME] Download a TAR container image\n"
2733 " pull-raw URL [NAME] Download a RAW container or VM image\n"
587fec42
LP
2734 " import-tar FILE [NAME] Import a local TAR container image\n"
2735 " import-raw FILE [NAME] Import a local RAW container or VM image\n"
6e9efa59
LP
2736 " export-tar NAME [FILE] Export a TAR container image locally\n"
2737 " export-raw NAME [FILE] Export a RAW container or VM image locally\n"
b5b38b41 2738 " list-transfers Show list of downloads in progress\n"
3d7415f4 2739 " cancel-transfer Cancel a download\n"
f7621db0 2740 , program_invocation_short_name);
56159e0d
LP
2741
2742 return 0;
1ee306e1
LP
2743}
2744
2745static int parse_argv(int argc, char *argv[]) {
2746
2747 enum {
2748 ARG_VERSION = 0x100,
2749 ARG_NO_PAGER,
e56056e9 2750 ARG_NO_LEGEND,
85500523 2751 ARG_VALUE,
1ee306e1 2752 ARG_KILL_WHO,
785890ac
LP
2753 ARG_READ_ONLY,
2754 ARG_MKDIR,
acf97e21 2755 ARG_NO_ASK_PASSWORD,
3d7415f4
LP
2756 ARG_VERIFY,
2757 ARG_FORCE,
587fec42 2758 ARG_FORMAT,
c454426c 2759 ARG_UID,
07b0b339 2760 ARG_NUMBER_IPS,
1ee306e1
LP
2761 };
2762
2763 static const struct option options[] = {
2764 { "help", no_argument, NULL, 'h' },
2765 { "version", no_argument, NULL, ARG_VERSION },
2766 { "property", required_argument, NULL, 'p' },
2767 { "all", no_argument, NULL, 'a' },
85500523 2768 { "value", no_argument, NULL, ARG_VALUE },
1ee306e1
LP
2769 { "full", no_argument, NULL, 'l' },
2770 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
e56056e9 2771 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
1ee306e1
LP
2772 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
2773 { "signal", required_argument, NULL, 's' },
2774 { "host", required_argument, NULL, 'H' },
a7893c6b 2775 { "machine", required_argument, NULL, 'M' },
785890ac
LP
2776 { "read-only", no_argument, NULL, ARG_READ_ONLY },
2777 { "mkdir", no_argument, NULL, ARG_MKDIR },
d8f52ed2 2778 { "quiet", no_argument, NULL, 'q' },
8b0cc9a3
LP
2779 { "lines", required_argument, NULL, 'n' },
2780 { "output", required_argument, NULL, 'o' },
acf97e21 2781 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
3d7415f4
LP
2782 { "verify", required_argument, NULL, ARG_VERIFY },
2783 { "force", no_argument, NULL, ARG_FORCE },
587fec42 2784 { "format", required_argument, NULL, ARG_FORMAT },
c454426c 2785 { "uid", required_argument, NULL, ARG_UID },
4d46e5db 2786 { "setenv", required_argument, NULL, 'E' },
07b0b339 2787 { "max-addresses", required_argument, NULL, ARG_NUMBER_IPS },
eb9da376 2788 {}
1ee306e1
LP
2789 };
2790
368d2643 2791 bool reorder = false;
768c1dec 2792 int c, r, shell = -1;
1ee306e1
LP
2793
2794 assert(argc >= 0);
2795 assert(argv);
2796
368d2643 2797 for (;;) {
61f638e5 2798 static const char option_string[] = "-hp:als:H:M:qn:o:E:";
368d2643 2799
0bf50960 2800 c = getopt_long(argc, argv, option_string + reorder, options, NULL);
768c1dec
LP
2801 if (c < 0)
2802 break;
2803
2804 switch (c) {
2805
2806 case 1: /* getopt_long() returns 1 if "-" was the first character of the option string, and a
2807 * non-option argument was discovered. */
2808
2809 assert(!reorder);
2810
368d2643
LP
2811 /* We generally are fine with the fact that getopt_long() reorders the command line, and looks
2812 * for switches after the main verb. However, for "shell" we really don't want that, since we
768c1dec
LP
2813 * want that switches specified after the machine name are passed to the program to execute,
2814 * and not processed by us. To make this possible, we'll first invoke getopt_long() with
2815 * reordering disabled (i.e. with the "-" prefix in the option string), looking for the first
2816 * non-option parameter. If it's the verb "shell" we remember its position and continue
2817 * processing options. In this case, as soon as we hit the next non-option argument we found
2818 * the machine name, and stop further processing. If the first non-option argument is any other
2819 * verb than "shell" we switch to normal reordering mode and continue processing arguments
2820 * normally. */
2821
2822 if (shell >= 0) {
2823 /* If we already found the "shell" verb on the command line, and now found the next
2824 * non-option argument, then this is the machine name and we should stop processing
2825 * further arguments. */
2826 optind --; /* don't process this argument, go one step back */
2827 goto done;
2828 }
2829 if (streq(optarg, "shell"))
2830 /* Remember the position of the "shell" verb, and continue processing normally. */
2831 shell = optind - 1;
2832 else {
2833 int saved_optind;
2834
2835 /* OK, this is some other verb. In this case, turn on reordering again, and continue
2836 * processing normally. */
368d2643 2837 reorder = true;
768c1dec
LP
2838
2839 /* We changed the option string. getopt_long() only looks at it again if we invoke it
2840 * at least once with a reset option index. Hence, let's reset the option index here,
2841 * then invoke getopt_long() again (ignoring what it has to say, after all we most
2842 * likely already processed it), and the bump the option index so that we read the
2843 * intended argument again. */
2844 saved_optind = optind;
2845 optind = 0;
2846 (void) getopt_long(argc, argv, option_string + reorder, options, NULL);
2847 optind = saved_optind - 1; /* go one step back, process this argument again */
368d2643
LP
2848 }
2849
2850 break;
1ee306e1
LP
2851
2852 case 'h':
56159e0d 2853 return help(0, NULL, NULL);
1ee306e1
LP
2854
2855 case ARG_VERSION:
3f6fd1ba 2856 return version();
1ee306e1 2857
a7893c6b
LP
2858 case 'p':
2859 r = strv_extend(&arg_property, optarg);
2860 if (r < 0)
2861 return log_oom();
1ee306e1
LP
2862
2863 /* If the user asked for a particular
2864 * property, show it to him, even if it is
2865 * empty. */
2866 arg_all = true;
2867 break;
1ee306e1
LP
2868
2869 case 'a':
2870 arg_all = true;
2871 break;
2872
85500523
ZJS
2873 case ARG_VALUE:
2874 arg_value = true;
2875 break;
2876
1ee306e1
LP
2877 case 'l':
2878 arg_full = true;
2879 break;
2880
8b0cc9a3
LP
2881 case 'n':
2882 if (safe_atou(optarg, &arg_lines) < 0) {
2883 log_error("Failed to parse lines '%s'", optarg);
2884 return -EINVAL;
2885 }
2886 break;
2887
2888 case 'o':
2889 arg_output = output_mode_from_string(optarg);
2890 if (arg_output < 0) {
2891 log_error("Unknown output '%s'.", optarg);
2892 return -EINVAL;
2893 }
2894 break;
2895
1ee306e1
LP
2896 case ARG_NO_PAGER:
2897 arg_no_pager = true;
2898 break;
2899
e56056e9
TA
2900 case ARG_NO_LEGEND:
2901 arg_legend = false;
2902 break;
2903
1ee306e1
LP
2904 case ARG_KILL_WHO:
2905 arg_kill_who = optarg;
2906 break;
2907
2908 case 's':
2909 arg_signal = signal_from_string_try_harder(optarg);
2910 if (arg_signal < 0) {
2911 log_error("Failed to parse signal string %s.", optarg);
2912 return -EINVAL;
2913 }
2914 break;
2915
acf97e21
LP
2916 case ARG_NO_ASK_PASSWORD:
2917 arg_ask_password = false;
2918 break;
2919
1ee306e1 2920 case 'H':
d21ed1ea 2921 arg_transport = BUS_TRANSPORT_REMOTE;
a7893c6b
LP
2922 arg_host = optarg;
2923 break;
2924
2925 case 'M':
de33fc62 2926 arg_transport = BUS_TRANSPORT_MACHINE;
a7893c6b 2927 arg_host = optarg;
1ee306e1
LP
2928 break;
2929
785890ac
LP
2930 case ARG_READ_ONLY:
2931 arg_read_only = true;
2932 break;
2933
2934 case ARG_MKDIR:
2935 arg_mkdir = true;
2936 break;
2937
d8f52ed2
LP
2938 case 'q':
2939 arg_quiet = true;
2940 break;
2941
3d7415f4 2942 case ARG_VERIFY:
6e18cc9f
LP
2943 arg_verify = import_verify_from_string(optarg);
2944 if (arg_verify < 0) {
2945 log_error("Failed to parse --verify= setting: %s", optarg);
2946 return -EINVAL;
2947 }
3d7415f4
LP
2948 break;
2949
2950 case ARG_FORCE:
2951 arg_force = true;
2952 break;
2953
587fec42
LP
2954 case ARG_FORMAT:
2955 if (!STR_IN_SET(optarg, "uncompressed", "xz", "gzip", "bzip2")) {
2956 log_error("Unknown format: %s", optarg);
2957 return -EINVAL;
2958 }
2959
2960 arg_format = optarg;
2961 break;
2962
c454426c
LP
2963 case ARG_UID:
2964 arg_uid = optarg;
2965 break;
2966
4d46e5db 2967 case 'E':
c454426c
LP
2968 if (!env_assignment_is_valid(optarg)) {
2969 log_error("Environment assignment invalid: %s", optarg);
2970 return -EINVAL;
2971 }
2972
2973 r = strv_extend(&arg_setenv, optarg);
2974 if (r < 0)
2975 return log_oom();
2976 break;
2977
07b0b339
SK
2978 case ARG_NUMBER_IPS:
2979 if (streq(optarg, "all"))
2980 arg_addrs = ALL_IP_ADDRESSES;
2981 else if (safe_atoi(optarg, &arg_addrs) < 0) {
2982 log_error("Invalid number of IPs");
2983 return -EINVAL;
2984 } else if (arg_addrs < 0) {
2985 log_error("Number of IPs cannot be negative");
2986 return -EINVAL;
2987 }
2988 break;
2989
1ee306e1
LP
2990 case '?':
2991 return -EINVAL;
2992
2993 default:
eb9da376 2994 assert_not_reached("Unhandled option");
1ee306e1 2995 }
368d2643 2996 }
1ee306e1 2997
768c1dec
LP
2998done:
2999 if (shell >= 0) {
3000 char *t;
3001 int i;
3002
3003 /* We found the "shell" verb while processing the argument list. Since we turned off reordering of the
3004 * argument list initially let's readjust it now, and move the "shell" verb to the back. */
3005
3006 optind -= 1; /* place the option index where the "shell" verb will be placed */
3007
3008 t = argv[shell];
3009 for (i = shell; i < optind; i++)
3010 argv[i] = argv[i+1];
3011 argv[optind] = t;
3012 }
3013
1ee306e1
LP
3014 return 1;
3015}
3016
56159e0d
LP
3017static int machinectl_main(int argc, char *argv[], sd_bus *bus) {
3018
3019 static const Verb verbs[] = {
3d7415f4
LP
3020 { "help", VERB_ANY, VERB_ANY, 0, help },
3021 { "list", VERB_ANY, 1, VERB_DEFAULT, list_machines },
3022 { "list-images", VERB_ANY, 1, 0, list_images },
3023 { "status", 2, VERB_ANY, 0, show_machine },
160e3793 3024 { "image-status", VERB_ANY, VERB_ANY, 0, show_image },
3d7415f4
LP
3025 { "show", VERB_ANY, VERB_ANY, 0, show_machine },
3026 { "show-image", VERB_ANY, VERB_ANY, 0, show_image },
3027 { "terminate", 2, VERB_ANY, 0, terminate_machine },
3028 { "reboot", 2, VERB_ANY, 0, reboot_machine },
3029 { "poweroff", 2, VERB_ANY, 0, poweroff_machine },
b2bb19bb 3030 { "stop", 2, VERB_ANY, 0, poweroff_machine }, /* Convenience alias */
3d7415f4 3031 { "kill", 2, VERB_ANY, 0, kill_machine },
91913f58
LP
3032 { "login", VERB_ANY, 2, 0, login_machine },
3033 { "shell", VERB_ANY, VERB_ANY, 0, shell_machine },
3d7415f4
LP
3034 { "bind", 3, 4, 0, bind_mount },
3035 { "copy-to", 3, 4, 0, copy_files },
3036 { "copy-from", 3, 4, 0, copy_files },
3037 { "remove", 2, VERB_ANY, 0, remove_image },
3038 { "rename", 3, 3, 0, rename_image },
3039 { "clone", 3, 3, 0, clone_image },
3040 { "read-only", 2, 3, 0, read_only_image },
3041 { "start", 2, VERB_ANY, 0, start_machine },
3042 { "enable", 2, VERB_ANY, 0, enable_machine },
3043 { "disable", 2, VERB_ANY, 0, enable_machine },
b6e676ce
LP
3044 { "import-tar", 2, 3, 0, import_tar },
3045 { "import-raw", 2, 3, 0, import_raw },
587fec42
LP
3046 { "export-tar", 2, 3, 0, export_tar },
3047 { "export-raw", 2, 3, 0, export_raw },
3d7415f4
LP
3048 { "pull-tar", 2, 3, 0, pull_tar },
3049 { "pull-raw", 2, 3, 0, pull_raw },
3d7415f4
LP
3050 { "list-transfers", VERB_ANY, 1, 0, list_transfers },
3051 { "cancel-transfer", 2, VERB_ANY, 0, cancel_transfer },
d6ce17c7 3052 { "set-limit", 2, 3, 0, set_limit },
d94c2b06 3053 { "clean", VERB_ANY, 1, 0, clean_images },
56159e0d 3054 {}
1ee306e1
LP
3055 };
3056
56159e0d 3057 return dispatch_verb(argc, argv, verbs, bus);
1ee306e1
LP
3058}
3059
3060int main(int argc, char*argv[]) {
cf647b69 3061 sd_bus *bus = NULL;
84f6181c 3062 int r;
1ee306e1
LP
3063
3064 setlocale(LC_ALL, "");
3065 log_parse_environment();
3066 log_open();
3067
3068 r = parse_argv(argc, argv);
84f6181c 3069 if (r <= 0)
1ee306e1 3070 goto finish;
1ee306e1 3071
266f3e26 3072 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
a1da8583 3073 if (r < 0) {
da927ba9 3074 log_error_errno(r, "Failed to create bus connection: %m");
a1da8583
TG
3075 goto finish;
3076 }
1ee306e1 3077
2723b3b5
LP
3078 sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
3079
56159e0d 3080 r = machinectl_main(argc, argv, bus);
1ee306e1
LP
3081
3082finish:
cf647b69 3083 sd_bus_flush_close_unref(bus);
1ee306e1 3084 pager_close();
acf97e21 3085 polkit_agent_close();
1ee306e1 3086
84f6181c 3087 strv_free(arg_property);
c454426c 3088 strv_free(arg_setenv);
84f6181c
LP
3089
3090 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1ee306e1 3091}