]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/portable/portablectl.c
util-lib: split out env file parsing code into env-file.c
[thirdparty/systemd.git] / src / portable / portablectl.c
CommitLineData
61d0578b
LP
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include <errno.h>
4#include <getopt.h>
5
6#include "sd-bus.h"
7
8#include "alloc-util.h"
9#include "bus-error.h"
10#include "bus-util.h"
11#include "def.h"
12#include "dirent-util.h"
686d13b9 13#include "env-file.h"
61d0578b 14#include "fd-util.h"
61d0578b
LP
15#include "format-table.h"
16#include "fs-util.h"
17#include "locale-util.h"
18#include "machine-image.h"
c55a594e 19#include "main-func.h"
61d0578b
LP
20#include "pager.h"
21#include "parse-util.h"
22#include "path-util.h"
294bf0c3 23#include "pretty-print.h"
61d0578b
LP
24#include "spawn-polkit-agent.h"
25#include "string-util.h"
26#include "strv.h"
27#include "terminal-util.h"
28#include "verbs.h"
29
0221d68a 30static PagerFlags arg_pager_flags = 0;
61d0578b
LP
31static bool arg_legend = true;
32static bool arg_ask_password = true;
33static bool arg_quiet = false;
34static const char *arg_profile = "default";
35static const char* arg_copy_mode = NULL;
36static bool arg_runtime = false;
37static bool arg_reload = true;
38static bool arg_cat = false;
39static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
0c760322 40static const char *arg_host = NULL;
61d0578b
LP
41
42static int determine_image(const char *image, bool permit_non_existing, char **ret) {
43 int r;
44
45 /* If the specified name is a valid image name, we pass it as-is to portabled, which will search for it in the
46 * usual search directories. Otherwise we presume it's a path, and will normalize it on the client's side
47 * (among other things, to make the path independent of the client's working directory) before passing it
48 * over. */
49
50 if (image_name_is_valid(image)) {
51 char *c;
52
53 if (!arg_quiet && laccess(image, F_OK) >= 0)
54 log_warning("Ambiguous invocation: current working directory contains file matching non-path argument '%s', ignoring. "
55 "Prefix argument with './' to force reference to file in current working directory.", image);
56
57 c = strdup(image);
58 if (!c)
59 return log_oom();
60
61 *ret = c;
62 return 0;
63 }
64
baaa35ad
ZJS
65 if (arg_transport != BUS_TRANSPORT_LOCAL)
66 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
67 "Operations on images by path not supported when connecting to remote systems.");
61d0578b
LP
68
69 r = chase_symlinks(image, NULL, CHASE_TRAIL_SLASH | (permit_non_existing ? CHASE_NONEXISTENT : 0), ret);
70 if (r < 0)
71 return log_error_errno(r, "Cannot normalize specified image path '%s': %m", image);
72
73 return 0;
74}
75
76static int extract_prefix(const char *path, char **ret) {
77 _cleanup_free_ char *name = NULL;
78 const char *bn, *underscore;
79 size_t m;
80
81 bn = basename(path);
82
83 underscore = strchr(bn, '_');
84 if (underscore)
85 m = underscore - bn;
86 else {
87 const char *e;
88
89 e = endswith(bn, ".raw");
90 if (!e)
91 e = strchr(bn, 0);
92
93 m = e - bn;
94 }
95
96 name = strndup(bn, m);
97 if (!name)
98 return -ENOMEM;
99
100 /* A slightly reduced version of what's permitted in unit names. With ':' and '\' are removed, as well as '_'
101 * which we use as delimiter for the second part of the image string, which we ignore for now. */
102 if (!in_charset(name, DIGITS LETTERS "-."))
103 return -EINVAL;
104
105 if (!filename_is_valid(name))
106 return -EINVAL;
107
810aa425 108 *ret = TAKE_PTR(name);
61d0578b
LP
109
110 return 0;
111}
112
113static int determine_matches(const char *image, char **l, bool allow_any, char ***ret) {
66c9dfdd 114 _cleanup_strv_free_ char **k = NULL;
61d0578b
LP
115 int r;
116
117 /* Determine the matches to apply. If the list is empty we derive the match from the image name. If the list
118 * contains exactly the "-" we return a wildcard list (which is the empty list), but only if this is expressly
119 * permitted. */
120
121 if (strv_isempty(l)) {
122 char *prefix;
123
124 r = extract_prefix(image, &prefix);
125 if (r < 0)
126 return log_error_errno(r, "Failed to extract prefix of image name '%s': %m", image);
127
128 if (!arg_quiet)
129 log_info("(Matching unit files with prefix '%s'.)", prefix);
130
61d0578b
LP
131 r = strv_consume(&k, prefix);
132 if (r < 0)
133 return log_oom();
134
135 } else if (strv_equal(l, STRV_MAKE("-"))) {
136
baaa35ad
ZJS
137 if (!allow_any)
138 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
139 "Refusing all unit file match.");
61d0578b
LP
140
141 if (!arg_quiet)
142 log_info("(Matching all unit files.)");
61d0578b 143 } else {
61d0578b
LP
144
145 k = strv_copy(l);
146 if (!k)
147 return log_oom();
148
21cffed7
LP
149 if (!arg_quiet) {
150 _cleanup_free_ char *joined = NULL;
151
152 joined = strv_join(k, "', '");
153 if (!joined)
154 return log_oom();
61d0578b 155
61d0578b 156 log_info("(Matching unit files with prefixes '%s'.)", joined);
21cffed7 157 }
61d0578b
LP
158 }
159
66c9dfdd 160 *ret = TAKE_PTR(k);
61d0578b
LP
161
162 return 0;
163}
164
165static int acquire_bus(sd_bus **bus) {
166 int r;
167
168 assert(bus);
169
170 if (*bus)
171 return 0;
172
173 r = bus_connect_transport(arg_transport, arg_host, false, bus);
174 if (r < 0)
175 return log_error_errno(r, "Failed to connect to bus: %m");
176
177 (void) sd_bus_set_allow_interactive_authorization(*bus, arg_ask_password);
178
179 return 0;
180}
181
182static int maybe_reload(sd_bus **bus) {
183 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
184 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
185 int r;
186
187 if (!arg_reload)
188 return 0;
189
190 r = acquire_bus(bus);
191 if (r < 0)
192 return r;
193
194 r = sd_bus_message_new_method_call(
195 *bus,
196 &m,
197 "org.freedesktop.systemd1",
198 "/org/freedesktop/systemd1",
199 "org.freedesktop.systemd1.Manager",
200 "Reload");
201 if (r < 0)
202 return bus_log_create_error(r);
203
204 /* Reloading the daemon may take long, hence set a longer timeout here */
205 r = sd_bus_call(*bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL);
206 if (r < 0)
207 return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r));
208
209 return 0;
210}
211
212static int inspect_image(int argc, char *argv[], void *userdata) {
213 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
214 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
215 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
216 _cleanup_strv_free_ char **matches = NULL;
217 _cleanup_free_ char *image = NULL;
218 bool nl = false, header = false;
219 const void *data;
220 const char *path;
221 size_t sz;
222 int r;
223
224 r = determine_image(argv[1], false, &image);
225 if (r < 0)
226 return r;
227
228 r = determine_matches(argv[1], argv + 2, true, &matches);
229 if (r < 0)
230 return r;
231
232 r = acquire_bus(&bus);
233 if (r < 0)
234 return r;
235
236 r = sd_bus_message_new_method_call(
237 bus,
238 &m,
239 "org.freedesktop.portable1",
240 "/org/freedesktop/portable1",
241 "org.freedesktop.portable1.Manager",
242 "GetImageMetadata");
243 if (r < 0)
244 return bus_log_create_error(r);
245
246 r = sd_bus_message_append(m, "s", image);
247 if (r < 0)
248 return bus_log_create_error(r);
249
250 r = sd_bus_message_append_strv(m, matches);
251 if (r < 0)
252 return bus_log_create_error(r);
253
254 r = sd_bus_call(bus, m, 0, &error, &reply);
255 if (r < 0)
256 return log_error_errno(r, "Failed to inspect image metadata: %s", bus_error_message(&error, r));
257
258 r = sd_bus_message_read(reply, "s", &path);
259 if (r < 0)
260 return bus_log_parse_error(r);
261
262 r = sd_bus_message_read_array(reply, 'y', &data, &sz);
263 if (r < 0)
264 return bus_log_parse_error(r);
265
0221d68a 266 (void) pager_open(arg_pager_flags);
61d0578b
LP
267
268 if (arg_cat) {
269 printf("%s-- OS Release: --%s\n", ansi_highlight(), ansi_normal());
270 fwrite(data, sz, 1, stdout);
271 fflush(stdout);
272 nl = true;
273 } else {
8d93d7d8 274 _cleanup_free_ char *pretty_portable = NULL, *pretty_os = NULL;
61d0578b
LP
275
276 _cleanup_fclose_ FILE *f;
277
278 f = fmemopen((void*) data, sz, "re");
279 if (!f)
280 return log_error_errno(errno, "Failed to open /etc/os-release buffer: %m");
281
aa8fbc74 282 r = parse_env_file(f, "/etc/os-release",
61d0578b 283 "PORTABLE_PRETTY_NAME", &pretty_portable,
13df9c39 284 "PRETTY_NAME", &pretty_os);
61d0578b
LP
285 if (r < 0)
286 return log_error_errno(r, "Failed to parse /etc/os-release: %m");
287
288 printf("Image:\n\t%s\n"
289 "Portable Service:\n\t%s\n"
290 "Operating System:\n\t%s\n",
291 path,
292 strna(pretty_portable),
293 strna(pretty_os));
294 }
295
296 r = sd_bus_message_enter_container(reply, 'a', "{say}");
297 if (r < 0)
298 return bus_log_parse_error(r);
299
300 for (;;) {
301 const char *name;
302
303 r = sd_bus_message_enter_container(reply, 'e', "say");
304 if (r < 0)
305 return bus_log_parse_error(r);
306 if (r == 0)
307 break;
308
309 r = sd_bus_message_read(reply, "s", &name);
310 if (r < 0)
311 return bus_log_parse_error(r);
312
313 r = sd_bus_message_read_array(reply, 'y', &data, &sz);
314 if (r < 0)
315 return bus_log_parse_error(r);
316
317 if (arg_cat) {
318 if (nl)
319 fputc('\n', stdout);
320
321 printf("%s-- Unit file: %s --%s\n", ansi_highlight(), name, ansi_normal());
322 fwrite(data, sz, 1, stdout);
323 fflush(stdout);
324 nl = true;
325 } else {
326 if (!header) {
327 fputs("Unit files:\n", stdout);
328 header = true;
329 }
330
331 fputc('\t', stdout);
332 fputs(name, stdout);
333 fputc('\n', stdout);
334 }
335
336 r = sd_bus_message_exit_container(reply);
337 if (r < 0)
338 return bus_log_parse_error(r);
339 }
340
341 r = sd_bus_message_exit_container(reply);
342 if (r < 0)
343 return bus_log_parse_error(r);
344
345 return 0;
346}
347
348static int print_changes(sd_bus_message *m) {
349 int r;
350
351 if (arg_quiet)
352 return 0;
353
354 r = sd_bus_message_enter_container(m, 'a', "(sss)");
355 if (r < 0)
356 return bus_log_parse_error(r);
357
358 for (;;) {
359 const char *type, *path, *source;
360
361 r = sd_bus_message_read(m, "(sss)", &type, &path, &source);
362 if (r < 0)
363 return bus_log_parse_error(r);
364 if (r == 0)
365 break;
366
367 if (streq(type, "symlink"))
368 log_info("Created symlink %s %s %s.", path, special_glyph(ARROW), source);
369 else if (streq(type, "copy")) {
370 if (isempty(source))
371 log_info("Copied %s.", path);
372 else
373 log_info("Copied %s %s %s.", source, special_glyph(ARROW), path);
374 } else if (streq(type, "unlink"))
375 log_info("Removed %s.", path);
376 else if (streq(type, "write"))
377 log_info("Written %s.", path);
378 else if (streq(type, "mkdir"))
379 log_info("Created directory %s.", path);
380 else
381 log_error("Unexpected change: %s/%s/%s", type, path, source);
382 }
383
384 r = sd_bus_message_exit_container(m);
385 if (r < 0)
386 return r;
387
388 return 0;
389}
390
391static int attach_image(int argc, char *argv[], void *userdata) {
392 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
393 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
394 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
395 _cleanup_strv_free_ char **matches = NULL;
396 _cleanup_free_ char *image = NULL;
397 int r;
398
399 r = determine_image(argv[1], false, &image);
400 if (r < 0)
401 return r;
402
403 r = determine_matches(argv[1], argv + 2, false, &matches);
404 if (r < 0)
405 return r;
406
407 r = acquire_bus(&bus);
408 if (r < 0)
409 return r;
410
411 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
412
413 r = sd_bus_message_new_method_call(
414 bus,
415 &m,
416 "org.freedesktop.portable1",
417 "/org/freedesktop/portable1",
418 "org.freedesktop.portable1.Manager",
419 "AttachImage");
420 if (r < 0)
421 return bus_log_create_error(r);
422
423 r = sd_bus_message_append(m, "s", image);
424 if (r < 0)
425 return bus_log_create_error(r);
426
427 r = sd_bus_message_append_strv(m, matches);
428 if (r < 0)
429 return bus_log_create_error(r);
430
431 r = sd_bus_message_append(m, "sbs", arg_profile, arg_runtime, arg_copy_mode);
432 if (r < 0)
433 return bus_log_create_error(r);
434
435 r = sd_bus_call(bus, m, 0, &error, &reply);
436 if (r < 0)
437 return log_error_errno(r, "Failed to attach image: %s", bus_error_message(&error, r));
438
439 (void) maybe_reload(&bus);
440
441 print_changes(reply);
442 return 0;
443}
444
445static int detach_image(int argc, char *argv[], void *userdata) {
446 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
447 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
448 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
449 _cleanup_free_ char *image = NULL;
450 int r;
451
452 r = determine_image(argv[1], true, &image);
453 if (r < 0)
454 return r;
455
456 r = acquire_bus(&bus);
457 if (r < 0)
458 return r;
459
460 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
461
462 r = sd_bus_call_method(
463 bus,
464 "org.freedesktop.portable1",
465 "/org/freedesktop/portable1",
466 "org.freedesktop.portable1.Manager",
467 "DetachImage",
468 &error,
469 &reply,
470 "sb", image, arg_runtime);
471 if (r < 0)
472 return log_error_errno(r, "Failed to detach image: %s", bus_error_message(&error, r));
473
474 (void) maybe_reload(&bus);
475
476 print_changes(reply);
477 return 0;
478}
479
480static int list_images(int argc, char *argv[], void *userdata) {
481 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
482 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
483 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
484 _cleanup_(table_unrefp) Table *table = NULL;
485 int r;
486
487 r = acquire_bus(&bus);
488 if (r < 0)
489 return r;
490
491 r = sd_bus_call_method(
492 bus,
493 "org.freedesktop.portable1",
494 "/org/freedesktop/portable1",
495 "org.freedesktop.portable1.Manager",
496 "ListImages",
497 &error,
498 &reply,
499 NULL);
500 if (r < 0)
501 return log_error_errno(r, "Failed to list images: %s", bus_error_message(&error, r));
502
503 table = table_new("NAME", "TYPE", "RO", "CRTIME", "MTIME", "USAGE", "STATE");
504 if (!table)
505 return log_oom();
506
507 r = sd_bus_message_enter_container(reply, 'a', "(ssbtttso)");
508 if (r < 0)
509 return bus_log_parse_error(r);
510
511 for (;;) {
31d99bd1 512 const char *name, *type, *state;
61d0578b
LP
513 uint64_t crtime, mtime, usage;
514 TableCell *cell;
515 bool ro_bool;
516 int ro_int;
517
31d99bd1 518 r = sd_bus_message_read(reply, "(ssbtttso)", &name, &type, &ro_int, &crtime, &mtime, &usage, &state, NULL);
61d0578b
LP
519 if (r < 0)
520 return bus_log_parse_error(r);
521 if (r == 0)
522 break;
523
524 r = table_add_many(table,
525 TABLE_STRING, name,
526 TABLE_STRING, type);
527 if (r < 0)
528 return log_error_errno(r, "Failed to add row to table: %m");
529
530 ro_bool = ro_int;
531 r = table_add_cell(table, &cell, TABLE_BOOLEAN, &ro_bool);
532 if (r < 0)
533 return log_error_errno(r, "Failed to add row to table: %m");
534
535 if (ro_bool) {
536 r = table_set_color(table, cell, ansi_highlight_red());
537 if (r < 0)
538 return log_error_errno(r, "Failed to set table cell color: %m");
539 }
540
541 r = table_add_many(table,
542 TABLE_TIMESTAMP, crtime,
543 TABLE_TIMESTAMP, mtime,
544 TABLE_SIZE, usage);
545 if (r < 0)
546 return log_error_errno(r, "Failed to add row to table: %m");
547
548 r = table_add_cell(table, &cell, TABLE_STRING, state);
549 if (r < 0)
550 return log_error_errno(r, "Failed to add row to table: %m");
551
552 if (!streq(state, "detached")) {
553 r = table_set_color(table, cell, ansi_highlight_green());
554 if (r < 0)
555 return log_error_errno(r, "Failed to set table cell color: %m");
556 }
557 }
558
559 r = sd_bus_message_exit_container(reply);
560 if (r < 0)
561 return bus_log_parse_error(r);
562
563 if (table_get_rows(table) > 1) {
564 r = table_set_sort(table, (size_t) 0, (size_t) -1);
565 if (r < 0)
566 return log_error_errno(r, "Failed to sort table: %m");
567
568 table_set_header(table, arg_legend);
569
570 r = table_print(table, NULL);
571 if (r < 0)
572 return log_error_errno(r, "Failed to show table: %m");
573 }
574
575 if (arg_legend) {
576 if (table_get_rows(table) > 1)
577 printf("\n%zu images listed.\n", table_get_rows(table) - 1);
578 else
579 printf("No images.\n");
580 }
581
582 return 0;
583}
584
585static int remove_image(int argc, char *argv[], void *userdata) {
586 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
587 int r, i;
588
589 r = acquire_bus(&bus);
590 if (r < 0)
591 return r;
592
593 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
594
595 for (i = 1; i < argc; i++) {
596 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
597 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
598
599 r = sd_bus_message_new_method_call(
600 bus,
601 &m,
602 "org.freedesktop.portable1",
603 "/org/freedesktop/portable1",
604 "org.freedesktop.portable1.Manager",
605 "RemoveImage");
606 if (r < 0)
607 return bus_log_create_error(r);
608
609 r = sd_bus_message_append(m, "s", argv[i]);
610 if (r < 0)
611 return bus_log_create_error(r);
612
613 /* This is a slow operation, hence turn off any method call timeouts */
614 r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL);
615 if (r < 0)
616 return log_error_errno(r, "Could not remove image: %s", bus_error_message(&error, r));
617 }
618
619 return 0;
620}
621
622static int read_only_image(int argc, char *argv[], void *userdata) {
623 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
624 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
625 int b = true, r;
626
627 if (argc > 2) {
628 b = parse_boolean(argv[2]);
629 if (b < 0)
630 return log_error_errno(b, "Failed to parse boolean argument: %s", argv[2]);
631 }
632
633 r = acquire_bus(&bus);
634 if (r < 0)
635 return r;
636
637 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
638
639 r = sd_bus_call_method(
640 bus,
641 "org.freedesktop.portable1",
642 "/org/freedesktop/portable1",
643 "org.freedesktop.portable1.Manager",
644 "MarkImageReadOnly",
645 &error,
646 NULL,
647 "sb", argv[1], b);
648 if (r < 0)
649 return log_error_errno(r, "Could not mark image read-only: %s", bus_error_message(&error, r));
650
651 return 0;
652}
653
654static int set_limit(int argc, char *argv[], void *userdata) {
655 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
656 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
657 uint64_t limit;
658 int r;
659
660 r = acquire_bus(&bus);
661 if (r < 0)
662 return r;
663
664 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
665
666 if (STR_IN_SET(argv[argc-1], "-", "none", "infinity"))
667 limit = (uint64_t) -1;
668 else {
669 r = parse_size(argv[argc-1], 1024, &limit);
670 if (r < 0)
671 return log_error_errno(r, "Failed to parse size: %s", argv[argc-1]);
672 }
673
674 if (argc > 2)
675 /* With two arguments changes the quota limit of the specified image */
676 r = sd_bus_call_method(
677 bus,
678 "org.freedesktop.portable1",
679 "/org/freedesktop/portable1",
680 "org.freedesktop.portable1.Manager",
681 "SetImageLimit",
682 &error,
683 NULL,
684 "st", argv[1], limit);
685 else
686 /* With one argument changes the pool quota limit */
687 r = sd_bus_call_method(
688 bus,
689 "org.freedesktop.portable1",
690 "/org/freedesktop/portable1",
691 "org.freedesktop.portable1.Manager",
692 "SetPoolLimit",
693 &error,
694 NULL,
695 "t", limit);
696
697 if (r < 0)
698 return log_error_errno(r, "Could not set limit: %s", bus_error_message(&error, r));
699
700 return 0;
701}
702
703static int is_image_attached(int argc, char *argv[], void *userdata) {
704 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
705 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
706 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
707 _cleanup_free_ char *image = NULL;
708 const char *state;
709 int r;
710
711 r = determine_image(argv[1], true, &image);
712 if (r < 0)
713 return r;
714
715 r = acquire_bus(&bus);
716 if (r < 0)
717 return r;
718
719 r = sd_bus_call_method(
720 bus,
721 "org.freedesktop.portable1",
722 "/org/freedesktop/portable1",
723 "org.freedesktop.portable1.Manager",
724 "GetImageState",
725 &error,
726 &reply,
727 "s", image);
728 if (r < 0)
729 return log_error_errno(r, "Failed to get image state: %s", bus_error_message(&error, r));
730
731 r = sd_bus_message_read(reply, "s", &state);
732 if (r < 0)
733 return r;
734
735 if (!arg_quiet)
736 puts(state);
737
738 return streq(state, "detached");
739}
740
741static int dump_profiles(void) {
742 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
743 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
744 _cleanup_strv_free_ char **l = NULL;
61d0578b
LP
745 char **i;
746 int r;
747
748 r = acquire_bus(&bus);
749 if (r < 0)
750 return r;
751
752 r = sd_bus_get_property_strv(
753 bus,
754 "org.freedesktop.portable1",
755 "/org/freedesktop/portable1",
756 "org.freedesktop.portable1.Manager",
757 "Profiles",
758 &error,
759 &l);
760 if (r < 0)
761 return log_error_errno(r, "Failed to acquire list of profiles: %s", bus_error_message(&error, r));
762
763 if (arg_legend)
764 log_info("Available unit profiles:");
765
766 STRV_FOREACH(i, l) {
767 fputs(*i, stdout);
768 fputc('\n', stdout);
769 }
770
771 return 0;
772}
773
774static int help(int argc, char *argv[], void *userdata) {
37ec0fdd
LP
775 _cleanup_free_ char *link = NULL;
776 int r;
61d0578b 777
0221d68a 778 (void) pager_open(arg_pager_flags);
61d0578b 779
37ec0fdd
LP
780 r = terminal_urlify_man("portablectl", "1", &link);
781 if (r < 0)
782 return log_oom();
783
61d0578b
LP
784 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
785 "Attach or detach portable services from the local system.\n\n"
786 " -h --help Show this help\n"
787 " --version Show package version\n"
788 " --no-pager Do not pipe output into a pager\n"
789 " --no-legend Do not show the headers and footers\n"
790 " --no-ask-password Do not ask for system passwords\n"
791 " -H --host=[USER@]HOST Operate on remote host\n"
792 " -M --machine=CONTAINER Operate on local container\n"
793 " -q --quiet Suppress informational messages\n"
794 " -p --profile=PROFILE Pick security profile for portable service\n"
795 " --copy=copy|auto|symlink Prefer copying or symlinks if possible\n"
796 " --runtime Attach portable service until next reboot only\n"
797 " --no-reload Don't reload the system and service manager\n"
798 " --cat When inspecting include unit and os-release file\n"
799 " contents\n\n"
800 "Commands:\n"
801 " list List available portable service images\n"
802 " attach NAME|PATH [PREFIX...]\n"
803 " Attach the specified portable service image\n"
804 " detach NAME|PATH Detach the specified portable service image\n"
805 " inspect NAME|PATH [PREFIX...]\n"
806 " Show details of specified portable service image\n"
807 " is-attached NAME|PATH Query if portable service image is attached\n"
808 " read-only NAME|PATH [BOOL] Mark or unmark portable service image read-only\n"
809 " remove NAME|PATH... Remove a portable service image\n"
810 " set-limit [NAME|PATH] Set image or pool size limit (disk quota)\n"
37ec0fdd
LP
811 "\nSee the %s for details.\n"
812 , program_invocation_short_name
813 , link
814 );
61d0578b
LP
815
816 return 0;
817}
818
819static int parse_argv(int argc, char *argv[]) {
820
821 enum {
822 ARG_VERSION = 0x100,
823 ARG_NO_PAGER,
824 ARG_NO_LEGEND,
825 ARG_NO_ASK_PASSWORD,
826 ARG_COPY,
827 ARG_RUNTIME,
828 ARG_NO_RELOAD,
829 ARG_CAT,
830 };
831
832 static const struct option options[] = {
833 { "help", no_argument, NULL, 'h' },
834 { "version", no_argument, NULL, ARG_VERSION },
835 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
836 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
837 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
838 { "host", required_argument, NULL, 'H' },
839 { "machine", required_argument, NULL, 'M' },
840 { "quiet", no_argument, NULL, 'q' },
841 { "profile", required_argument, NULL, 'p' },
842 { "copy", required_argument, NULL, ARG_COPY },
843 { "runtime", no_argument, NULL, ARG_RUNTIME },
844 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
845 { "cat", no_argument, NULL, ARG_CAT },
846 {}
847 };
848
849 assert(argc >= 0);
850 assert(argv);
851
852 for (;;) {
853 int c;
854
855 c = getopt_long(argc, argv, "hH:M:qp:", options, NULL);
856 if (c < 0)
857 break;
858
859 switch (c) {
860
861 case 'h':
37ec0fdd 862 return help(0, NULL, NULL);
61d0578b
LP
863
864 case ARG_VERSION:
865 return version();
866
867 case ARG_NO_PAGER:
0221d68a 868 arg_pager_flags |= PAGER_DISABLE;
61d0578b
LP
869 break;
870
871 case ARG_NO_LEGEND:
872 arg_legend = false;
873 break;
874
875 case ARG_NO_ASK_PASSWORD:
876 arg_ask_password = false;
877 break;
878
879 case 'H':
880 arg_transport = BUS_TRANSPORT_REMOTE;
881 arg_host = optarg;
882 break;
883
884 case 'M':
885 arg_transport = BUS_TRANSPORT_MACHINE;
886 arg_host = optarg;
887 break;
888
889 case 'q':
890 arg_quiet = true;
891 break;
892
893 case 'p':
b3c66c44
LP
894 if (streq(optarg, "help"))
895 return dump_profiles();
896
baaa35ad
ZJS
897 if (!filename_is_valid(optarg))
898 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
899 "Unit profile name not valid: %s", optarg);
61d0578b 900
61d0578b
LP
901 arg_profile = optarg;
902 break;
903
904 case ARG_COPY:
905 if (streq(optarg, "auto"))
906 arg_copy_mode = NULL;
907 else if (STR_IN_SET(optarg, "copy", "symlink"))
908 arg_copy_mode = optarg;
f524522b
LP
909 else if (streq(optarg, "help")) {
910 puts("auto\n"
911 "copy\n"
912 "symlink");
913 return 0;
baaa35ad
ZJS
914 } else
915 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
916 "Failed to parse --copy= argument: %s", optarg);
61d0578b
LP
917
918 break;
919
920 case ARG_RUNTIME:
921 arg_runtime = true;
922 break;
923
924 case ARG_NO_RELOAD:
925 arg_reload = false;
926 break;
927
928 case ARG_CAT:
929 arg_cat = true;
930 break;
931
932 case '?':
933 return -EINVAL;
934
935 default:
936 assert_not_reached("Unhandled option");
937 }
938 }
939
940 return 1;
941}
942
c55a594e 943static int run(int argc, char *argv[]) {
61d0578b
LP
944 static const Verb verbs[] = {
945 { "help", VERB_ANY, VERB_ANY, 0, help },
946 { "list", VERB_ANY, 1, VERB_DEFAULT, list_images },
947 { "attach", 2, VERB_ANY, 0, attach_image },
948 { "detach", 2, 2, 0, detach_image },
949 { "inspect", 2, VERB_ANY, 0, inspect_image },
950 { "is-attached", 2, 2, 0, is_image_attached },
951 { "read-only", 2, 3, 0, read_only_image },
952 { "remove", 2, VERB_ANY, 0, remove_image },
953 { "set-limit", 3, 3, 0, set_limit },
954 {}
955 };
956
957 int r;
958
959 log_parse_environment();
960 log_open();
961
962 r = parse_argv(argc, argv);
963 if (r <= 0)
c55a594e 964 return r;
61d0578b 965
c55a594e 966 return dispatch_verb(argc, argv, verbs, NULL);
61d0578b 967}
c55a594e
ZJS
968
969DEFINE_MAIN_FUNCTION(run);