Debian Codegraph @

src/portable/portablectl.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "ansi-color.h"
7 #include "build.h"
8 #include "bus-error.h"
9 #include "bus-locator.h"
10 #include "bus-unit-util.h"
11 #include "bus-util.h"
12 #include "bus-wait-for-jobs.h"
13 #include "chase.h"
14 #include "env-file.h"
15 #include "format-table.h"
16 #include "fs-util.h"
17 #include "glyph-util.h"
18 #include "help-util.h"
19 #include "install.h"
20 #include "main-func.h"
21 #include "options.h"
22 #include "os-util.h"
23 #include "pager.h"
24 #include "parse-argument.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "polkit-agent.h"
28 #include "portable.h"
29 #include "string-util.h"
30 #include "strv.h"
31 #include "verbs.h"
32
33 static PagerFlags arg_pager_flags = 0;
34 static bool arg_legend = true;
35 static bool arg_ask_password = true;
36 static bool arg_quiet = false;
37 static const char *arg_profile = "default";
38 static const char *arg_copy_mode = NULL;
39 static bool arg_runtime = false;
40 static bool arg_reload = true;
41 static bool arg_cat = false;
42 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
43 static const char *arg_host = NULL;
44 static bool arg_enable = false;
45 static bool arg_now = false;
46 static bool arg_no_block = false;
47 static char **arg_extension_images = NULL;
48 static bool arg_force = false;
49 static bool arg_clean = false;
50 static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
51
52 STATIC_DESTRUCTOR_REGISTER(arg_extension_images, strv_freep);
53
54 static bool is_portable_managed(const char *unit) {
55 return ENDSWITH_SET(unit, ".service", ".target", ".socket", ".path", ".timer");
56 }
57
58 static int determine_image(const char *image, bool permit_non_existing, char **ret) {
59 int r;
60
61 assert(ret);
62
63 /* If the specified name is a valid image name, we pass it as-is to portabled, which will search for it in the
64 * usual search directories. Otherwise we presume it's a path, and will normalize it on the client's side
65 * (among other things, to make the path independent of the client's working directory) before passing it
66 * over. */
67
68 if (image_name_is_valid(image)) {
69 char *c;
70
71 if (!arg_quiet && access_nofollow(image, F_OK) >= 0)
72 log_warning("Ambiguous invocation: current working directory contains file matching non-path argument '%s', ignoring. "
73 "Prefix argument with './' to force reference to file in current working directory.", image);
74
75 c = strdup(image);
76 if (!c)
77 return log_oom();
78
79 *ret = c;
80 return 0;
81 }
82
83 if (arg_transport != BUS_TRANSPORT_LOCAL)
84 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
85 "Operations on images by path not supported when connecting to remote systems.");
86
87 r = chase(image, NULL, CHASE_TRAIL_SLASH | (permit_non_existing ? CHASE_NONEXISTENT : 0), ret, NULL);
88 if (r < 0)
89 return log_error_errno(r, "Cannot normalize specified image path '%s': %m", image);
90
91 return 0;
92 }
93
94 static int attach_extensions_to_message(sd_bus_message *m, const char *method, char **extensions) {
95 int r;
96
97 assert(m);
98 assert(method);
99
100 /* The new methods also have flags parameters that are independent of the extensions */
101 if (strv_isempty(extensions) && !endswith(method, "WithExtensions"))
102 return 0;
103
104 r = sd_bus_message_open_container(m, 'a', "s");
105 if (r < 0)
106 return bus_log_create_error(r);
107
108 STRV_FOREACH(p, extensions) {
109 _cleanup_free_ char *resolved_extension_image = NULL;
110
111 r = determine_image(
112 *p,
113 startswith_strv(method, STRV_MAKE("Get", "Detach")),
114 &resolved_extension_image);
115 if (r < 0)
116 return r;
117
118 r = sd_bus_message_append(m, "s", resolved_extension_image);
119 if (r < 0)
120 return bus_log_create_error(r);
121 }
122
123 r = sd_bus_message_close_container(m);
124 if (r < 0)
125 return bus_log_create_error(r);
126
127 return 0;
128 }
129
130 static int extract_prefix(const char *path, char **ret) {
131 _cleanup_free_ char *name = NULL, *bn = NULL;
132 const char *underscore;
133 size_t m;
134 int r;
135
136 assert(ret);
137
138 r = path_extract_filename(path, &bn);
139 if (r < 0)
140 return r;
141
142 underscore = strchr(bn, '_');
143 if (underscore)
144 m = underscore - bn;
145 else {
146 const char *e;
147
148 e = ENDSWITH_SET(bn, ".raw.v", ".raw", ".v");
149 if (!e)
150 e = strchr(bn, 0);
151
152 m = e - bn;
153 }
154
155 name = strndup(bn, m);
156 if (!name)
157 return -ENOMEM;
158
159 /* A slightly reduced version of what's permitted in unit names. With ':' and '\' are removed, as
160 * well as '_' which we use as delimiter for the second part of the image string, which we ignore for
161 * now. */
162 if (!in_charset(name, ALPHANUMERICAL "-."))
163 return -EINVAL;
164
165 if (!filename_is_valid(name))
166 return -EINVAL;
167
168 *ret = TAKE_PTR(name);
169 return 0;
170 }
171
172 static int determine_matches(const char *image, char **l, bool allow_any, char ***ret) {
173 _cleanup_strv_free_ char **k = NULL;
174 int r;
175
176 /* Determine the matches to apply. If the list is empty we derive the match from the image name. If
177 * the list contains exactly the "-" we return a wildcard list (which is the empty list), but only if
178 * this is expressly permitted. */
179
180 if (strv_isempty(l)) {
181 char *prefix;
182
183 r = extract_prefix(image, &prefix);
184 if (r < 0)
185 return log_error_errno(r, "Failed to extract prefix of image name '%s': %m", image);
186
187 if (!arg_quiet)
188 log_info("(Matching unit files with prefix '%s'.)", prefix);
189
190 r = strv_consume(&k, prefix);
191 if (r < 0)
192 return log_oom();
193
194 } else if (strv_equal(l, STRV_MAKE("-"))) {
195
196 if (!allow_any)
197 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
198 "Refusing all unit file match.");
199
200 if (!arg_quiet)
201 log_info("(Matching all unit files.)");
202 } else {
203
204 k = strv_copy(l);
205 if (!k)
206 return log_oom();
207
208 if (!arg_quiet) {
209 _cleanup_free_ char *joined = NULL;
210
211 joined = strv_join(k, "', '");
212 if (!joined)
213 return log_oom();
214
215 log_info("(Matching unit files with prefixes '%s'.)", joined);
216 }
217 }
218
219 *ret = TAKE_PTR(k);
220
221 return 0;
222 }
223
224 static int acquire_bus(sd_bus **bus) {
225 int r;
226
227 assert(bus);
228
229 if (*bus)
230 return 0;
231
232 r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, bus);
233 if (r < 0)
234 return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
235
236 (void) sd_bus_set_allow_interactive_authorization(*bus, arg_ask_password);
237
238 return 0;
239 }
240
241 static int maybe_reload(sd_bus **bus) {
242 int r;
243
244 assert(bus);
245
246 if (!arg_reload)
247 return 0;
248
249 r = acquire_bus(bus);
250 if (r < 0)
251 return r;
252
253 return bus_service_manager_reload(*bus);
254 }
255
256 VERB_DEFAULT_NOARG(verb_list_images, "list",
257 "List available portable service images (default)");
258 static int verb_list_images(int argc, char *argv[], uintptr_t _data, void *userdata) {
259 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
260 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
261 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
262 _cleanup_(table_unrefp) Table *table = NULL;
263 int r;
264
265 r = acquire_bus(&bus);
266 if (r < 0)
267 return r;
268
269 r = bus_call_method(bus, bus_portable_mgr, "ListImages", &error, &reply, NULL);
270 if (r < 0)
271 return log_error_errno(r, "Failed to list images: %s", bus_error_message(&error, r));
272
273 table = table_new("name", "type", "ro", "crtime", "mtime", "usage", "state");
274 if (!table)
275 return log_oom();
276
277 r = sd_bus_message_enter_container(reply, 'a', "(ssbtttso)");
278 if (r < 0)
279 return bus_log_parse_error(r);
280
281 for (;;) {
282 const char *name, *type, *state;
283 uint64_t crtime, mtime, usage;
284 int ro_int;
285
286 r = sd_bus_message_read(reply, "(ssbtttso)", &name, &type, &ro_int, &crtime, &mtime, &usage, &state, NULL);
287 if (r < 0)
288 return bus_log_parse_error(r);
289 if (r == 0)
290 break;
291
292 r = table_add_many(table,
293 TABLE_STRING, name,
294 TABLE_STRING, type,
295 TABLE_BOOLEAN, ro_int,
296 TABLE_SET_COLOR, ro_int ? ansi_highlight_red() : NULL,
297 TABLE_TIMESTAMP, crtime,
298 TABLE_TIMESTAMP, mtime,
299 TABLE_SIZE, usage,
300 TABLE_STRING, state,
301 TABLE_SET_COLOR, !streq(state, "detached") ? ansi_highlight_green() : NULL);
302 if (r < 0)
303 return table_log_add_error(r);
304 }
305
306 r = sd_bus_message_exit_container(reply);
307 if (r < 0)
308 return bus_log_parse_error(r);
309
310 if (!table_isempty(table)) {
311 r = table_set_sort(table, (size_t) 0);
312 if (r < 0)
313 return table_log_sort_error(r);
314
315 table_set_header(table, arg_legend);
316
317 r = table_print_or_warn(table);
318 if (r < 0)
319 return r;
320 }
321
322 if (arg_legend) {
323 if (table_isempty(table))
324 printf("No images.\n");
325 else
326 printf("\n%zu images listed.\n", table_get_rows(table) - 1);
327 }
328
329 return 0;
330 }
331
332 static int get_image_metadata(sd_bus *bus, const char *image, char **matches, sd_bus_message **reply) {
333 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
334 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
335 uint64_t flags = arg_force ? PORTABLE_FORCE_EXTENSION : 0;
336 const char *method;
337 int r;
338
339 assert(bus);
340 assert(reply);
341
342 method = strv_isempty(arg_extension_images) && !arg_force ? "GetImageMetadata" : "GetImageMetadataWithExtensions";
343
344 r = bus_message_new_method_call(bus, &m, bus_portable_mgr, method);
345 if (r < 0)
346 return bus_log_create_error(r);
347
348 r = sd_bus_message_append(m, "s", image);
349 if (r < 0)
350 return bus_log_create_error(r);
351
352 r = attach_extensions_to_message(m, method, arg_extension_images);
353 if (r < 0)
354 return r;
355
356 r = sd_bus_message_append_strv(m, matches);
357 if (r < 0)
358 return bus_log_create_error(r);
359
360 if (streq(method, "GetImageMetadataWithExtensions")) {
361 r = sd_bus_message_append(m, "t", flags);
362 if (r < 0)
363 return bus_log_create_error(r);
364 }
365
366 r = sd_bus_call(bus, m, 0, &error, reply);
367 if (r < 0)
368 return log_error_errno(r, "Failed to inspect image metadata: %s", bus_error_message(&error, r));
369
370 return 0;
371 }
372
373 VERB(verb_inspect_image, "inspect", "NAME|PATH [PREFIX…]", 2, VERB_ANY, 0,
374 "Show details of specified portable service image");
375 static int verb_inspect_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
376 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
377 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
378 _cleanup_strv_free_ char **matches = NULL;
379 _cleanup_free_ char *image = NULL;
380 bool nl = false, header = false;
381 const char *path;
382 const void *data;
383 size_t sz;
384 int r;
385
386 r = determine_image(argv[1], false, &image);
387 if (r < 0)
388 return r;
389
390 r = determine_matches(argv[1], argv + 2, true, &matches);
391 if (r < 0)
392 return r;
393
394 r = acquire_bus(&bus);
395 if (r < 0)
396 return r;
397
398 r = get_image_metadata(bus, image, matches, &reply);
399 if (r < 0)
400 return r;
401
402 r = sd_bus_message_read(reply, "s", &path);
403 if (r < 0)
404 return bus_log_parse_error(r);
405
406 r = sd_bus_message_read_array(reply, 'y', &data, &sz);
407 if (r < 0)
408 return bus_log_parse_error(r);
409
410 pager_open(arg_pager_flags);
411
412 if (arg_cat) {
413 printf("%s-- OS Release: --%s\n", ansi_highlight(), ansi_normal());
414 fwrite(data, sz, 1, stdout);
415 fflush(stdout);
416 nl = true;
417 } else {
418 _cleanup_free_ char *pretty_portable = NULL, *pretty_os = NULL;
419
420 r = parse_env_data(
421 data, sz,
422 "/etc/os-release",
423 "PORTABLE_PRETTY_NAME", &pretty_portable,
424 "PRETTY_NAME", &pretty_os);
425 if (r < 0)
426 return log_error_errno(r, "Failed to parse /etc/os-release: %m");
427
428 printf("Image:\n\t%s\n"
429 "Portable Service:\n\t%s\n"
430 "Operating System:\n\t%s\n",
431 path,
432 strna(pretty_portable),
433 strna(pretty_os));
434 }
435
436 if (!strv_isempty(arg_extension_images)) {
437 /* If we specified any extensions, we'll first get back exactly the paths (and
438 * extension-release content) for each one of the arguments. */
439
440 r = sd_bus_message_enter_container(reply, 'a', "{say}");
441 if (r < 0)
442 return bus_log_parse_error(r);
443
444 for (size_t i = 0; i < strv_length(arg_extension_images); ++i) {
445 const char *name;
446
447 r = sd_bus_message_enter_container(reply, 'e', "say");
448 if (r < 0)
449 return bus_log_parse_error(r);
450 if (r == 0)
451 break;
452
453 r = sd_bus_message_read(reply, "s", &name);
454 if (r < 0)
455 return bus_log_parse_error(r);
456
457 r = sd_bus_message_read_array(reply, 'y', &data, &sz);
458 if (r < 0)
459 return bus_log_parse_error(r);
460
461 if (arg_cat) {
462 if (nl)
463 fputc('\n', stdout);
464
465 printf("%s-- Extension Release: %s --%s\n", ansi_highlight(), name, ansi_normal());
466 fwrite(data, sz, 1, stdout);
467 fflush(stdout);
468 nl = true;
469 } else {
470 _cleanup_free_ char *pretty_portable = NULL, *sysext_pretty_os = NULL,
471 *sysext_level = NULL, *sysext_id = NULL,
472 *sysext_version_id = NULL, *sysext_scope = NULL,
473 *portable_prefixes = NULL, *id = NULL, *version_id = NULL,
474 *sysext_image_id = NULL, *sysext_image_version = NULL,
475 *sysext_build_id = NULL, *confext_pretty_os = NULL,
476 *confext_level = NULL, *confext_id = NULL,
477 *confext_version_id = NULL, *confext_scope = NULL,
478 *confext_image_id = NULL, *confext_image_version = NULL,
479 *confext_build_id = NULL;
480
481 r = parse_env_data(
482 data, sz,
483 name,
484 "SYSEXT_ID", &sysext_id,
485 "SYSEXT_VERSION_ID", &sysext_version_id,
486 "SYSEXT_BUILD_ID", &sysext_build_id,
487 "SYSEXT_IMAGE_ID", &sysext_image_id,
488 "SYSEXT_IMAGE_VERSION", &sysext_image_version,
489 "SYSEXT_SCOPE", &sysext_scope,
490 "SYSEXT_LEVEL", &sysext_level,
491 "SYSEXT_PRETTY_NAME", &sysext_pretty_os,
492 "CONFEXT_ID", &confext_id,
493 "CONFEXT_VERSION_ID", &confext_version_id,
494 "CONFEXT_BUILD_ID", &confext_build_id,
495 "CONFEXT_IMAGE_ID", &confext_image_id,
496 "CONFEXT_IMAGE_VERSION", &confext_image_version,
497 "CONFEXT_SCOPE", &confext_scope,
498 "CONFEXT_LEVEL", &confext_level,
499 "CONFEXT_PRETTY_NAME", &confext_pretty_os,
500 "ID", &id,
501 "VERSION_ID", &version_id,
502 "PORTABLE_PRETTY_NAME", &pretty_portable,
503 "PORTABLE_PREFIXES", &portable_prefixes);
504 if (r < 0)
505 return log_error_errno(r, "Failed to parse extension release from '%s': %m", name);
506
507 printf("Extension:\n\t%s\n"
508 "\tExtension Scope:\n\t\t%s\n"
509 "\tExtension Compatibility Level:\n\t\t%s\n"
510 "\tExtension Compatibility OS:\n\t\t%s\n"
511 "\tExtension Compatibility OS Version:\n\t\t%s\n"
512 "\tPortable Service:\n\t\t%s\n"
513 "\tPortable Prefixes:\n\t\t%s\n"
514 "\tExtension Image:\n\t\t%s%s%s %s%s%s\n",
515 name,
516 strna(sysext_scope ?: confext_scope),
517 strna(sysext_level ?: confext_level),
518 strna(id),
519 strna(version_id),
520 strna(pretty_portable),
521 strna(portable_prefixes),
522 strempty(sysext_pretty_os ?: confext_pretty_os),
523 (sysext_pretty_os ?: confext_pretty_os) ? " (" : "ID: ",
524 strna(sysext_id ?: sysext_image_id ?: confext_id ?: confext_image_id),
525 (sysext_pretty_os ?: confext_pretty_os) ? "" : "Version: ",
526 strna(sysext_version_id ?: sysext_image_version ?: sysext_build_id ?: confext_version_id ?: confext_image_version ?: confext_build_id),
527 (sysext_pretty_os ?: confext_pretty_os) ? ")" : "");
528 }
529
530 r = sd_bus_message_exit_container(reply);
531 if (r < 0)
532 return bus_log_parse_error(r);
533 }
534
535 r = sd_bus_message_exit_container(reply);
536 if (r < 0)
537 return bus_log_parse_error(r);
538 }
539
540 r = sd_bus_message_enter_container(reply, 'a', "{say}");
541 if (r < 0)
542 return bus_log_parse_error(r);
543
544 for (;;) {
545 const char *name;
546
547 r = sd_bus_message_enter_container(reply, 'e', "say");
548 if (r < 0)
549 return bus_log_parse_error(r);
550 if (r == 0)
551 break;
552
553 r = sd_bus_message_read(reply, "s", &name);
554 if (r < 0)
555 return bus_log_parse_error(r);
556
557 r = sd_bus_message_read_array(reply, 'y', &data, &sz);
558 if (r < 0)
559 return bus_log_parse_error(r);
560
561 if (arg_cat) {
562 if (nl)
563 fputc('\n', stdout);
564
565 printf("%s-- Unit file: %s --%s\n", ansi_highlight(), name, ansi_normal());
566 fwrite(data, sz, 1, stdout);
567 fflush(stdout);
568 nl = true;
569 } else {
570 if (!header) {
571 fputs("Unit files:\n", stdout);
572 header = true;
573 }
574
575 fputc('\t', stdout);
576 fputs(name, stdout);
577 fputc('\n', stdout);
578 }
579
580 r = sd_bus_message_exit_container(reply);
581 if (r < 0)
582 return bus_log_parse_error(r);
583 }
584
585 r = sd_bus_message_exit_container(reply);
586 if (r < 0)
587 return bus_log_parse_error(r);
588
589 return 0;
590 }
591
592 static int print_changes(sd_bus_message *m) {
593 int r;
594
595 if (arg_quiet)
596 return 0;
597
598 r = sd_bus_message_enter_container(m, 'a', "(sss)");
599 if (r < 0)
600 return bus_log_parse_error(r);
601
602 for (;;) {
603 const char *type, *path, *source;
604
605 r = sd_bus_message_read(m, "(sss)", &type, &path, &source);
606 if (r < 0)
607 return bus_log_parse_error(r);
608 if (r == 0)
609 break;
610
611 if (streq(type, "symlink"))
612 log_info("Created symlink %s %s %s.", path, glyph(GLYPH_ARROW_RIGHT), source);
613 else if (streq(type, "copy")) {
614 if (isempty(source))
615 log_info("Copied %s.", path);
616 else
617 log_info("Copied %s %s %s.", source, glyph(GLYPH_ARROW_RIGHT), path);
618 } else if (streq(type, "unlink"))
619 log_info("Removed %s.", path);
620 else if (streq(type, "write"))
621 log_info("Written %s.", path);
622 else if (streq(type, "mkdir"))
623 log_info("Created directory %s.", path);
624 else
625 log_error("Unexpected change: %s/%s/%s", type, path, source);
626 }
627
628 r = sd_bus_message_exit_container(m);
629 if (r < 0)
630 return r;
631
632 return 0;
633 }
634
635 static int maybe_enable_disable(sd_bus *bus, const char *path, bool enable) {
636 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
637 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
638 _cleanup_strv_free_ char **names = NULL;
639 const uint64_t flags = UNIT_FILE_PORTABLE | (arg_runtime ? UNIT_FILE_RUNTIME : 0);
640 int r;
641
642 if (!arg_enable)
643 return 0;
644
645 names = strv_new(path, NULL);
646 if (!names)
647 return log_oom();
648
649 r = bus_message_new_method_call(
650 bus,
651 &m,
652 bus_systemd_mgr,
653 enable ? "EnableUnitFilesWithFlags" : "DisableUnitFilesWithFlags");
654 if (r < 0)
655 return bus_log_create_error(r);
656
657 r = sd_bus_message_append_strv(m, names);
658 if (r < 0)
659 return bus_log_create_error(r);
660
661 r = sd_bus_message_append(m, "t", flags);
662 if (r < 0)
663 return bus_log_create_error(r);
664
665 r = sd_bus_call(bus, m, 0, &error, &reply);
666 if (r < 0)
667 return log_error_errno(r, "Failed to %s the portable service %s: %s",
668 enable ? "enable" : "disable", path, bus_error_message(&error, r));
669
670 if (enable) {
671 r = sd_bus_message_skip(reply, "b");
672 if (r < 0)
673 return bus_log_parse_error(r);
674 }
675
676 (void) bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet);
677
678 return 0;
679 }
680
681 static int maybe_start_stop_restart(sd_bus *bus, const char *path, const char *method, BusWaitForJobs *wait) {
682 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
683 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
684 _cleanup_free_ char *name = NULL;
685 const char *job = NULL;
686 int r;
687
688 assert(STR_IN_SET(method, "StartUnit", "StopUnit", "RestartUnit"));
689
690 if (!arg_now)
691 return 0;
692
693 r = path_extract_filename(path, &name);
694 if (r < 0)
695 return log_error_errno(r, "Failed to extract file name from '%s': %m", path);
696
697 r = bus_call_method(
698 bus,
699 bus_systemd_mgr,
700 method,
701 &error,
702 &reply,
703 "ss", name, "replace");
704 if (r < 0)
705 return log_error_errno(r, "Failed to call %s on the portable service %s: %s",
706 method,
707 path,
708 bus_error_message(&error, r));
709
710 r = sd_bus_message_read(reply, "o", &job);
711 if (r < 0)
712 return bus_log_parse_error(r);
713
714 if (!arg_quiet)
715 log_info("Queued %s to call %s on portable service %s.", job, method, name);
716
717 if (wait) {
718 r = bus_wait_for_jobs_add(wait, job);
719 if (r < 0)
720 return log_error_errno(r, "Failed to watch %s job to call %s on %s: %m",
721 job, method, name);
722 }
723
724 return 0;
725 }
726
727 static int maybe_enable_start(sd_bus *bus, sd_bus_message *reply) {
728 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *wait = NULL;
729 int r;
730
731 if (!arg_enable && !arg_now)
732 return 0;
733
734 if (!arg_no_block) {
735 r = bus_wait_for_jobs_new(bus, &wait);
736 if (r < 0)
737 return log_error_errno(r, "Could not watch jobs: %m");
738 }
739
740 r = sd_bus_message_rewind(reply, true);
741 if (r < 0)
742 return r;
743 r = sd_bus_message_enter_container(reply, 'a', "(sss)");
744 if (r < 0)
745 return bus_log_parse_error(r);
746
747 for (;;) {
748 char *type, *path, *source;
749
750 r = sd_bus_message_read(reply, "(sss)", &type, &path, &source);
751 if (r < 0)
752 return bus_log_parse_error(r);
753 if (r == 0)
754 break;
755
756 if (STR_IN_SET(type, "symlink", "copy") && is_portable_managed(path)) {
757 (void) maybe_enable_disable(bus, path, true);
758 (void) maybe_start_stop_restart(bus, path, "StartUnit", wait);
759 }
760 }
761
762 r = sd_bus_message_exit_container(reply);
763 if (r < 0)
764 return r;
765
766 if (!arg_no_block) {
767 r = bus_wait_for_jobs(wait, arg_quiet, NULL);
768 if (r < 0)
769 return r;
770 }
771
772 return 0;
773 }
774
775 static int maybe_stop_enable_restart(sd_bus *bus, sd_bus_message *reply) {
776 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *wait = NULL;
777 int r;
778
779 if (!arg_enable && !arg_now)
780 return 0;
781
782 if (!arg_no_block) {
783 r = bus_wait_for_jobs_new(bus, &wait);
784 if (r < 0)
785 return log_error_errno(r, "Could not watch jobs: %m");
786 }
787
788 r = sd_bus_message_rewind(reply, true);
789 if (r < 0)
790 return r;
791
792 /* First we get a list of units that were definitely removed, not just re-attached,
793 * so we can also stop them if the user asked us to. */
794 r = sd_bus_message_enter_container(reply, 'a', "(sss)");
795 if (r < 0)
796 return bus_log_parse_error(r);
797
798 for (;;) {
799 char *type, *path, *source;
800
801 r = sd_bus_message_read(reply, "(sss)", &type, &path, &source);
802 if (r < 0)
803 return bus_log_parse_error(r);
804 if (r == 0)
805 break;
806
807 if (streq(type, "unlink") && is_portable_managed(path))
808 (void) maybe_start_stop_restart(bus, path, "StopUnit", wait);
809 }
810
811 r = sd_bus_message_exit_container(reply);
812 if (r < 0)
813 return r;
814
815 /* Then we get a list of units that were either added or changed, so that we can
816 * enable them and/or restart them if the user asked us to. */
817 r = sd_bus_message_enter_container(reply, 'a', "(sss)");
818 if (r < 0)
819 return bus_log_parse_error(r);
820
821 for (;;) {
822 char *type, *path, *source;
823
824 r = sd_bus_message_read(reply, "(sss)", &type, &path, &source);
825 if (r < 0)
826 return bus_log_parse_error(r);
827 if (r == 0)
828 break;
829
830 if (STR_IN_SET(type, "symlink", "copy") && is_portable_managed(path)) {
831 (void) maybe_enable_disable(bus, path, true);
832 (void) maybe_start_stop_restart(bus, path, "RestartUnit", wait);
833 }
834 }
835
836 r = sd_bus_message_exit_container(reply);
837 if (r < 0)
838 return r;
839
840 if (!arg_no_block) {
841 r = bus_wait_for_jobs(wait, arg_quiet, NULL);
842 if (r < 0)
843 return r;
844 }
845
846 return 0;
847 }
848
849 static int maybe_clean_units(sd_bus *bus, char **units) {
850 int r;
851
852 assert(bus);
853
854 if (!arg_clean)
855 return 0;
856
857 STRV_FOREACH(name, units) {
858 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
859 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
860
861 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "CleanUnit");
862 if (r < 0)
863 return bus_log_create_error(r);
864
865 r = sd_bus_message_append(m, "s", *name);
866 if (r < 0)
867 return bus_log_create_error(r);
868
869 r = sd_bus_message_append_strv(m, STRV_MAKE("all", "fdstore"));
870 if (r < 0)
871 return bus_log_create_error(r);
872
873 r = sd_bus_call(bus, m, 0, &error, NULL);
874 if (r < 0)
875 return log_error_errno(
876 r,
877 "Failed to call CleanUnit on portable service %s: %s",
878 *name,
879 bus_error_message(&error, r));
880 }
881
882 return 0;
883 }
884
885 static int maybe_stop_disable_clean(sd_bus *bus, char *image, char *argv[]) {
886 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *wait = NULL;
887 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
888 _cleanup_strv_free_ char **matches = NULL, **units = NULL;
889 int r;
890
891 if (!arg_enable && !arg_now && !arg_clean)
892 return 0;
893
894 r = determine_matches(argv[1], argv + 2, true, &matches);
895 if (r < 0)
896 return r;
897
898 r = bus_wait_for_jobs_new(bus, &wait);
899 if (r < 0)
900 return log_error_errno(r, "Could not watch jobs: %m");
901
902 r = get_image_metadata(bus, image, matches, &reply);
903 if (r < 0)
904 return r;
905
906 r = sd_bus_message_skip(reply, "say");
907 if (r < 0)
908 return bus_log_parse_error(r);
909
910 /* If we specified any extensions or --force (which makes the request go through the new
911 * WithExtensions calls), we'll first get an array of extension-release metadata. */
912 if (!strv_isempty(arg_extension_images) || arg_force) {
913 r = sd_bus_message_skip(reply, "a{say}");
914 if (r < 0)
915 return bus_log_parse_error(r);
916 }
917
918 r = sd_bus_message_enter_container(reply, 'a', "{say}");
919 if (r < 0)
920 return bus_log_parse_error(r);
921
922 for (;;) {
923 const char *name;
924
925 r = sd_bus_message_enter_container(reply, 'e', "say");
926 if (r < 0)
927 return bus_log_parse_error(r);
928 if (r == 0)
929 break;
930
931 r = sd_bus_message_read(reply, "s", &name);
932 if (r < 0)
933 return bus_log_parse_error(r);
934
935 r = sd_bus_message_skip(reply, "ay");
936 if (r < 0)
937 return bus_log_parse_error(r);
938
939 r = sd_bus_message_exit_container(reply);
940 if (r < 0)
941 return bus_log_parse_error(r);
942
943 (void) maybe_start_stop_restart(bus, name, "StopUnit", wait);
944 (void) maybe_enable_disable(bus, name, false);
945
946 r = strv_extend(&units, name);
947 if (r < 0)
948 return log_oom();
949 }
950
951 r = sd_bus_message_exit_container(reply);
952 if (r < 0)
953 return bus_log_parse_error(r);
954
955 /* Stopping must always block or the detach will fail if the unit is still running */
956 r = bus_wait_for_jobs(wait, arg_quiet, NULL);
957 if (r < 0)
958 return r;
959
960 /* Need to ensure all units are stopped before calling CleanUnit, as files might be in use. */
961 (void) maybe_clean_units(bus, units);
962
963 return 0;
964 }
965
966 static int attach_reattach_image(int argc, char *argv[], const char *method) {
967 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
968 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
969 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
970 _cleanup_strv_free_ char **matches = NULL;
971 _cleanup_free_ char *image = NULL;
972 int r;
973
974 assert(method);
975 assert(STR_IN_SET(method, "AttachImage", "ReattachImage", "AttachImageWithExtensions", "ReattachImageWithExtensions"));
976
977 r = determine_image(argv[1], false, &image);
978 if (r < 0)
979 return r;
980
981 r = determine_matches(argv[1], argv + 2, false, &matches);
982 if (r < 0)
983 return r;
984
985 r = acquire_bus(&bus);
986 if (r < 0)
987 return r;
988
989 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
990
991 r = bus_message_new_method_call(bus, &m, bus_portable_mgr, method);
992 if (r < 0)
993 return bus_log_create_error(r);
994
995 r = sd_bus_message_append(m, "s", image);
996 if (r < 0)
997 return bus_log_create_error(r);
998
999 r = attach_extensions_to_message(m, method, arg_extension_images);
1000 if (r < 0)
1001 return r;
1002
1003 r = sd_bus_message_append_strv(m, matches);
1004 if (r < 0)
1005 return bus_log_create_error(r);
1006
1007 r = sd_bus_message_append(m, "s", arg_profile);
1008 if (r < 0)
1009 return bus_log_create_error(r);
1010
1011 if (STR_IN_SET(method, "AttachImageWithExtensions", "ReattachImageWithExtensions")) {
1012 uint64_t flags = (arg_runtime ? PORTABLE_RUNTIME : 0) | (arg_force ? PORTABLE_FORCE_ATTACH | PORTABLE_FORCE_EXTENSION : 0);
1013
1014 r = sd_bus_message_append(m, "st", arg_copy_mode, flags);
1015 } else
1016 r = sd_bus_message_append(m, "bs", arg_runtime, arg_copy_mode);
1017 if (r < 0)
1018 return bus_log_create_error(r);
1019
1020 r = sd_bus_call(bus, m, 0, &error, &reply);
1021 if (r < 0)
1022 return log_error_errno(r, "%s failed: %s", method, bus_error_message(&error, r));
1023
1024 (void) maybe_reload(&bus);
1025
1026 print_changes(reply);
1027
1028 if (STR_IN_SET(method, "AttachImage", "AttachImageWithExtensions"))
1029 (void) maybe_enable_start(bus, reply);
1030 else {
1031 /* ReattachImage returns 2 lists - removed units first, and changed/added second */
1032 print_changes(reply);
1033 (void) maybe_stop_enable_restart(bus, reply);
1034 }
1035
1036 return 0;
1037 }
1038
1039 VERB(verb_attach_image, "attach", "NAME|PATH [PREFIX…]", 2, VERB_ANY, 0,
1040 "Attach the specified portable service image");
1041 static int verb_attach_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
1042 return attach_reattach_image(argc, argv, strv_isempty(arg_extension_images) && !arg_force ? "AttachImage" : "AttachImageWithExtensions");
1043 }
1044
1045 VERB(verb_detach_image, "detach", "NAME|PATH [PREFIX…]", 2, VERB_ANY, 0,
1046 "Detach the specified portable service image");
1047 static int verb_detach_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
1048 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
1049 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1050 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1051 _cleanup_free_ char *image = NULL;
1052 const char *method;
1053 int r;
1054
1055 r = determine_image(argv[1], true, &image);
1056 if (r < 0)
1057 return r;
1058
1059 r = acquire_bus(&bus);
1060 if (r < 0)
1061 return r;
1062
1063 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1064
1065 (void) maybe_stop_disable_clean(bus, image, argv);
1066
1067 method = strv_isempty(arg_extension_images) && !arg_force ? "DetachImage" : "DetachImageWithExtensions";
1068
1069 r = bus_message_new_method_call(bus, &m, bus_portable_mgr, method);
1070 if (r < 0)
1071 return bus_log_create_error(r);
1072
1073 r = sd_bus_message_append(m, "s", image);
1074 if (r < 0)
1075 return bus_log_create_error(r);
1076
1077 r = attach_extensions_to_message(m, method, arg_extension_images);
1078 if (r < 0)
1079 return r;
1080
1081 if (streq(method, "DetachImage"))
1082 r = sd_bus_message_append(m, "b", arg_runtime);
1083 else {
1084 uint64_t flags = (arg_runtime ? PORTABLE_RUNTIME : 0) | (arg_force ? PORTABLE_FORCE_ATTACH | PORTABLE_FORCE_EXTENSION : 0);
1085
1086 r = sd_bus_message_append(m, "t", flags);
1087 }
1088 if (r < 0)
1089 return bus_log_create_error(r);
1090
1091 r = sd_bus_call(bus, m, 0, &error, &reply);
1092 if (r < 0)
1093 return log_error_errno(r, "%s failed: %s", method, bus_error_message(&error, r));
1094
1095 (void) maybe_reload(&bus);
1096
1097 print_changes(reply);
1098 return 0;
1099 }
1100
1101 VERB(verb_reattach_image, "reattach", "NAME|PATH [PREFIX…]", 2, VERB_ANY, 0,
1102 "Reattach the specified portable service image");
1103 static int verb_reattach_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
1104 return attach_reattach_image(argc, argv, strv_isempty(arg_extension_images) && !arg_force ? "ReattachImage" : "ReattachImageWithExtensions");
1105 }
1106
1107 VERB(verb_is_image_attached, "is-attached", "NAME|PATH", 2, 2, 0,
1108 "Query if portable service image is attached");
1109 static int verb_is_image_attached(int argc, char *argv[], uintptr_t _data, void *userdata) {
1110 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
1111 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1112 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1113 _cleanup_free_ char *image = NULL;
1114 const char *state, *method;
1115 int r;
1116
1117 r = determine_image(argv[1], true, &image);
1118 if (r < 0)
1119 return r;
1120
1121 r = acquire_bus(&bus);
1122 if (r < 0)
1123 return r;
1124
1125 method = strv_isempty(arg_extension_images) ? "GetImageState" : "GetImageStateWithExtensions";
1126
1127 r = bus_message_new_method_call(bus, &m, bus_portable_mgr, method);
1128 if (r < 0)
1129 return bus_log_create_error(r);
1130
1131 r = sd_bus_message_append(m, "s", image);
1132 if (r < 0)
1133 return bus_log_create_error(r);
1134
1135 r = attach_extensions_to_message(m, method, arg_extension_images);
1136 if (r < 0)
1137 return r;
1138
1139 if (!strv_isempty(arg_extension_images)) {
1140 r = sd_bus_message_append(m, "t", UINT64_C(0));
1141 if (r < 0)
1142 return bus_log_create_error(r);
1143 }
1144
1145 r = sd_bus_call(bus, m, 0, &error, &reply);
1146 if (r < 0)
1147 return log_error_errno(r, "%s failed: %s", method, bus_error_message(&error, r));
1148
1149 r = sd_bus_message_read(reply, "s", &state);
1150 if (r < 0)
1151 return r;
1152
1153 if (!arg_quiet)
1154 puts(state);
1155
1156 return streq(state, "detached");
1157 }
1158
1159 VERB(verb_read_only_image, "read-only", "NAME|PATH [BOOL]", 2, 3, 0,
1160 "Mark or unmark portable service image read-only");
1161 static int verb_read_only_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
1162 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1163 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1164 int b = true, r;
1165
1166 if (argc > 2) {
1167 b = parse_boolean(argv[2]);
1168 if (b < 0)
1169 return log_error_errno(b, "Failed to parse boolean argument: %s", argv[2]);
1170 }
1171
1172 r = acquire_bus(&bus);
1173 if (r < 0)
1174 return r;
1175
1176 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1177
1178 r = bus_call_method(bus, bus_portable_mgr, "MarkImageReadOnly", &error, NULL, "sb", argv[1], b);
1179 if (r < 0)
1180 return log_error_errno(r, "Could not mark image read-only: %s", bus_error_message(&error, r));
1181
1182 return 0;
1183 }
1184
1185 VERB(verb_remove_image, "remove", "NAME|PATH…", 2, VERB_ANY, 0,
1186 "Remove a portable service image");
1187 static int verb_remove_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
1188 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1189 int r, i;
1190
1191 r = acquire_bus(&bus);
1192 if (r < 0)
1193 return r;
1194
1195 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1196
1197 for (i = 1; i < argc; i++) {
1198 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1199 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1200
1201 r = bus_message_new_method_call(bus, &m, bus_portable_mgr, "RemoveImage");
1202 if (r < 0)
1203 return bus_log_create_error(r);
1204
1205 r = sd_bus_message_append(m, "s", argv[i]);
1206 if (r < 0)
1207 return bus_log_create_error(r);
1208
1209 /* This is a slow operation, hence turn off any method call timeouts */
1210 r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL);
1211 if (r < 0)
1212 return log_error_errno(r, "Could not remove image: %s", bus_error_message(&error, r));
1213 }
1214
1215 return 0;
1216 }
1217
1218 VERB(verb_set_limit, "set-limit", "[NAME|PATH] LIMIT", 2, 3, 0,
1219 "Set image or pool size limit (disk quota)");
1220 static int verb_set_limit(int argc, char *argv[], uintptr_t _data, void *userdata) {
1221 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1222 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1223 uint64_t limit;
1224 int r;
1225
1226 r = acquire_bus(&bus);
1227 if (r < 0)
1228 return r;
1229
1230 (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1231
1232 if (STR_IN_SET(argv[argc-1], "-", "none", "infinity"))
1233 limit = UINT64_MAX;
1234 else {
1235 r = parse_size(argv[argc-1], 1024, &limit);
1236 if (r < 0)
1237 return log_error_errno(r, "Failed to parse size: %s", argv[argc-1]);
1238 }
1239
1240 if (argc > 2)
1241 /* With two arguments changes the quota limit of the specified image */
1242 r = bus_call_method(bus, bus_portable_mgr, "SetImageLimit", &error, NULL, "st", argv[1], limit);
1243 else
1244 /* With one argument changes the pool quota limit */
1245 r = bus_call_method(bus, bus_portable_mgr, "SetPoolLimit", &error, NULL, "t", limit);
1246
1247 if (r < 0)
1248 return log_error_errno(r, "Could not set limit: %s", bus_error_message(&error, r));
1249
1250 return 0;
1251 }
1252
1253 static int dump_profiles(void) {
1254 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1255 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1256 _cleanup_strv_free_ char **l = NULL;
1257 int r;
1258
1259 r = acquire_bus(&bus);
1260 if (r < 0)
1261 return r;
1262
1263 r = bus_get_property_strv(bus, bus_portable_mgr, "Profiles", &error, &l);
1264 if (r < 0)
1265 return log_error_errno(r, "Failed to acquire list of profiles: %s", bus_error_message(&error, r));
1266
1267 if (arg_legend)
1268 log_info("Available unit profiles:");
1269
1270 STRV_FOREACH(i, l) {
1271 fputs(*i, stdout);
1272 fputc('\n', stdout);
1273 }
1274
1275 return 0;
1276 }
1277
1278 static int help(void) {
1279 _cleanup_(table_unrefp) Table *verbs = NULL, *options = NULL;
1280 int r;
1281
1282 pager_open(arg_pager_flags);
1283
1284 r = verbs_get_help_table(&verbs);
1285 if (r < 0)
1286 return r;
1287
1288 r = option_parser_get_help_table(&options);
1289 if (r < 0)
1290 return r;
1291
1292 (void) table_sync_column_widths(0, verbs, options);
1293
1294 help_cmdline("[OPTIONS…] COMMAND …");
1295 help_abstract("Attach or detach portable services in the local system.");
1296
1297 help_section("Commands");
1298 r = table_print_or_warn(verbs);
1299 if (r < 0)
1300 return r;
1301
1302 help_section("Options");
1303 r = table_print_or_warn(options);
1304 if (r < 0)
1305 return r;
1306
1307 help_man_page_reference("portablectl", "1");
1308 return 0;
1309 }
1310
1311 VERB_COMMON_HELP_HIDDEN(help);
1312
1313 static int parse_argv(int argc, char *argv[], char ***remaining_args) {
1314 assert(argc >= 0);
1315 assert(argv);
1316 assert(remaining_args);
1317
1318 OptionParser opts = { argc, argv };
1319 int r;
1320
1321 FOREACH_OPTION_OR_RETURN(c, &opts)
1322 switch (c) {
1323
1324 OPTION_COMMON_HELP:
1325 return help();
1326
1327 OPTION_COMMON_VERSION:
1328 return version();
1329
1330 OPTION_COMMON_NO_PAGER:
1331 arg_pager_flags |= PAGER_DISABLE;
1332 break;
1333
1334 OPTION_COMMON_NO_LEGEND:
1335 arg_legend = false;
1336 break;
1337
1338 OPTION_COMMON_NO_ASK_PASSWORD:
1339 arg_ask_password = false;
1340 break;
1341
1342 OPTION_COMMON_HOST:
1343 arg_transport = BUS_TRANSPORT_REMOTE;
1344 arg_host = opts.arg;
1345 break;
1346
1347 OPTION_COMMON_MACHINE:
1348 r = parse_machine_argument(opts.arg, &arg_host, &arg_transport);
1349 if (r < 0)
1350 return r;
1351 break;
1352
1353 OPTION('q', "quiet", NULL, "Suppress informational messages"):
1354 arg_quiet = true;
1355 break;
1356
1357 OPTION('p', "profile", "PROFILE",
1358 "Pick security profile for portable service"):
1359 if (streq(opts.arg, "help"))
1360 return dump_profiles();
1361
1362 if (!filename_is_valid(opts.arg))
1363 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1364 "Unit profile name not valid: %s", opts.arg);
1365
1366 arg_profile = opts.arg;
1367 break;
1368
1369 OPTION_LONG("copy", "MODE",
1370 "Pick copying or symlinking of resources "
1371 "(copy, auto, symlink, mixed)"):
1372 if (streq(opts.arg, "auto"))
1373 arg_copy_mode = NULL;
1374 else if (STR_IN_SET(opts.arg, "copy", "symlink", "mixed"))
1375 arg_copy_mode = opts.arg;
1376 else if (streq(opts.arg, "help")) {
1377 puts("auto\n"
1378 "copy\n"
1379 "symlink\n"
1380 "mixed\n");
1381 return 0;
1382 } else
1383 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1384 "Failed to parse --copy= argument: %s", opts.arg);
1385 break;
1386
1387 OPTION_LONG("runtime", NULL,
1388 "Attach portable service until next reboot only"):
1389 arg_runtime = true;
1390 break;
1391
1392 OPTION_LONG("no-reload", NULL,
1393 "Don't reload the system and service manager"):
1394 arg_reload = false;
1395 break;
1396
1397 OPTION_LONG("cat", NULL,
1398 "When inspecting include unit and os-release file contents"):
1399 arg_cat = true;
1400 break;
1401
1402 OPTION_LONG("enable", NULL,
1403 "Immediately enable/disable the portable service after attach/detach"):
1404 arg_enable = true;
1405 break;
1406
1407 OPTION_LONG("now", NULL,
1408 "Immediately start/stop the portable service after attach/before detach"):
1409 arg_now = true;
1410 break;
1411
1412 OPTION_LONG("no-block", NULL,
1413 "Don't block waiting for attach --now to complete"):
1414 arg_no_block = true;
1415 break;
1416
1417 OPTION_LONG("extension", "PATH",
1418 "Extend the image with an overlay"):
1419 r = strv_extend(&arg_extension_images, opts.arg);
1420 if (r < 0)
1421 return log_oom();
1422 break;
1423
1424 OPTION_LONG("force", NULL,
1425 "Skip 'already active' check when attaching or detaching an image (with extensions)"):
1426 arg_force = true;
1427 break;
1428
1429 OPTION_LONG("clean", NULL,
1430 "When detaching, also remove configuration, state, "
1431 "cache, logs or runtime data of the portable service(s)"):
1432 arg_clean = true;
1433 break;
1434
1435 OPTION_LONG("user", NULL, /* help= */ NULL):
1436 arg_runtime_scope = RUNTIME_SCOPE_USER;
1437 break;
1438
1439 OPTION_LONG("system", NULL, /* help= */ NULL):
1440 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
1441 break;
1442 }
1443
1444 *remaining_args = option_parser_get_args(&opts);
1445 return 1;
1446 }
1447
1448 static int run(int argc, char *argv[]) {
1449 char **args = NULL;
1450 int r;
1451
1452 log_setup();
1453
1454 r = parse_argv(argc, argv, &args);
1455 if (r <= 0)
1456 return r;
1457
1458 return dispatch_verb(args, NULL);
1459 }
1460
1461 DEFINE_MAIN_FUNCTION(run);