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