Bug Summary

File:build-scan/../src/machine/machinectl.c
Warning:line 2595, column 17
Although the value stored to 'r' is used in the enclosing expression, the value is never actually read from 'r'

Annotated Source Code

Press '?' to see keyboard shortcuts

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