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