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