File: | build-scan/../src/machine/machined-dbus.c |
Warning: | line 825, column 17 Value stored to 'result_fd' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | |
3 | #include <errno(*__errno_location ()).h> |
4 | #include <string.h> |
5 | #include <unistd.h> |
6 | |
7 | #include "sd-id128.h" |
8 | |
9 | #include "alloc-util.h" |
10 | #include "btrfs-util.h" |
11 | #include "bus-common-errors.h" |
12 | #include "bus-util.h" |
13 | #include "cgroup-util.h" |
14 | #include "fd-util.h" |
15 | #include "fileio.h" |
16 | #include "format-util.h" |
17 | #include "hostname-util.h" |
18 | #include "image-dbus.h" |
19 | #include "io-util.h" |
20 | #include "machine-dbus.h" |
21 | #include "machine-image.h" |
22 | #include "machine-pool.h" |
23 | #include "machined.h" |
24 | #include "path-util.h" |
25 | #include "process-util.h" |
26 | #include "stdio-util.h" |
27 | #include "strv.h" |
28 | #include "unit-name.h" |
29 | #include "user-util.h" |
30 | |
31 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_pool_path, "s", "/var/lib/machines")int property_get_pool_path(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply , void *userdata, sd_bus_error *error) { do { if ((__builtin_expect (!!(!(bus)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ( "bus"), "../src/machine/machined-dbus.c", 31, __PRETTY_FUNCTION__ ); } while (0); do { if ((__builtin_expect(!!(!(reply)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("reply"), "../src/machine/machined-dbus.c" , 31, __PRETTY_FUNCTION__); } while (0); return sd_bus_message_append (reply, "s", "/var/lib/machines"); }; |
32 | |
33 | static int property_get_pool_usage( |
34 | sd_bus *bus, |
35 | const char *path, |
36 | const char *interface, |
37 | const char *property, |
38 | sd_bus_message *reply, |
39 | void *userdata, |
40 | sd_bus_error *error) { |
41 | |
42 | _cleanup_close___attribute__((cleanup(closep))) int fd = -1; |
43 | uint64_t usage = (uint64_t) -1; |
44 | struct stat st; |
45 | |
46 | assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("bus"), "../src/machine/machined-dbus.c" , 46, __PRETTY_FUNCTION__); } while (0); |
47 | assert(reply)do { if ((__builtin_expect(!!(!(reply)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("reply"), "../src/machine/machined-dbus.c" , 47, __PRETTY_FUNCTION__); } while (0); |
48 | |
49 | /* We try to read the quota info from /var/lib/machines, as |
50 | * well as the usage of the loopback file |
51 | * /var/lib/machines.raw, and pick the larger value. */ |
52 | |
53 | fd = open("/var/lib/machines", O_RDONLY00|O_CLOEXEC02000000|O_DIRECTORY0200000); |
54 | if (fd >= 0) { |
55 | BtrfsQuotaInfo q; |
56 | |
57 | if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0) |
58 | usage = q.referenced; |
59 | } |
60 | |
61 | if (stat("/var/lib/machines.raw", &st) >= 0) { |
62 | if (usage == (uint64_t) -1 || st.st_blocks * 512ULL > usage) |
63 | usage = st.st_blocks * 512ULL; |
64 | } |
65 | |
66 | return sd_bus_message_append(reply, "t", usage); |
67 | } |
68 | |
69 | static int property_get_pool_limit( |
70 | sd_bus *bus, |
71 | const char *path, |
72 | const char *interface, |
73 | const char *property, |
74 | sd_bus_message *reply, |
75 | void *userdata, |
76 | sd_bus_error *error) { |
77 | |
78 | _cleanup_close___attribute__((cleanup(closep))) int fd = -1; |
79 | uint64_t size = (uint64_t) -1; |
80 | struct stat st; |
81 | |
82 | assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("bus"), "../src/machine/machined-dbus.c" , 82, __PRETTY_FUNCTION__); } while (0); |
83 | assert(reply)do { if ((__builtin_expect(!!(!(reply)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("reply"), "../src/machine/machined-dbus.c" , 83, __PRETTY_FUNCTION__); } while (0); |
84 | |
85 | /* We try to read the quota limit from /var/lib/machines, as |
86 | * well as the size of the loopback file |
87 | * /var/lib/machines.raw, and pick the smaller value. */ |
88 | |
89 | fd = open("/var/lib/machines", O_RDONLY00|O_CLOEXEC02000000|O_DIRECTORY0200000); |
90 | if (fd >= 0) { |
91 | BtrfsQuotaInfo q; |
92 | |
93 | if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0) |
94 | size = q.referenced_max; |
95 | } |
96 | |
97 | if (stat("/var/lib/machines.raw", &st) >= 0) { |
98 | if (size == (uint64_t) -1 || (uint64_t) st.st_size < size) |
99 | size = st.st_size; |
100 | } |
101 | |
102 | return sd_bus_message_append(reply, "t", size); |
103 | } |
104 | |
105 | static int method_get_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
106 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); |
107 | Manager *m = userdata; |
108 | Machine *machine; |
109 | const char *name; |
110 | int r; |
111 | |
112 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 112, __PRETTY_FUNCTION__); } while (0); |
113 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 113 , __PRETTY_FUNCTION__); } while (0); |
114 | |
115 | r = sd_bus_message_read(message, "s", &name); |
116 | if (r < 0) |
117 | return r; |
118 | |
119 | machine = hashmap_get(m->machines, name); |
120 | if (!machine) |
121 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE"org.freedesktop.machine1.NoSuchMachine", "No machine '%s' known", name); |
122 | |
123 | p = machine_bus_path(machine); |
124 | if (!p) |
125 | return -ENOMEM12; |
126 | |
127 | return sd_bus_reply_method_return(message, "o", p); |
128 | } |
129 | |
130 | static int method_get_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
131 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); |
132 | Manager *m = userdata; |
133 | const char *name; |
134 | int r; |
135 | |
136 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 136, __PRETTY_FUNCTION__); } while (0); |
137 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 137 , __PRETTY_FUNCTION__); } while (0); |
138 | |
139 | r = sd_bus_message_read(message, "s", &name); |
140 | if (r < 0) |
141 | return r; |
142 | |
143 | r = image_find(IMAGE_MACHINE, name, NULL((void*)0)); |
144 | if (r == -ENOENT2) |
145 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE"org.freedesktop.machine1.NoSuchImage", "No image '%s' known", name); |
146 | if (r < 0) |
147 | return r; |
148 | |
149 | p = image_bus_path(name); |
150 | if (!p) |
151 | return -ENOMEM12; |
152 | |
153 | return sd_bus_reply_method_return(message, "o", p); |
154 | } |
155 | |
156 | static int method_get_machine_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
157 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); |
158 | Manager *m = userdata; |
159 | Machine *machine = NULL((void*)0); |
160 | pid_t pid; |
161 | int r; |
162 | |
163 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 163, __PRETTY_FUNCTION__); } while (0); |
164 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 164 , __PRETTY_FUNCTION__); } while (0); |
165 | |
166 | assert_cc(sizeof(pid_t) == sizeof(uint32_t))GCC diagnostic push
; GCC diagnostic ignored "-Wdeclaration-after-statement" ; struct _assert_struct_17 { char x[(sizeof(pid_t) == sizeof (uint32_t)) ? 0 : -1]; }; GCC diagnostic pop ; |
167 | |
168 | r = sd_bus_message_read(message, "u", &pid); |
169 | if (r < 0) |
170 | return r; |
171 | |
172 | if (pid < 0) |
173 | return -EINVAL22; |
174 | |
175 | if (pid == 0) { |
176 | _cleanup_(sd_bus_creds_unrefp)__attribute__((cleanup(sd_bus_creds_unrefp))) sd_bus_creds *creds = NULL((void*)0); |
177 | |
178 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); |
179 | if (r < 0) |
180 | return r; |
181 | |
182 | r = sd_bus_creds_get_pid(creds, &pid); |
183 | if (r < 0) |
184 | return r; |
185 | } |
186 | |
187 | r = manager_get_machine_by_pid(m, pid, &machine); |
188 | if (r < 0) |
189 | return r; |
190 | if (!machine) |
191 | return sd_bus_error_setf(error, BUS_ERROR_NO_MACHINE_FOR_PID"org.freedesktop.machine1.NoMachineForPID", "PID "PID_FMT"%" "i"" does not belong to any known machine", pid); |
192 | |
193 | p = machine_bus_path(machine); |
194 | if (!p) |
195 | return -ENOMEM12; |
196 | |
197 | return sd_bus_reply_method_return(message, "o", p); |
198 | } |
199 | |
200 | static int method_list_machines(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
201 | _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0); |
202 | Manager *m = userdata; |
203 | Machine *machine; |
204 | Iterator i; |
205 | int r; |
206 | |
207 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 207, __PRETTY_FUNCTION__); } while (0); |
208 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 208 , __PRETTY_FUNCTION__); } while (0); |
209 | |
210 | r = sd_bus_message_new_method_return(message, &reply); |
211 | if (r < 0) |
212 | return sd_bus_error_set_errno(error, r); |
213 | |
214 | r = sd_bus_message_open_container(reply, 'a', "(ssso)"); |
215 | if (r < 0) |
216 | return sd_bus_error_set_errno(error, r); |
217 | |
218 | HASHMAP_FOREACH(machine, m->machines, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->machines), & (i), (void**)&(machine), ((void*)0)); ) { |
219 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); |
220 | |
221 | p = machine_bus_path(machine); |
222 | if (!p) |
223 | return -ENOMEM12; |
224 | |
225 | r = sd_bus_message_append(reply, "(ssso)", |
226 | machine->name, |
227 | strempty(machine_class_to_string(machine->class)), |
228 | machine->service, |
229 | p); |
230 | if (r < 0) |
231 | return sd_bus_error_set_errno(error, r); |
232 | } |
233 | |
234 | r = sd_bus_message_close_container(reply); |
235 | if (r < 0) |
236 | return sd_bus_error_set_errno(error, r); |
237 | |
238 | return sd_bus_send(NULL((void*)0), reply, NULL((void*)0)); |
239 | } |
240 | |
241 | static int method_create_or_register_machine(Manager *manager, sd_bus_message *message, bool_Bool read_network, Machine **_m, sd_bus_error *error) { |
242 | const char *name, *service, *class, *root_directory; |
243 | const int32_t *netif = NULL((void*)0); |
244 | MachineClass c; |
245 | uint32_t leader; |
246 | sd_id128_t id; |
247 | const void *v; |
248 | Machine *m; |
249 | size_t n, n_netif = 0; |
250 | int r; |
251 | |
252 | assert(manager)do { if ((__builtin_expect(!!(!(manager)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("manager"), "../src/machine/machined-dbus.c" , 252, __PRETTY_FUNCTION__); } while (0); |
253 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 253, __PRETTY_FUNCTION__); } while (0); |
254 | assert(_m)do { if ((__builtin_expect(!!(!(_m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("_m"), "../src/machine/machined-dbus.c", 254, __PRETTY_FUNCTION__); } while (0); |
255 | |
256 | r = sd_bus_message_read(message, "s", &name); |
257 | if (r < 0) |
258 | return r; |
259 | if (!machine_name_is_valid(name)hostname_is_valid(name, 0)) |
260 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid machine name"); |
261 | |
262 | r = sd_bus_message_read_array(message, 'y', &v, &n); |
263 | if (r < 0) |
264 | return r; |
265 | if (n == 0) |
266 | id = SD_ID128_NULL((const sd_id128_t) { .qwords = { 0, 0 }}); |
267 | else if (n == 16) |
268 | memcpy(&id, v, n); |
269 | else |
270 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid machine ID parameter"); |
271 | |
272 | r = sd_bus_message_read(message, "ssus", &service, &class, &leader, &root_directory); |
273 | if (r < 0) |
274 | return r; |
275 | |
276 | if (read_network) { |
277 | size_t i; |
278 | |
279 | r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif); |
280 | if (r < 0) |
281 | return r; |
282 | |
283 | n_netif /= sizeof(int32_t); |
284 | |
285 | for (i = 0; i < n_netif; i++) { |
286 | if (netif[i] <= 0) |
287 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid network interface index %i", netif[i]); |
288 | } |
289 | } |
290 | |
291 | if (isempty(class)) |
292 | c = _MACHINE_CLASS_INVALID; |
293 | else { |
294 | c = machine_class_from_string(class); |
295 | if (c < 0) |
296 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid machine class parameter"); |
297 | } |
298 | |
299 | if (leader == 1) |
300 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid leader PID"); |
301 | |
302 | if (!isempty(root_directory) && !path_is_absolute(root_directory)) |
303 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Root directory must be empty or an absolute path"); |
304 | |
305 | if (leader == 0) { |
306 | _cleanup_(sd_bus_creds_unrefp)__attribute__((cleanup(sd_bus_creds_unrefp))) sd_bus_creds *creds = NULL((void*)0); |
307 | |
308 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); |
309 | if (r < 0) |
310 | return r; |
311 | |
312 | assert_cc(sizeof(uint32_t) == sizeof(pid_t))GCC diagnostic push
; GCC diagnostic ignored "-Wdeclaration-after-statement" ; struct _assert_struct_18 { char x[(sizeof(uint32_t) == sizeof (pid_t)) ? 0 : -1]; }; GCC diagnostic pop ; |
313 | |
314 | r = sd_bus_creds_get_pid(creds, (pid_t*) &leader); |
315 | if (r < 0) |
316 | return r; |
317 | } |
318 | |
319 | if (hashmap_get(manager->machines, name)) |
320 | return sd_bus_error_setf(error, BUS_ERROR_MACHINE_EXISTS"org.freedesktop.machine1.MachineExists", "Machine '%s' already exists", name); |
321 | |
322 | r = manager_add_machine(manager, name, &m); |
323 | if (r < 0) |
324 | return r; |
325 | |
326 | m->leader = leader; |
327 | m->class = c; |
328 | m->id = id; |
329 | |
330 | if (!isempty(service)) { |
331 | m->service = strdup(service); |
332 | if (!m->service) { |
333 | r = -ENOMEM12; |
334 | goto fail; |
335 | } |
336 | } |
337 | |
338 | if (!isempty(root_directory)) { |
339 | m->root_directory = strdup(root_directory); |
340 | if (!m->root_directory) { |
341 | r = -ENOMEM12; |
342 | goto fail; |
343 | } |
344 | } |
345 | |
346 | if (n_netif > 0) { |
347 | assert_cc(sizeof(int32_t) == sizeof(int))GCC diagnostic push
; GCC diagnostic ignored "-Wdeclaration-after-statement" ; struct _assert_struct_19 { char x[(sizeof(int32_t) == sizeof (int)) ? 0 : -1]; }; GCC diagnostic pop ; |
348 | m->netif = memdup(netif, sizeof(int32_t) * n_netif); |
349 | if (!m->netif) { |
350 | r = -ENOMEM12; |
351 | goto fail; |
352 | } |
353 | |
354 | m->n_netif = n_netif; |
355 | } |
356 | |
357 | *_m = m; |
358 | |
359 | return 1; |
360 | |
361 | fail: |
362 | machine_add_to_gc_queue(m); |
363 | return r; |
364 | } |
365 | |
366 | static int method_create_machine_internal(sd_bus_message *message, bool_Bool read_network, void *userdata, sd_bus_error *error) { |
367 | Manager *manager = userdata; |
368 | Machine *m = NULL((void*)0); |
369 | int r; |
370 | |
371 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 371, __PRETTY_FUNCTION__); } while (0); |
372 | assert(manager)do { if ((__builtin_expect(!!(!(manager)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("manager"), "../src/machine/machined-dbus.c" , 372, __PRETTY_FUNCTION__); } while (0); |
373 | |
374 | r = method_create_or_register_machine(manager, message, read_network, &m, error); |
375 | if (r < 0) |
376 | return r; |
377 | |
378 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); |
379 | if (r < 0) |
380 | goto fail; |
381 | |
382 | r = machine_start(m, message, error); |
383 | if (r < 0) |
384 | goto fail; |
385 | |
386 | m->create_message = sd_bus_message_ref(message); |
387 | return 1; |
388 | |
389 | fail: |
390 | machine_add_to_gc_queue(m); |
391 | return r; |
392 | } |
393 | |
394 | static int method_create_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
395 | return method_create_machine_internal(message, true1, userdata, error); |
396 | } |
397 | |
398 | static int method_create_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
399 | return method_create_machine_internal(message, false0, userdata, error); |
400 | } |
401 | |
402 | static int method_register_machine_internal(sd_bus_message *message, bool_Bool read_network, void *userdata, sd_bus_error *error) { |
403 | Manager *manager = userdata; |
404 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); |
405 | Machine *m = NULL((void*)0); |
406 | int r; |
407 | |
408 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 408, __PRETTY_FUNCTION__); } while (0); |
409 | assert(manager)do { if ((__builtin_expect(!!(!(manager)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("manager"), "../src/machine/machined-dbus.c" , 409, __PRETTY_FUNCTION__); } while (0); |
410 | |
411 | r = method_create_or_register_machine(manager, message, read_network, &m, error); |
412 | if (r < 0) |
413 | return r; |
414 | |
415 | r = cg_pid_get_unit(m->leader, &m->unit); |
416 | if (r < 0) { |
417 | r = sd_bus_error_set_errnof(error, r, |
418 | "Failed to determine unit of process "PID_FMT"%" "i"" : %m", |
419 | m->leader); |
420 | goto fail; |
421 | } |
422 | |
423 | r = machine_start(m, NULL((void*)0), error); |
424 | if (r < 0) |
425 | goto fail; |
426 | |
427 | p = machine_bus_path(m); |
428 | if (!p) { |
429 | r = -ENOMEM12; |
430 | goto fail; |
431 | } |
432 | |
433 | return sd_bus_reply_method_return(message, "o", p); |
434 | |
435 | fail: |
436 | machine_add_to_gc_queue(m); |
437 | return r; |
438 | } |
439 | |
440 | static int method_register_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
441 | return method_register_machine_internal(message, true1, userdata, error); |
442 | } |
443 | |
444 | static int method_register_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
445 | return method_register_machine_internal(message, false0, userdata, error); |
446 | } |
447 | |
448 | static int redirect_method_to_machine(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) { |
449 | Machine *machine; |
450 | const char *name; |
451 | int r; |
452 | |
453 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 453, __PRETTY_FUNCTION__); } while (0); |
454 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 454 , __PRETTY_FUNCTION__); } while (0); |
455 | assert(method)do { if ((__builtin_expect(!!(!(method)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("method"), "../src/machine/machined-dbus.c" , 455, __PRETTY_FUNCTION__); } while (0); |
456 | |
457 | r = sd_bus_message_read(message, "s", &name); |
458 | if (r < 0) |
459 | return sd_bus_error_set_errno(error, r); |
460 | |
461 | machine = hashmap_get(m->machines, name); |
462 | if (!machine) |
463 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE"org.freedesktop.machine1.NoSuchMachine", "No machine '%s' known", name); |
464 | |
465 | return method(message, machine, error); |
466 | } |
467 | |
468 | static int method_terminate_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
469 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_terminate); |
470 | } |
471 | |
472 | static int method_kill_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
473 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_kill); |
474 | } |
475 | |
476 | static int method_get_machine_addresses(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
477 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_addresses); |
478 | } |
479 | |
480 | static int method_get_machine_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
481 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_os_release); |
482 | } |
483 | |
484 | static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
485 | _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0); |
486 | _cleanup_(image_hashmap_freep)__attribute__((cleanup(image_hashmap_freep))) Hashmap *images = NULL((void*)0); |
487 | Manager *m = userdata; |
488 | Image *image; |
489 | Iterator i; |
490 | int r; |
491 | |
492 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 492, __PRETTY_FUNCTION__); } while (0); |
493 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 493 , __PRETTY_FUNCTION__); } while (0); |
494 | |
495 | images = hashmap_new(&string_hash_ops)internal_hashmap_new(&string_hash_ops ); |
496 | if (!images) |
497 | return -ENOMEM12; |
498 | |
499 | r = image_discover(IMAGE_MACHINE, images); |
500 | if (r < 0) |
501 | return r; |
502 | |
503 | r = sd_bus_message_new_method_return(message, &reply); |
504 | if (r < 0) |
505 | return r; |
506 | |
507 | r = sd_bus_message_open_container(reply, 'a', "(ssbttto)"); |
508 | if (r < 0) |
509 | return r; |
510 | |
511 | HASHMAP_FOREACH(image, images, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((images), &(i), (void**)&(image), ((void*)0)); ) { |
512 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); |
513 | |
514 | p = image_bus_path(image->name); |
515 | if (!p) |
516 | return -ENOMEM12; |
517 | |
518 | r = sd_bus_message_append(reply, "(ssbttto)", |
519 | image->name, |
520 | image_type_to_string(image->type), |
521 | image->read_only, |
522 | image->crtime, |
523 | image->mtime, |
524 | image->usage, |
525 | p); |
526 | if (r < 0) |
527 | return r; |
528 | } |
529 | |
530 | r = sd_bus_message_close_container(reply); |
531 | if (r < 0) |
532 | return r; |
533 | |
534 | return sd_bus_send(NULL((void*)0), reply, NULL((void*)0)); |
535 | } |
536 | |
537 | static int method_open_machine_pty(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
538 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_pty); |
539 | } |
540 | |
541 | static int method_open_machine_login(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
542 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_login); |
543 | } |
544 | |
545 | static int method_open_machine_shell(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
546 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_shell); |
547 | } |
548 | |
549 | static int method_bind_mount_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
550 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_bind_mount); |
551 | } |
552 | |
553 | static int method_copy_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
554 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_copy); |
555 | } |
556 | |
557 | static int method_open_machine_root_directory(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
558 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_root_directory); |
559 | } |
560 | |
561 | static int method_get_machine_uid_shift(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
562 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_uid_shift); |
563 | } |
564 | |
565 | static int redirect_method_to_image(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) { |
566 | _cleanup_(image_unrefp)__attribute__((cleanup(image_unrefp))) Image* i = NULL((void*)0); |
567 | const char *name; |
568 | int r; |
569 | |
570 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 570, __PRETTY_FUNCTION__); } while (0); |
571 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 571 , __PRETTY_FUNCTION__); } while (0); |
572 | assert(method)do { if ((__builtin_expect(!!(!(method)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("method"), "../src/machine/machined-dbus.c" , 572, __PRETTY_FUNCTION__); } while (0); |
573 | |
574 | r = sd_bus_message_read(message, "s", &name); |
575 | if (r < 0) |
576 | return r; |
577 | |
578 | if (!image_name_is_valid(name)) |
579 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Image name '%s' is invalid.", name); |
580 | |
581 | r = image_find(IMAGE_MACHINE, name, &i); |
582 | if (r == -ENOENT2) |
583 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE"org.freedesktop.machine1.NoSuchImage", "No image '%s' known", name); |
584 | if (r < 0) |
585 | return r; |
586 | |
587 | i->userdata = m; |
588 | return method(message, i, error); |
589 | } |
590 | |
591 | static int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
592 | return redirect_method_to_image(message, userdata, error, bus_image_method_remove); |
593 | } |
594 | |
595 | static int method_rename_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
596 | return redirect_method_to_image(message, userdata, error, bus_image_method_rename); |
597 | } |
598 | |
599 | static int method_clone_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
600 | return redirect_method_to_image(message, userdata, error, bus_image_method_clone); |
601 | } |
602 | |
603 | static int method_mark_image_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
604 | return redirect_method_to_image(message, userdata, error, bus_image_method_mark_read_only); |
605 | } |
606 | |
607 | static int method_get_image_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
608 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_hostname); |
609 | } |
610 | |
611 | static int method_get_image_machine_id(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
612 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_machine_id); |
613 | } |
614 | |
615 | static int method_get_image_machine_info(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
616 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_machine_info); |
617 | } |
618 | |
619 | static int method_get_image_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
620 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_os_release); |
621 | } |
622 | |
623 | static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) { |
624 | _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0); |
625 | _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *f = NULL((void*)0); |
626 | bool_Bool success; |
627 | size_t n; |
628 | int r; |
629 | |
630 | assert(operation)do { if ((__builtin_expect(!!(!(operation)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("operation"), "../src/machine/machined-dbus.c" , 630, __PRETTY_FUNCTION__); } while (0); |
631 | assert(operation->extra_fd >= 0)do { if ((__builtin_expect(!!(!(operation->extra_fd >= 0 )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("operation->extra_fd >= 0" ), "../src/machine/machined-dbus.c", 631, __PRETTY_FUNCTION__ ); } while (0); |
632 | |
633 | if (lseek(operation->extra_fd, 0, SEEK_SET0) == (off_t) -1) |
634 | return -errno(*__errno_location ()); |
635 | |
636 | f = fdopen(operation->extra_fd, "re"); |
637 | if (!f) |
638 | return -errno(*__errno_location ()); |
639 | |
640 | operation->extra_fd = -1; |
641 | |
642 | /* The resulting temporary file starts with a boolean value that indicates success or not. */ |
643 | errno(*__errno_location ()) = 0; |
644 | n = fread(&success, 1, sizeof(success), f); |
645 | if (n != sizeof(success)) |
646 | return ret < 0 ? ret : (errno(*__errno_location ()) != 0 ? -errno(*__errno_location ()) : -EIO5); |
647 | |
648 | if (ret < 0) { |
649 | _cleanup_free___attribute__((cleanup(freep))) char *name = NULL((void*)0); |
650 | |
651 | /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean |
652 | * set to false followed by the name of the failed image. Let's try to read this and use it for the |
653 | * error message. If we can't read it, don't mind, and return the naked error. */ |
654 | |
655 | if (success) /* The resulting temporary file could not be updated, ignore it. */ |
656 | return ret; |
657 | |
658 | r = read_nul_string(f, &name); |
659 | if (r < 0 || isempty(name)) /* Same here... */ |
660 | return ret; |
661 | |
662 | return sd_bus_error_set_errnof(error, ret, "Failed to remove image %s: %m", name); |
663 | } |
664 | |
665 | assert(success)do { if ((__builtin_expect(!!(!(success)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("success"), "../src/machine/machined-dbus.c" , 665, __PRETTY_FUNCTION__); } while (0); |
666 | |
667 | r = sd_bus_message_new_method_return(operation->message, &reply); |
668 | if (r < 0) |
669 | return r; |
670 | |
671 | r = sd_bus_message_open_container(reply, 'a', "(st)"); |
672 | if (r < 0) |
673 | return r; |
674 | |
675 | /* On success the resulting temporary file will contain a list of image names that were removed followed by |
676 | * their size on disk. Let's read that and turn it into a bus message. */ |
677 | for (;;) { |
678 | _cleanup_free___attribute__((cleanup(freep))) char *name = NULL((void*)0); |
679 | uint64_t size; |
680 | |
681 | r = read_nul_string(f, &name); |
682 | if (r < 0) |
683 | return r; |
684 | if (isempty(name)) /* reached the end */ |
685 | break; |
686 | |
687 | errno(*__errno_location ()) = 0; |
688 | n = fread(&size, 1, sizeof(size), f); |
689 | if (n != sizeof(size)) |
690 | return errno(*__errno_location ()) != 0 ? -errno(*__errno_location ()) : -EIO5; |
691 | |
692 | r = sd_bus_message_append(reply, "(st)", name, size); |
693 | if (r < 0) |
694 | return r; |
695 | } |
696 | |
697 | r = sd_bus_message_close_container(reply); |
698 | if (r < 0) |
699 | return r; |
700 | |
701 | return sd_bus_send(NULL((void*)0), reply, NULL((void*)0)); |
702 | } |
703 | |
704 | static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
705 | enum { |
706 | REMOVE_ALL, |
707 | REMOVE_HIDDEN, |
708 | } mode; |
709 | |
710 | _cleanup_close_pair___attribute__((cleanup(close_pairp))) int errno_pipe_fd[2] = { -1, -1 }; |
711 | _cleanup_close___attribute__((cleanup(closep))) int result_fd = -1; |
712 | Manager *m = userdata; |
713 | Operation *operation; |
714 | const char *mm; |
715 | pid_t child; |
716 | int r; |
717 | |
718 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 718, __PRETTY_FUNCTION__); } while (0); |
719 | |
720 | if (m->n_operations >= OPERATIONS_MAX64) |
721 | return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED"org.freedesktop.DBus.Error.LimitsExceeded", "Too many ongoing operations."); |
722 | |
723 | r = sd_bus_message_read(message, "s", &mm); |
724 | if (r < 0) |
725 | return r; |
726 | |
727 | if (streq(mm, "all")(strcmp((mm),("all")) == 0)) |
728 | mode = REMOVE_ALL; |
729 | else if (streq(mm, "hidden")(strcmp((mm),("hidden")) == 0)) |
730 | mode = REMOVE_HIDDEN; |
731 | else |
732 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Unknown mode '%s'.", mm); |
733 | |
734 | r = bus_verify_polkit_async( |
735 | message, |
736 | CAP_SYS_ADMIN21, |
737 | "org.freedesktop.machine1.manage-machines", |
738 | NULL((void*)0), |
739 | false0, |
740 | UID_INVALID((uid_t) -1), |
741 | &m->polkit_registry, |
742 | error); |
743 | if (r < 0) |
744 | return r; |
745 | if (r == 0) |
746 | return 1; /* Will call us back */ |
747 | |
748 | if (pipe2(errno_pipe_fd, O_CLOEXEC02000000|O_NONBLOCK04000) < 0) |
749 | return sd_bus_error_set_errnof(error, errno(*__errno_location ()), "Failed to create pipe: %m"); |
750 | |
751 | /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this |
752 | * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this |
753 | * continuously */ |
754 | result_fd = open_tmpfile_unlinkable(NULL((void*)0), O_RDWR02|O_CLOEXEC02000000); |
755 | if (result_fd < 0) |
756 | return -errno(*__errno_location ()); |
757 | |
758 | /* This might be a slow operation, run it asynchronously in a background process */ |
759 | r = safe_fork("(sd-clean)", FORK_RESET_SIGNALS, &child); |
760 | if (r < 0) |
761 | return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m"); |
762 | if (r == 0) { |
763 | _cleanup_(image_hashmap_freep)__attribute__((cleanup(image_hashmap_freep))) Hashmap *images = NULL((void*)0); |
764 | bool_Bool success = true1; |
765 | Image *image; |
766 | Iterator i; |
767 | ssize_t l; |
768 | |
769 | errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); |
770 | |
771 | images = hashmap_new(&string_hash_ops)internal_hashmap_new(&string_hash_ops ); |
772 | if (!images) { |
773 | r = -ENOMEM12; |
774 | goto child_fail; |
775 | } |
776 | |
777 | r = image_discover(IMAGE_MACHINE, images); |
778 | if (r < 0) |
779 | goto child_fail; |
780 | |
781 | l = write(result_fd, &success, sizeof(success)); |
782 | if (l < 0) { |
783 | r = -errno(*__errno_location ()); |
784 | goto child_fail; |
785 | } |
786 | |
787 | HASHMAP_FOREACH(image, images, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((images), &(i), (void**)&(image), ((void*)0)); ) { |
788 | |
789 | /* We can't remove vendor images (i.e. those in /usr) */ |
790 | if (IMAGE_IS_VENDOR(image)) |
791 | continue; |
792 | |
793 | if (IMAGE_IS_HOST(image)) |
794 | continue; |
795 | |
796 | if (mode == REMOVE_HIDDEN && !IMAGE_IS_HIDDEN(image)) |
797 | continue; |
798 | |
799 | r = image_remove(image); |
800 | if (r == -EBUSY16) /* keep images that are currently being used. */ |
801 | continue; |
802 | if (r < 0) { |
803 | /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */ |
804 | success = false0; |
805 | (void) ftruncate(result_fd, 0); |
806 | (void) lseek(result_fd, 0, SEEK_SET0); |
807 | (void) write(result_fd, &success, sizeof(success)); |
808 | (void) write(result_fd, image->name, strlen(image->name)+1); |
809 | goto child_fail; |
810 | } |
811 | |
812 | l = write(result_fd, image->name, strlen(image->name)+1); |
813 | if (l < 0) { |
814 | r = -errno(*__errno_location ()); |
815 | goto child_fail; |
816 | } |
817 | |
818 | l = write(result_fd, &image->usage_exclusive, sizeof(image->usage_exclusive)); |
819 | if (l < 0) { |
820 | r = -errno(*__errno_location ()); |
821 | goto child_fail; |
822 | } |
823 | } |
824 | |
825 | result_fd = safe_close(result_fd); |
Value stored to 'result_fd' is never read | |
826 | _exit(EXIT_SUCCESS0); |
827 | |
828 | child_fail: |
829 | (void) write(errno_pipe_fd[1], &r, sizeof(r)); |
830 | _exit(EXIT_FAILURE1); |
831 | } |
832 | |
833 | errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
834 | |
835 | /* The clean-up might take a while, hence install a watch on the child and return */ |
836 | |
837 | r = operation_new(m, NULL((void*)0), child, message, errno_pipe_fd[0], &operation); |
838 | if (r < 0) { |
839 | (void) sigkill_wait(child); |
840 | return r; |
841 | } |
842 | |
843 | operation->extra_fd = result_fd; |
844 | operation->done = clean_pool_done; |
845 | |
846 | result_fd = -1; |
847 | errno_pipe_fd[0] = -1; |
848 | |
849 | return 1; |
850 | } |
851 | |
852 | static int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
853 | Manager *m = userdata; |
854 | uint64_t limit; |
855 | int r; |
856 | |
857 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 857, __PRETTY_FUNCTION__); } while (0); |
858 | |
859 | r = sd_bus_message_read(message, "t", &limit); |
860 | if (r < 0) |
861 | return r; |
862 | if (!FILE_SIZE_VALID_OR_INFINITY(limit)) |
863 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "New limit out of range"); |
864 | |
865 | r = bus_verify_polkit_async( |
866 | message, |
867 | CAP_SYS_ADMIN21, |
868 | "org.freedesktop.machine1.manage-machines", |
869 | NULL((void*)0), |
870 | false0, |
871 | UID_INVALID((uid_t) -1), |
872 | &m->polkit_registry, |
873 | error); |
874 | if (r < 0) |
875 | return r; |
876 | if (r == 0) |
877 | return 1; /* Will call us back */ |
878 | |
879 | /* Set up the machine directory if necessary */ |
880 | r = setup_machine_directory(limit, error); |
881 | if (r < 0) |
882 | return r; |
883 | |
884 | /* Resize the backing loopback device, if there is one, except if we asked to drop any limit */ |
885 | if (limit != (uint64_t) -1) { |
886 | r = btrfs_resize_loopback("/var/lib/machines", limit, false0); |
887 | if (r == -ENOTTY25) |
888 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED"org.freedesktop.DBus.Error.NotSupported", "Quota is only supported on btrfs."); |
889 | if (r < 0 && r != -ENODEV19) /* ignore ENODEV, as that's what is returned if the file system is not on loopback */ |
890 | return sd_bus_error_set_errnof(error, r, "Failed to adjust loopback limit: %m"); |
891 | } |
892 | |
893 | (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit); |
894 | |
895 | r = btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit); |
896 | if (r == -ENOTTY25) |
897 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED"org.freedesktop.DBus.Error.NotSupported", "Quota is only supported on btrfs."); |
898 | if (r < 0) |
899 | return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m"); |
900 | |
901 | return sd_bus_reply_method_return(message, NULL((void*)0)); |
902 | } |
903 | |
904 | static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
905 | return redirect_method_to_image(message, userdata, error, bus_image_method_set_limit); |
906 | } |
907 | |
908 | static int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
909 | _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *f = NULL((void*)0); |
910 | Manager *m = userdata; |
911 | const char *name, *p; |
912 | Machine *machine; |
913 | uint32_t uid; |
914 | int r; |
915 | |
916 | r = sd_bus_message_read(message, "su", &name, &uid); |
917 | if (r < 0) |
918 | return r; |
919 | |
920 | if (!uid_is_valid(uid)) |
921 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid user ID " UID_FMT"%" "u", uid); |
922 | |
923 | machine = hashmap_get(m->machines, name); |
924 | if (!machine) |
925 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE"org.freedesktop.machine1.NoSuchMachine", "No machine '%s' known", name); |
926 | |
927 | if (machine->class != MACHINE_CONTAINER) |
928 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Not supported for non-container machines."); |
929 | |
930 | p = procfs_file_alloca(machine->leader, "uid_map")({ pid_t _pid_ = (machine->leader); const char *_r_; if (_pid_ == 0) { _r_ = ("/proc/self/" "uid_map"); } else { _r_ = __builtin_alloca ((sizeof("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 : sizeof(pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof (pid_t) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)]) )) + 1 + sizeof("uid_map")); sprintf((char*) _r_, "/proc/""%" "i""/" "uid_map", _pid_); } _r_; }); |
931 | f = fopen(p, "re"); |
932 | if (!f) |
933 | return -errno(*__errno_location ()); |
934 | |
935 | for (;;) { |
936 | uid_t uid_base, uid_shift, uid_range, converted; |
937 | int k; |
938 | |
939 | errno(*__errno_location ()) = 0; |
940 | k = fscanf(f, UID_FMT"%" "u" " " UID_FMT"%" "u" " " UID_FMT"%" "u", &uid_base, &uid_shift, &uid_range); |
941 | if (k < 0 && feof(f)) |
942 | break; |
943 | if (k != 3) { |
944 | if (ferror(f) && errno(*__errno_location ()) > 0) |
945 | return -errno(*__errno_location ()); |
946 | |
947 | return -EIO5; |
948 | } |
949 | |
950 | if (uid < uid_base || uid >= uid_base + uid_range) |
951 | continue; |
952 | |
953 | converted = uid - uid_base + uid_shift; |
954 | if (!uid_is_valid(converted)) |
955 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid user ID " UID_FMT"%" "u", uid); |
956 | |
957 | return sd_bus_reply_method_return(message, "u", (uint32_t) converted); |
958 | } |
959 | |
960 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING"org.freedesktop.machine1.NoSuchUserMapping", "Machine '%s' has no matching user mappings.", name); |
961 | } |
962 | |
963 | static int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
964 | Manager *m = userdata; |
965 | Machine *machine; |
966 | uid_t uid; |
967 | Iterator i; |
968 | int r; |
969 | |
970 | r = sd_bus_message_read(message, "u", &uid); |
971 | if (r < 0) |
972 | return r; |
973 | if (!uid_is_valid(uid)) |
974 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid user ID " UID_FMT"%" "u", uid); |
975 | if (uid < 0x10000) |
976 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING"org.freedesktop.machine1.NoSuchUserMapping", "User " UID_FMT"%" "u" " belongs to host UID range", uid); |
977 | |
978 | HASHMAP_FOREACH(machine, m->machines, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->machines), & (i), (void**)&(machine), ((void*)0)); ) { |
979 | _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *f = NULL((void*)0); |
980 | char p[STRLEN("/proc//uid_map")(sizeof("""/proc//uid_map""") - 1) + DECIMAL_STR_MAX(pid_t)(2+(sizeof(pid_t) <= 1 ? 3 : sizeof(pid_t) <= 2 ? 5 : sizeof (pid_t) <= 4 ? 10 : sizeof(pid_t) <= 8 ? 20 : sizeof(int [-2*(sizeof(pid_t) > 8)]))) + 1]; |
981 | |
982 | if (machine->class != MACHINE_CONTAINER) |
983 | continue; |
984 | |
985 | xsprintf(p, "/proc/" UID_FMT "/uid_map", machine->leader)do { if ((__builtin_expect(!!(!(((size_t) snprintf(p, __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (p), typeof(&*(p))), sizeof(p)/sizeof((p)[0]), ((void)0)) ), "/proc/" "%" "u" "/uid_map", machine->leader) < (__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (p), typeof(&*(p))), sizeof(p)/sizeof((p)[0]), ((void)0)) ))))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("xsprintf: " "p" "[] must be big enough"), "../src/machine/machined-dbus.c" , 985, __PRETTY_FUNCTION__); } while (0); |
986 | f = fopen(p, "re"); |
987 | if (!f) { |
988 | log_warning_errno(errno, "Failed to open %s, ignoring,", p)({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/machine/machined-dbus.c", 988, __func__ , "Failed to open %s, ignoring,", p) : -abs(_e); }); |
989 | continue; |
990 | } |
991 | |
992 | for (;;) { |
993 | _cleanup_free___attribute__((cleanup(freep))) char *o = NULL((void*)0); |
994 | uid_t uid_base, uid_shift, uid_range, converted; |
995 | int k; |
996 | |
997 | errno(*__errno_location ()) = 0; |
998 | k = fscanf(f, UID_FMT"%" "u" " " UID_FMT"%" "u" " " UID_FMT"%" "u", &uid_base, &uid_shift, &uid_range); |
999 | if (k < 0 && feof(f)) |
1000 | break; |
1001 | if (k != 3) { |
1002 | if (ferror(f) && errno(*__errno_location ()) > 0) |
1003 | return -errno(*__errno_location ()); |
1004 | |
1005 | return -EIO5; |
1006 | } |
1007 | |
1008 | /* The private user namespace is disabled, ignoring. */ |
1009 | if (uid_shift == 0) |
1010 | continue; |
1011 | |
1012 | if (uid < uid_shift || uid >= uid_shift + uid_range) |
1013 | continue; |
1014 | |
1015 | converted = (uid - uid_shift + uid_base); |
1016 | if (!uid_is_valid(converted)) |
1017 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid user ID " UID_FMT"%" "u", uid); |
1018 | |
1019 | o = machine_bus_path(machine); |
1020 | if (!o) |
1021 | return -ENOMEM12; |
1022 | |
1023 | return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted); |
1024 | } |
1025 | } |
1026 | |
1027 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING"org.freedesktop.machine1.NoSuchUserMapping", "No matching user mapping for " UID_FMT"%" "u" ".", uid); |
1028 | } |
1029 | |
1030 | static int method_map_from_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) { |
1031 | _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *f = NULL((void*)0); |
1032 | Manager *m = groupdata; |
1033 | const char *name, *p; |
1034 | Machine *machine; |
1035 | uint32_t gid; |
1036 | int r; |
1037 | |
1038 | r = sd_bus_message_read(message, "su", &name, &gid); |
1039 | if (r < 0) |
1040 | return r; |
1041 | |
1042 | if (!gid_is_valid(gid)) |
1043 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid group ID " GID_FMT"%" "u", gid); |
1044 | |
1045 | machine = hashmap_get(m->machines, name); |
1046 | if (!machine) |
1047 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE"org.freedesktop.machine1.NoSuchMachine", "No machine '%s' known", name); |
1048 | |
1049 | if (machine->class != MACHINE_CONTAINER) |
1050 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Not supported for non-container machines."); |
1051 | |
1052 | p = procfs_file_alloca(machine->leader, "gid_map")({ pid_t _pid_ = (machine->leader); const char *_r_; if (_pid_ == 0) { _r_ = ("/proc/self/" "gid_map"); } else { _r_ = __builtin_alloca ((sizeof("""/proc/""") - 1) + (2+(sizeof(pid_t) <= 1 ? 3 : sizeof(pid_t) <= 2 ? 5 : sizeof(pid_t) <= 4 ? 10 : sizeof (pid_t) <= 8 ? 20 : sizeof(int[-2*(sizeof(pid_t) > 8)]) )) + 1 + sizeof("gid_map")); sprintf((char*) _r_, "/proc/""%" "i""/" "gid_map", _pid_); } _r_; }); |
1053 | f = fopen(p, "re"); |
1054 | if (!f) |
1055 | return -errno(*__errno_location ()); |
1056 | |
1057 | for (;;) { |
1058 | gid_t gid_base, gid_shift, gid_range, converted; |
1059 | int k; |
1060 | |
1061 | errno(*__errno_location ()) = 0; |
1062 | k = fscanf(f, GID_FMT"%" "u" " " GID_FMT"%" "u" " " GID_FMT"%" "u", &gid_base, &gid_shift, &gid_range); |
1063 | if (k < 0 && feof(f)) |
1064 | break; |
1065 | if (k != 3) { |
1066 | if (ferror(f) && errno(*__errno_location ()) > 0) |
1067 | return -errno(*__errno_location ()); |
1068 | |
1069 | return -EIO5; |
1070 | } |
1071 | |
1072 | if (gid < gid_base || gid >= gid_base + gid_range) |
1073 | continue; |
1074 | |
1075 | converted = gid - gid_base + gid_shift; |
1076 | if (!gid_is_valid(converted)) |
1077 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid group ID " GID_FMT"%" "u", gid); |
1078 | |
1079 | return sd_bus_reply_method_return(message, "u", (uint32_t) converted); |
1080 | } |
1081 | |
1082 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING"org.freedesktop.machine1.NoSuchGroupMapping", "Machine '%s' has no matching group mappings.", name); |
1083 | } |
1084 | |
1085 | static int method_map_to_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) { |
1086 | Manager *m = groupdata; |
1087 | Machine *machine; |
1088 | gid_t gid; |
1089 | Iterator i; |
1090 | int r; |
1091 | |
1092 | r = sd_bus_message_read(message, "u", &gid); |
1093 | if (r < 0) |
1094 | return r; |
1095 | if (!gid_is_valid(gid)) |
1096 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid group ID " GID_FMT"%" "u", gid); |
1097 | if (gid < 0x10000) |
1098 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING"org.freedesktop.machine1.NoSuchGroupMapping", "Group " GID_FMT"%" "u" " belongs to host GID range", gid); |
1099 | |
1100 | HASHMAP_FOREACH(machine, m->machines, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->machines), & (i), (void**)&(machine), ((void*)0)); ) { |
1101 | _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *f = NULL((void*)0); |
1102 | char p[STRLEN("/proc//gid_map")(sizeof("""/proc//gid_map""") - 1) + DECIMAL_STR_MAX(pid_t)(2+(sizeof(pid_t) <= 1 ? 3 : sizeof(pid_t) <= 2 ? 5 : sizeof (pid_t) <= 4 ? 10 : sizeof(pid_t) <= 8 ? 20 : sizeof(int [-2*(sizeof(pid_t) > 8)]))) + 1]; |
1103 | |
1104 | if (machine->class != MACHINE_CONTAINER) |
1105 | continue; |
1106 | |
1107 | xsprintf(p, "/proc/" GID_FMT "/gid_map", machine->leader)do { if ((__builtin_expect(!!(!(((size_t) snprintf(p, __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (p), typeof(&*(p))), sizeof(p)/sizeof((p)[0]), ((void)0)) ), "/proc/" "%" "u" "/gid_map", machine->leader) < (__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (p), typeof(&*(p))), sizeof(p)/sizeof((p)[0]), ((void)0)) ))))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("xsprintf: " "p" "[] must be big enough"), "../src/machine/machined-dbus.c" , 1107, __PRETTY_FUNCTION__); } while (0); |
1108 | f = fopen(p, "re"); |
1109 | if (!f) { |
1110 | log_warning_errno(errno, "Failed to open %s, ignoring,", p)({ int _level = ((4)), _e = (((*__errno_location ()))), _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm) >= ((_level) & 0x07)) ? log_internal_realm(((_realm) << 10 | (_level)), _e, "../src/machine/machined-dbus.c", 1110, __func__ , "Failed to open %s, ignoring,", p) : -abs(_e); }); |
1111 | continue; |
1112 | } |
1113 | |
1114 | for (;;) { |
1115 | _cleanup_free___attribute__((cleanup(freep))) char *o = NULL((void*)0); |
1116 | gid_t gid_base, gid_shift, gid_range, converted; |
1117 | int k; |
1118 | |
1119 | errno(*__errno_location ()) = 0; |
1120 | k = fscanf(f, GID_FMT"%" "u" " " GID_FMT"%" "u" " " GID_FMT"%" "u", &gid_base, &gid_shift, &gid_range); |
1121 | if (k < 0 && feof(f)) |
1122 | break; |
1123 | if (k != 3) { |
1124 | if (ferror(f) && errno(*__errno_location ()) > 0) |
1125 | return -errno(*__errno_location ()); |
1126 | |
1127 | return -EIO5; |
1128 | } |
1129 | |
1130 | /* The private user namespace is disabled, ignoring. */ |
1131 | if (gid_shift == 0) |
1132 | continue; |
1133 | |
1134 | if (gid < gid_shift || gid >= gid_shift + gid_range) |
1135 | continue; |
1136 | |
1137 | converted = (gid - gid_shift + gid_base); |
1138 | if (!gid_is_valid(converted)) |
1139 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS"org.freedesktop.DBus.Error.InvalidArgs", "Invalid group ID " GID_FMT"%" "u", gid); |
1140 | |
1141 | o = machine_bus_path(machine); |
1142 | if (!o) |
1143 | return -ENOMEM12; |
1144 | |
1145 | return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted); |
1146 | } |
1147 | } |
1148 | |
1149 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING"org.freedesktop.machine1.NoSuchGroupMapping", "No matching group mapping for " GID_FMT"%" "u" ".", gid); |
1150 | } |
1151 | |
1152 | const sd_bus_vtable manager_vtable[] = { |
1153 | SD_BUS_VTABLE_START(0){ .type = _SD_BUS_VTABLE_START, .flags = 0, .x = { .start = { .element_size = sizeof(sd_bus_vtable) }, }, }, |
1154 | SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0){ .type = _SD_BUS_VTABLE_PROPERTY, .flags = 0, .x = { .property = { .member = "PoolPath", .signature = "s", .get = property_get_pool_path , .set = ((void*)0), .offset = 0, }, }, }, |
1155 | SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0){ .type = _SD_BUS_VTABLE_PROPERTY, .flags = 0, .x = { .property = { .member = "PoolUsage", .signature = "t", .get = property_get_pool_usage , .set = ((void*)0), .offset = 0, }, }, }, |
1156 | SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0){ .type = _SD_BUS_VTABLE_PROPERTY, .flags = 0, .x = { .property = { .member = "PoolLimit", .signature = "t", .get = property_get_pool_limit , .set = ((void*)0), .offset = 0, }, }, }, |
1157 | SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "GetMachine", .signature = "s" , .result = "o", .handler = method_get_machine, .offset = 0, } , }, }, |
1158 | SD_BUS_METHOD("GetImage", "s", "o", method_get_image, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "GetImage", .signature = "s", . result = "o", .handler = method_get_image, .offset = 0, }, }, }, |
1159 | SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "GetMachineByPID", .signature = "u", .result = "o", .handler = method_get_machine_by_pid, .offset = 0, }, }, }, |
1160 | SD_BUS_METHOD("ListMachines", NULL, "a(ssso)", method_list_machines, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "ListMachines", .signature = ( (void*)0), .result = "a(ssso)", .handler = method_list_machines , .offset = 0, }, }, }, |
1161 | SD_BUS_METHOD("ListImages", NULL, "a(ssbttto)", method_list_images, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "ListImages", .signature = ((void *)0), .result = "a(ssbttto)", .handler = method_list_images, . offset = 0, }, }, }, |
1162 | SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine, 0){ .type = _SD_BUS_VTABLE_METHOD, .flags = 0, .x = { .method = { .member = "CreateMachine", .signature = "sayssusa(sv)", .result = "o", .handler = method_create_machine, .offset = 0, }, }, }, |
1163 | SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network, 0){ .type = _SD_BUS_VTABLE_METHOD, .flags = 0, .x = { .method = { .member = "CreateMachineWithNetwork", .signature = "sayssusaia(sv)" , .result = "o", .handler = method_create_machine_with_network , .offset = 0, }, }, }, |
1164 | SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine, 0){ .type = _SD_BUS_VTABLE_METHOD, .flags = 0, .x = { .method = { .member = "RegisterMachine", .signature = "sayssus", .result = "o", .handler = method_register_machine, .offset = 0, }, } , }, |
1165 | SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network, 0){ .type = _SD_BUS_VTABLE_METHOD, .flags = 0, .x = { .method = { .member = "RegisterMachineWithNetwork", .signature = "sayssusai" , .result = "o", .handler = method_register_machine_with_network , .offset = 0, }, }, }, |
1166 | SD_BUS_METHOD("TerminateMachine", "s", NULL, method_terminate_machine, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "TerminateMachine", .signature = "s", .result = ((void*)0), .handler = method_terminate_machine , .offset = 0, }, }, }, |
1167 | SD_BUS_METHOD("KillMachine", "ssi", NULL, method_kill_machine, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "KillMachine", .signature = "ssi" , .result = ((void*)0), .handler = method_kill_machine, .offset = 0, }, }, }, |
1168 | SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "GetMachineAddresses", .signature = "s", .result = "a(iay)", .handler = method_get_machine_addresses , .offset = 0, }, }, }, |
1169 | SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "GetMachineOSRelease", .signature = "s", .result = "a{ss}", .handler = method_get_machine_os_release , .offset = 0, }, }, }, |
1170 | SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty, 0){ .type = _SD_BUS_VTABLE_METHOD, .flags = 0, .x = { .method = { .member = "OpenMachinePTY", .signature = "s", .result = "hs" , .handler = method_open_machine_pty, .offset = 0, }, }, }, |
1171 | SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "OpenMachineLogin", .signature = "s", .result = "hs", .handler = method_open_machine_login, .offset = 0, }, }, }, |
1172 | SD_BUS_METHOD("OpenMachineShell", "sssasas", "hs", method_open_machine_shell, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "OpenMachineShell", .signature = "sssasas", .result = "hs", .handler = method_open_machine_shell , .offset = 0, }, }, }, |
1173 | SD_BUS_METHOD("BindMountMachine", "sssbb", NULL, method_bind_mount_machine, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "BindMountMachine", .signature = "sssbb", .result = ((void*)0), .handler = method_bind_mount_machine , .offset = 0, }, }, }, |
1174 | SD_BUS_METHOD("CopyFromMachine", "sss", NULL, method_copy_machine, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "CopyFromMachine", .signature = "sss", .result = ((void*)0), .handler = method_copy_machine, .offset = 0, }, }, }, |
1175 | SD_BUS_METHOD("CopyToMachine", "sss", NULL, method_copy_machine, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "CopyToMachine", .signature = "sss" , .result = ((void*)0), .handler = method_copy_machine, .offset = 0, }, }, }, |
1176 | SD_BUS_METHOD("OpenMachineRootDirectory", "s", "h", method_open_machine_root_directory, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "OpenMachineRootDirectory", .signature = "s", .result = "h", .handler = method_open_machine_root_directory , .offset = 0, }, }, }, |
1177 | SD_BUS_METHOD("GetMachineUIDShift", "s", "u", method_get_machine_uid_shift, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "GetMachineUIDShift", .signature = "s", .result = "u", .handler = method_get_machine_uid_shift , .offset = 0, }, }, }, |
1178 | SD_BUS_METHOD("RemoveImage", "s", NULL, method_remove_image, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "RemoveImage", .signature = "s" , .result = ((void*)0), .handler = method_remove_image, .offset = 0, }, }, }, |
1179 | SD_BUS_METHOD("RenameImage", "ss", NULL, method_rename_image, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "RenameImage", .signature = "ss" , .result = ((void*)0), .handler = method_rename_image, .offset = 0, }, }, }, |
1180 | SD_BUS_METHOD("CloneImage", "ssb", NULL, method_clone_image, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "CloneImage", .signature = "ssb" , .result = ((void*)0), .handler = method_clone_image, .offset = 0, }, }, }, |
1181 | SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL, method_mark_image_read_only, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "MarkImageReadOnly", .signature = "sb", .result = ((void*)0), .handler = method_mark_image_read_only , .offset = 0, }, }, }, |
1182 | SD_BUS_METHOD("GetImageHostname", "s", "s", method_get_image_hostname, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "GetImageHostname", .signature = "s", .result = "s", .handler = method_get_image_hostname, . offset = 0, }, }, }, |
1183 | SD_BUS_METHOD("GetImageMachineID", "s", "ay", method_get_image_machine_id, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "GetImageMachineID", .signature = "s", .result = "ay", .handler = method_get_image_machine_id , .offset = 0, }, }, }, |
1184 | SD_BUS_METHOD("GetImageMachineInfo", "s", "a{ss}", method_get_image_machine_info, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "GetImageMachineInfo", .signature = "s", .result = "a{ss}", .handler = method_get_image_machine_info , .offset = 0, }, }, }, |
1185 | SD_BUS_METHOD("GetImageOSRelease", "s", "a{ss}", method_get_image_os_release, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "GetImageOSRelease", .signature = "s", .result = "a{ss}", .handler = method_get_image_os_release , .offset = 0, }, }, }, |
1186 | SD_BUS_METHOD("SetPoolLimit", "t", NULL, method_set_pool_limit, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "SetPoolLimit", .signature = "t" , .result = ((void*)0), .handler = method_set_pool_limit, .offset = 0, }, }, }, |
1187 | SD_BUS_METHOD("SetImageLimit", "st", NULL, method_set_image_limit, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "SetImageLimit", .signature = "st" , .result = ((void*)0), .handler = method_set_image_limit, .offset = 0, }, }, }, |
1188 | SD_BUS_METHOD("CleanPool", "s", "a(st)", method_clean_pool, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "CleanPool", .signature = "s", .result = "a(st)", .handler = method_clean_pool, .offset = 0 , }, }, }, |
1189 | SD_BUS_METHOD("MapFromMachineUser", "su", "u", method_map_from_machine_user, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "MapFromMachineUser", .signature = "su", .result = "u", .handler = method_map_from_machine_user , .offset = 0, }, }, }, |
1190 | SD_BUS_METHOD("MapToMachineUser", "u", "sou", method_map_to_machine_user, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "MapToMachineUser", .signature = "u", .result = "sou", .handler = method_map_to_machine_user , .offset = 0, }, }, }, |
1191 | SD_BUS_METHOD("MapFromMachineGroup", "su", "u", method_map_from_machine_group, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "MapFromMachineGroup", .signature = "su", .result = "u", .handler = method_map_from_machine_group , .offset = 0, }, }, }, |
1192 | SD_BUS_METHOD("MapToMachineGroup", "u", "sou", method_map_to_machine_group, SD_BUS_VTABLE_UNPRIVILEGED){ .type = _SD_BUS_VTABLE_METHOD, .flags = SD_BUS_VTABLE_UNPRIVILEGED , .x = { .method = { .member = "MapToMachineGroup", .signature = "u", .result = "sou", .handler = method_map_to_machine_group , .offset = 0, }, }, }, |
1193 | SD_BUS_SIGNAL("MachineNew", "so", 0){ .type = _SD_BUS_VTABLE_SIGNAL, .flags = 0, .x = { .signal = { .member = "MachineNew", .signature = "so", }, }, }, |
1194 | SD_BUS_SIGNAL("MachineRemoved", "so", 0){ .type = _SD_BUS_VTABLE_SIGNAL, .flags = 0, .x = { .signal = { .member = "MachineRemoved", .signature = "so", }, }, }, |
1195 | SD_BUS_VTABLE_END{ .type = _SD_BUS_VTABLE_END, .flags = 0, .x = { { 0 } }, } |
1196 | }; |
1197 | |
1198 | int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1199 | const char *path, *result, *unit; |
1200 | Manager *m = userdata; |
1201 | Machine *machine; |
1202 | uint32_t id; |
1203 | int r; |
1204 | |
1205 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 1205, __PRETTY_FUNCTION__); } while (0); |
1206 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 1206 , __PRETTY_FUNCTION__); } while (0); |
1207 | |
1208 | r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); |
1209 | if (r < 0) { |
1210 | bus_log_parse_error(r); |
1211 | return 0; |
1212 | } |
1213 | |
1214 | machine = hashmap_get(m->machine_units, unit); |
1215 | if (!machine) |
1216 | return 0; |
1217 | |
1218 | if (streq_ptr(path, machine->scope_job)) { |
1219 | machine->scope_job = mfree(machine->scope_job); |
1220 | |
1221 | if (machine->started) { |
1222 | if (streq(result, "done")(strcmp((result),("done")) == 0)) |
1223 | machine_send_create_reply(machine, NULL((void*)0)); |
1224 | else { |
1225 | _cleanup_(sd_bus_error_free)__attribute__((cleanup(sd_bus_error_free))) sd_bus_error e = SD_BUS_ERROR_NULL((const sd_bus_error) {(((void*)0)), (((void*)0)), 0}); |
1226 | |
1227 | sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED"org.freedesktop.systemd1.JobFailed", "Start job for unit %s failed with '%s'", unit, result); |
1228 | |
1229 | machine_send_create_reply(machine, &e); |
1230 | } |
1231 | } |
1232 | |
1233 | machine_save(machine); |
1234 | } |
1235 | |
1236 | machine_add_to_gc_queue(machine); |
1237 | return 0; |
1238 | } |
1239 | |
1240 | int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1241 | _cleanup_free___attribute__((cleanup(freep))) char *unit = NULL((void*)0); |
1242 | const char *path; |
1243 | Manager *m = userdata; |
1244 | Machine *machine; |
1245 | int r; |
1246 | |
1247 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 1247, __PRETTY_FUNCTION__); } while (0); |
1248 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 1248 , __PRETTY_FUNCTION__); } while (0); |
1249 | |
1250 | path = sd_bus_message_get_path(message); |
1251 | if (!path) |
1252 | return 0; |
1253 | |
1254 | r = unit_name_from_dbus_path(path, &unit); |
1255 | if (r == -EINVAL22) /* not for a unit */ |
1256 | return 0; |
1257 | if (r < 0) { |
1258 | log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/machine/machined-dbus.c" , 1258, __func__); |
1259 | return 0; |
1260 | } |
1261 | |
1262 | machine = hashmap_get(m->machine_units, unit); |
1263 | if (!machine) |
1264 | return 0; |
1265 | |
1266 | machine_add_to_gc_queue(machine); |
1267 | return 0; |
1268 | } |
1269 | |
1270 | int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1271 | const char *path, *unit; |
1272 | Manager *m = userdata; |
1273 | Machine *machine; |
1274 | int r; |
1275 | |
1276 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 1276, __PRETTY_FUNCTION__); } while (0); |
1277 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 1277 , __PRETTY_FUNCTION__); } while (0); |
1278 | |
1279 | r = sd_bus_message_read(message, "so", &unit, &path); |
1280 | if (r < 0) { |
1281 | bus_log_parse_error(r); |
1282 | return 0; |
1283 | } |
1284 | |
1285 | machine = hashmap_get(m->machine_units, unit); |
1286 | if (!machine) |
1287 | return 0; |
1288 | |
1289 | machine_add_to_gc_queue(machine); |
1290 | return 0; |
1291 | } |
1292 | |
1293 | int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1294 | Manager *m = userdata; |
1295 | Machine *machine; |
1296 | Iterator i; |
1297 | int b, r; |
1298 | |
1299 | assert(message)do { if ((__builtin_expect(!!(!(message)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("message"), "../src/machine/machined-dbus.c" , 1299, __PRETTY_FUNCTION__); } while (0); |
1300 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 1300 , __PRETTY_FUNCTION__); } while (0); |
1301 | |
1302 | r = sd_bus_message_read(message, "b", &b); |
1303 | if (r < 0) { |
1304 | bus_log_parse_error(r); |
1305 | return 0; |
1306 | } |
1307 | if (b) |
1308 | return 0; |
1309 | |
1310 | /* systemd finished reloading, let's recheck all our machines */ |
1311 | log_debug("System manager has been reloaded, rechecking machines...")({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/machine/machined-dbus.c", 1311, __func__, "System manager has been reloaded, rechecking machines..." ) : -abs(_e); }); |
1312 | |
1313 | HASHMAP_FOREACH(machine, m->machines, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((m->machines), & (i), (void**)&(machine), ((void*)0)); ) |
1314 | machine_add_to_gc_queue(machine); |
1315 | |
1316 | return 0; |
1317 | } |
1318 | |
1319 | int manager_start_scope( |
1320 | Manager *manager, |
1321 | const char *scope, |
1322 | pid_t pid, |
1323 | const char *slice, |
1324 | const char *description, |
1325 | sd_bus_message *more_properties, |
1326 | sd_bus_error *error, |
1327 | char **job) { |
1328 | |
1329 | _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *m = NULL((void*)0), *reply = NULL((void*)0); |
1330 | int r; |
1331 | |
1332 | assert(manager)do { if ((__builtin_expect(!!(!(manager)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("manager"), "../src/machine/machined-dbus.c" , 1332, __PRETTY_FUNCTION__); } while (0); |
1333 | assert(scope)do { if ((__builtin_expect(!!(!(scope)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("scope"), "../src/machine/machined-dbus.c" , 1333, __PRETTY_FUNCTION__); } while (0); |
1334 | assert(pid > 1)do { if ((__builtin_expect(!!(!(pid > 1)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("pid > 1"), "../src/machine/machined-dbus.c" , 1334, __PRETTY_FUNCTION__); } while (0); |
1335 | |
1336 | r = sd_bus_message_new_method_call( |
1337 | manager->bus, |
1338 | &m, |
1339 | "org.freedesktop.systemd1", |
1340 | "/org/freedesktop/systemd1", |
1341 | "org.freedesktop.systemd1.Manager", |
1342 | "StartTransientUnit"); |
1343 | if (r < 0) |
1344 | return r; |
1345 | |
1346 | r = sd_bus_message_append(m, "ss", strempty(scope), "fail"); |
1347 | if (r < 0) |
1348 | return r; |
1349 | |
1350 | r = sd_bus_message_open_container(m, 'a', "(sv)"); |
1351 | if (r < 0) |
1352 | return r; |
1353 | |
1354 | if (!isempty(slice)) { |
1355 | r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice); |
1356 | if (r < 0) |
1357 | return r; |
1358 | } |
1359 | |
1360 | if (!isempty(description)) { |
1361 | r = sd_bus_message_append(m, "(sv)", "Description", "s", description); |
1362 | if (r < 0) |
1363 | return r; |
1364 | } |
1365 | |
1366 | r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid); |
1367 | if (r < 0) |
1368 | return r; |
1369 | |
1370 | r = sd_bus_message_append(m, "(sv)", "Delegate", "b", 1); |
1371 | if (r < 0) |
1372 | return r; |
1373 | |
1374 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", UINT64_C(16384)16384UL); |
1375 | if (r < 0) |
1376 | return bus_log_create_error(r); |
1377 | |
1378 | if (more_properties) { |
1379 | r = sd_bus_message_copy(m, more_properties, true1); |
1380 | if (r < 0) |
1381 | return r; |
1382 | } |
1383 | |
1384 | r = sd_bus_message_close_container(m); |
1385 | if (r < 0) |
1386 | return r; |
1387 | |
1388 | r = sd_bus_message_append(m, "a(sa(sv))", 0); |
1389 | if (r < 0) |
1390 | return r; |
1391 | |
1392 | r = sd_bus_call(manager->bus, m, 0, error, &reply); |
1393 | if (r < 0) |
1394 | return r; |
1395 | |
1396 | if (job) { |
1397 | const char *j; |
1398 | char *copy; |
1399 | |
1400 | r = sd_bus_message_read(reply, "o", &j); |
1401 | if (r < 0) |
1402 | return r; |
1403 | |
1404 | copy = strdup(j); |
1405 | if (!copy) |
1406 | return -ENOMEM12; |
1407 | |
1408 | *job = copy; |
1409 | } |
1410 | |
1411 | return 1; |
1412 | } |
1413 | |
1414 | int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { |
1415 | _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0); |
1416 | int r; |
1417 | |
1418 | assert(manager)do { if ((__builtin_expect(!!(!(manager)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("manager"), "../src/machine/machined-dbus.c" , 1418, __PRETTY_FUNCTION__); } while (0); |
1419 | assert(unit)do { if ((__builtin_expect(!!(!(unit)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("unit"), "../src/machine/machined-dbus.c" , 1419, __PRETTY_FUNCTION__); } while (0); |
1420 | |
1421 | r = sd_bus_call_method( |
1422 | manager->bus, |
1423 | "org.freedesktop.systemd1", |
1424 | "/org/freedesktop/systemd1", |
1425 | "org.freedesktop.systemd1.Manager", |
1426 | "StopUnit", |
1427 | error, |
1428 | &reply, |
1429 | "ss", unit, "fail"); |
1430 | if (r < 0) { |
1431 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT"org.freedesktop.systemd1.NoSuchUnit") || |
1432 | sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED"org.freedesktop.systemd1.LoadFailed")) { |
1433 | |
1434 | if (job) |
1435 | *job = NULL((void*)0); |
1436 | |
1437 | sd_bus_error_free(error); |
1438 | return 0; |
1439 | } |
1440 | |
1441 | return r; |
1442 | } |
1443 | |
1444 | if (job) { |
1445 | const char *j; |
1446 | char *copy; |
1447 | |
1448 | r = sd_bus_message_read(reply, "o", &j); |
1449 | if (r < 0) |
1450 | return r; |
1451 | |
1452 | copy = strdup(j); |
1453 | if (!copy) |
1454 | return -ENOMEM12; |
1455 | |
1456 | *job = copy; |
1457 | } |
1458 | |
1459 | return 1; |
1460 | } |
1461 | |
1462 | int manager_kill_unit(Manager *manager, const char *unit, int signo, sd_bus_error *error) { |
1463 | assert(manager)do { if ((__builtin_expect(!!(!(manager)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("manager"), "../src/machine/machined-dbus.c" , 1463, __PRETTY_FUNCTION__); } while (0); |
1464 | assert(unit)do { if ((__builtin_expect(!!(!(unit)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("unit"), "../src/machine/machined-dbus.c" , 1464, __PRETTY_FUNCTION__); } while (0); |
1465 | |
1466 | return sd_bus_call_method( |
1467 | manager->bus, |
1468 | "org.freedesktop.systemd1", |
1469 | "/org/freedesktop/systemd1", |
1470 | "org.freedesktop.systemd1.Manager", |
1471 | "KillUnit", |
1472 | error, |
1473 | NULL((void*)0), |
1474 | "ssi", unit, "all", signo); |
1475 | } |
1476 | |
1477 | int manager_unit_is_active(Manager *manager, const char *unit) { |
1478 | _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}); |
1479 | _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0); |
1480 | _cleanup_free___attribute__((cleanup(freep))) char *path = NULL((void*)0); |
1481 | const char *state; |
1482 | int r; |
1483 | |
1484 | assert(manager)do { if ((__builtin_expect(!!(!(manager)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("manager"), "../src/machine/machined-dbus.c" , 1484, __PRETTY_FUNCTION__); } while (0); |
1485 | assert(unit)do { if ((__builtin_expect(!!(!(unit)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("unit"), "../src/machine/machined-dbus.c" , 1485, __PRETTY_FUNCTION__); } while (0); |
1486 | |
1487 | path = unit_dbus_path_from_name(unit); |
1488 | if (!path) |
1489 | return -ENOMEM12; |
1490 | |
1491 | r = sd_bus_get_property( |
1492 | manager->bus, |
1493 | "org.freedesktop.systemd1", |
1494 | path, |
1495 | "org.freedesktop.systemd1.Unit", |
1496 | "ActiveState", |
1497 | &error, |
1498 | &reply, |
1499 | "s"); |
1500 | if (r < 0) { |
1501 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY"org.freedesktop.DBus.Error.NoReply") || |
1502 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED"org.freedesktop.DBus.Error.Disconnected")) |
1503 | return true1; |
1504 | |
1505 | if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT"org.freedesktop.systemd1.NoSuchUnit") || |
1506 | sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED"org.freedesktop.systemd1.LoadFailed")) |
1507 | return false0; |
1508 | |
1509 | return r; |
1510 | } |
1511 | |
1512 | r = sd_bus_message_read(reply, "s", &state); |
1513 | if (r < 0) |
1514 | return -EINVAL22; |
1515 | |
1516 | return !STR_IN_SET(state, "inactive", "failed")(!!strv_find((((char**) ((const char*[]) { "inactive", "failed" , ((void*)0) }))), (state))); |
1517 | } |
1518 | |
1519 | int manager_job_is_active(Manager *manager, const char *path) { |
1520 | _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}); |
1521 | _cleanup_(sd_bus_message_unrefp)__attribute__((cleanup(sd_bus_message_unrefp))) sd_bus_message *reply = NULL((void*)0); |
1522 | int r; |
1523 | |
1524 | assert(manager)do { if ((__builtin_expect(!!(!(manager)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("manager"), "../src/machine/machined-dbus.c" , 1524, __PRETTY_FUNCTION__); } while (0); |
1525 | assert(path)do { if ((__builtin_expect(!!(!(path)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("path"), "../src/machine/machined-dbus.c" , 1525, __PRETTY_FUNCTION__); } while (0); |
1526 | |
1527 | r = sd_bus_get_property( |
1528 | manager->bus, |
1529 | "org.freedesktop.systemd1", |
1530 | path, |
1531 | "org.freedesktop.systemd1.Job", |
1532 | "State", |
1533 | &error, |
1534 | &reply, |
1535 | "s"); |
1536 | if (r < 0) { |
1537 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY"org.freedesktop.DBus.Error.NoReply") || |
1538 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED"org.freedesktop.DBus.Error.Disconnected")) |
1539 | return true1; |
1540 | |
1541 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT"org.freedesktop.DBus.Error.UnknownObject")) |
1542 | return false0; |
1543 | |
1544 | return r; |
1545 | } |
1546 | |
1547 | /* We don't actually care about the state really. The fact |
1548 | * that we could read the job state is enough for us */ |
1549 | |
1550 | return true1; |
1551 | } |
1552 | |
1553 | int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) { |
1554 | Machine *mm; |
1555 | int r; |
1556 | |
1557 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 1557 , __PRETTY_FUNCTION__); } while (0); |
1558 | assert(pid >= 1)do { if ((__builtin_expect(!!(!(pid >= 1)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("pid >= 1"), "../src/machine/machined-dbus.c" , 1558, __PRETTY_FUNCTION__); } while (0); |
1559 | assert(machine)do { if ((__builtin_expect(!!(!(machine)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("machine"), "../src/machine/machined-dbus.c" , 1559, __PRETTY_FUNCTION__); } while (0); |
1560 | |
1561 | mm = hashmap_get(m->machine_leaders, PID_TO_PTR(pid)); |
1562 | if (!mm) { |
1563 | _cleanup_free___attribute__((cleanup(freep))) char *unit = NULL((void*)0); |
1564 | |
1565 | r = cg_pid_get_unit(pid, &unit); |
1566 | if (r >= 0) |
1567 | mm = hashmap_get(m->machine_units, unit); |
1568 | } |
1569 | if (!mm) |
1570 | return 0; |
1571 | |
1572 | *machine = mm; |
1573 | return 1; |
1574 | } |
1575 | |
1576 | int manager_add_machine(Manager *m, const char *name, Machine **_machine) { |
1577 | Machine *machine; |
1578 | |
1579 | assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("m"), "../src/machine/machined-dbus.c", 1579 , __PRETTY_FUNCTION__); } while (0); |
1580 | assert(name)do { if ((__builtin_expect(!!(!(name)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("name"), "../src/machine/machined-dbus.c" , 1580, __PRETTY_FUNCTION__); } while (0); |
1581 | |
1582 | machine = hashmap_get(m->machines, name); |
1583 | if (!machine) { |
1584 | machine = machine_new(m, _MACHINE_CLASS_INVALID, name); |
1585 | if (!machine) |
1586 | return -ENOMEM12; |
1587 | } |
1588 | |
1589 | if (_machine) |
1590 | *_machine = machine; |
1591 | |
1592 | return 0; |
1593 | } |