]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-cgroup.c
cgroup: Implement default propagation of MemoryLow with DefaultMemoryLow
[thirdparty/systemd.git] / src / core / dbus-cgroup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <arpa/inet.h>
4
5 #include "af-list.h"
6 #include "alloc-util.h"
7 #include "bpf-firewall.h"
8 #include "bus-util.h"
9 #include "cgroup-util.h"
10 #include "cgroup.h"
11 #include "dbus-cgroup.h"
12 #include "dbus-util.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "limits-util.h"
16 #include "path-util.h"
17
18 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_cgroup_device_policy, cgroup_device_policy, CGroupDevicePolicy);
19
20 static int property_get_cgroup_mask(
21 sd_bus *bus,
22 const char *path,
23 const char *interface,
24 const char *property,
25 sd_bus_message *reply,
26 void *userdata,
27 sd_bus_error *error) {
28
29 CGroupMask *mask = userdata;
30 CGroupController ctrl;
31 int r;
32
33 assert(bus);
34 assert(reply);
35
36 r = sd_bus_message_open_container(reply, 'a', "s");
37 if (r < 0)
38 return r;
39
40 for (ctrl = 0; ctrl < _CGROUP_CONTROLLER_MAX; ctrl++) {
41 if ((*mask & CGROUP_CONTROLLER_TO_MASK(ctrl)) == 0)
42 continue;
43
44 r = sd_bus_message_append(reply, "s", cgroup_controller_to_string(ctrl));
45 if (r < 0)
46 return r;
47 }
48
49 return sd_bus_message_close_container(reply);
50 }
51
52 static int property_get_delegate_controllers(
53 sd_bus *bus,
54 const char *path,
55 const char *interface,
56 const char *property,
57 sd_bus_message *reply,
58 void *userdata,
59 sd_bus_error *error) {
60
61 CGroupContext *c = userdata;
62
63 assert(bus);
64 assert(reply);
65 assert(c);
66
67 if (!c->delegate)
68 return sd_bus_message_append(reply, "as", 0);
69
70 return property_get_cgroup_mask(bus, path, interface, property, reply, &c->delegate_controllers, error);
71 }
72
73 static int property_get_io_device_weight(
74 sd_bus *bus,
75 const char *path,
76 const char *interface,
77 const char *property,
78 sd_bus_message *reply,
79 void *userdata,
80 sd_bus_error *error) {
81
82 CGroupContext *c = userdata;
83 CGroupIODeviceWeight *w;
84 int r;
85
86 assert(bus);
87 assert(reply);
88 assert(c);
89
90 r = sd_bus_message_open_container(reply, 'a', "(st)");
91 if (r < 0)
92 return r;
93
94 LIST_FOREACH(device_weights, w, c->io_device_weights) {
95 r = sd_bus_message_append(reply, "(st)", w->path, w->weight);
96 if (r < 0)
97 return r;
98 }
99
100 return sd_bus_message_close_container(reply);
101 }
102
103 static int property_get_io_device_limits(
104 sd_bus *bus,
105 const char *path,
106 const char *interface,
107 const char *property,
108 sd_bus_message *reply,
109 void *userdata,
110 sd_bus_error *error) {
111
112 CGroupContext *c = userdata;
113 CGroupIODeviceLimit *l;
114 int r;
115
116 assert(bus);
117 assert(reply);
118 assert(c);
119
120 r = sd_bus_message_open_container(reply, 'a', "(st)");
121 if (r < 0)
122 return r;
123
124 LIST_FOREACH(device_limits, l, c->io_device_limits) {
125 CGroupIOLimitType type;
126
127 type = cgroup_io_limit_type_from_string(property);
128 if (type < 0 || l->limits[type] == cgroup_io_limit_defaults[type])
129 continue;
130
131 r = sd_bus_message_append(reply, "(st)", l->path, l->limits[type]);
132 if (r < 0)
133 return r;
134 }
135
136 return sd_bus_message_close_container(reply);
137 }
138
139 static int property_get_io_device_latency(
140 sd_bus *bus,
141 const char *path,
142 const char *interface,
143 const char *property,
144 sd_bus_message *reply,
145 void *userdata,
146 sd_bus_error *error) {
147
148 CGroupContext *c = userdata;
149 CGroupIODeviceLatency *l;
150 int r;
151
152 assert(bus);
153 assert(reply);
154 assert(c);
155
156 r = sd_bus_message_open_container(reply, 'a', "(st)");
157 if (r < 0)
158 return r;
159
160 LIST_FOREACH(device_latencies, l, c->io_device_latencies) {
161 r = sd_bus_message_append(reply, "(st)", l->path, l->target_usec);
162 if (r < 0)
163 return r;
164 }
165
166 return sd_bus_message_close_container(reply);
167 }
168
169 static int property_get_blockio_device_weight(
170 sd_bus *bus,
171 const char *path,
172 const char *interface,
173 const char *property,
174 sd_bus_message *reply,
175 void *userdata,
176 sd_bus_error *error) {
177
178 CGroupContext *c = userdata;
179 CGroupBlockIODeviceWeight *w;
180 int r;
181
182 assert(bus);
183 assert(reply);
184 assert(c);
185
186 r = sd_bus_message_open_container(reply, 'a', "(st)");
187 if (r < 0)
188 return r;
189
190 LIST_FOREACH(device_weights, w, c->blockio_device_weights) {
191 r = sd_bus_message_append(reply, "(st)", w->path, w->weight);
192 if (r < 0)
193 return r;
194 }
195
196 return sd_bus_message_close_container(reply);
197 }
198
199 static int property_get_blockio_device_bandwidths(
200 sd_bus *bus,
201 const char *path,
202 const char *interface,
203 const char *property,
204 sd_bus_message *reply,
205 void *userdata,
206 sd_bus_error *error) {
207
208 CGroupContext *c = userdata;
209 CGroupBlockIODeviceBandwidth *b;
210 int r;
211
212 assert(bus);
213 assert(reply);
214 assert(c);
215
216 r = sd_bus_message_open_container(reply, 'a', "(st)");
217 if (r < 0)
218 return r;
219
220 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
221 uint64_t v;
222
223 if (streq(property, "BlockIOReadBandwidth"))
224 v = b->rbps;
225 else
226 v = b->wbps;
227
228 if (v == CGROUP_LIMIT_MAX)
229 continue;
230
231 r = sd_bus_message_append(reply, "(st)", b->path, v);
232 if (r < 0)
233 return r;
234 }
235
236 return sd_bus_message_close_container(reply);
237 }
238
239 static int property_get_device_allow(
240 sd_bus *bus,
241 const char *path,
242 const char *interface,
243 const char *property,
244 sd_bus_message *reply,
245 void *userdata,
246 sd_bus_error *error) {
247
248 CGroupContext *c = userdata;
249 CGroupDeviceAllow *a;
250 int r;
251
252 assert(bus);
253 assert(reply);
254 assert(c);
255
256 r = sd_bus_message_open_container(reply, 'a', "(ss)");
257 if (r < 0)
258 return r;
259
260 LIST_FOREACH(device_allow, a, c->device_allow) {
261 unsigned k = 0;
262 char rwm[4];
263
264 if (a->r)
265 rwm[k++] = 'r';
266 if (a->w)
267 rwm[k++] = 'w';
268 if (a->m)
269 rwm[k++] = 'm';
270
271 rwm[k] = 0;
272
273 r = sd_bus_message_append(reply, "(ss)", a->path, rwm);
274 if (r < 0)
275 return r;
276 }
277
278 return sd_bus_message_close_container(reply);
279 }
280
281 static int property_get_ip_address_access(
282 sd_bus *bus,
283 const char *path,
284 const char *interface,
285 const char *property,
286 sd_bus_message *reply,
287 void *userdata,
288 sd_bus_error *error) {
289
290 IPAddressAccessItem** items = userdata, *i;
291 int r;
292
293 r = sd_bus_message_open_container(reply, 'a', "(iayu)");
294 if (r < 0)
295 return r;
296
297 LIST_FOREACH(items, i, *items) {
298
299 r = sd_bus_message_open_container(reply, 'r', "iayu");
300 if (r < 0)
301 return r;
302
303 r = sd_bus_message_append(reply, "i", i->family);
304 if (r < 0)
305 return r;
306
307 r = sd_bus_message_append_array(reply, 'y', &i->address, FAMILY_ADDRESS_SIZE(i->family));
308 if (r < 0)
309 return r;
310
311 r = sd_bus_message_append(reply, "u", (uint32_t) i->prefixlen);
312 if (r < 0)
313 return r;
314
315 r = sd_bus_message_close_container(reply);
316 if (r < 0)
317 return r;
318 }
319
320 return sd_bus_message_close_container(reply);
321 }
322
323 const sd_bus_vtable bus_cgroup_vtable[] = {
324 SD_BUS_VTABLE_START(0),
325 SD_BUS_PROPERTY("Delegate", "b", bus_property_get_bool, offsetof(CGroupContext, delegate), 0),
326 SD_BUS_PROPERTY("DelegateControllers", "as", property_get_delegate_controllers, 0, 0),
327 SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, cpu_accounting), 0),
328 SD_BUS_PROPERTY("CPUWeight", "t", NULL, offsetof(CGroupContext, cpu_weight), 0),
329 SD_BUS_PROPERTY("StartupCPUWeight", "t", NULL, offsetof(CGroupContext, startup_cpu_weight), 0),
330 SD_BUS_PROPERTY("CPUShares", "t", NULL, offsetof(CGroupContext, cpu_shares), 0),
331 SD_BUS_PROPERTY("StartupCPUShares", "t", NULL, offsetof(CGroupContext, startup_cpu_shares), 0),
332 SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec, offsetof(CGroupContext, cpu_quota_per_sec_usec), 0),
333 SD_BUS_PROPERTY("CPUQuotaPeriodUSec", "t", bus_property_get_usec, offsetof(CGroupContext, cpu_quota_period_usec), 0),
334 SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, io_accounting), 0),
335 SD_BUS_PROPERTY("IOWeight", "t", NULL, offsetof(CGroupContext, io_weight), 0),
336 SD_BUS_PROPERTY("StartupIOWeight", "t", NULL, offsetof(CGroupContext, startup_io_weight), 0),
337 SD_BUS_PROPERTY("IODeviceWeight", "a(st)", property_get_io_device_weight, 0, 0),
338 SD_BUS_PROPERTY("IOReadBandwidthMax", "a(st)", property_get_io_device_limits, 0, 0),
339 SD_BUS_PROPERTY("IOWriteBandwidthMax", "a(st)", property_get_io_device_limits, 0, 0),
340 SD_BUS_PROPERTY("IOReadIOPSMax", "a(st)", property_get_io_device_limits, 0, 0),
341 SD_BUS_PROPERTY("IOWriteIOPSMax", "a(st)", property_get_io_device_limits, 0, 0),
342 SD_BUS_PROPERTY("IODeviceLatencyTargetUSec", "a(st)", property_get_io_device_latency, 0, 0),
343 SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, blockio_accounting), 0),
344 SD_BUS_PROPERTY("BlockIOWeight", "t", NULL, offsetof(CGroupContext, blockio_weight), 0),
345 SD_BUS_PROPERTY("StartupBlockIOWeight", "t", NULL, offsetof(CGroupContext, startup_blockio_weight), 0),
346 SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight, 0, 0),
347 SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths, 0, 0),
348 SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths, 0, 0),
349 SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, memory_accounting), 0),
350 SD_BUS_PROPERTY("DefaultMemoryLow", "t", NULL, offsetof(CGroupContext, default_memory_low), 0),
351 SD_BUS_PROPERTY("MemoryMin", "t", NULL, offsetof(CGroupContext, memory_min), 0),
352 SD_BUS_PROPERTY("MemoryLow", "t", NULL, offsetof(CGroupContext, memory_low), 0),
353 SD_BUS_PROPERTY("MemoryHigh", "t", NULL, offsetof(CGroupContext, memory_high), 0),
354 SD_BUS_PROPERTY("MemoryMax", "t", NULL, offsetof(CGroupContext, memory_max), 0),
355 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL, offsetof(CGroupContext, memory_swap_max), 0),
356 SD_BUS_PROPERTY("MemoryLimit", "t", NULL, offsetof(CGroupContext, memory_limit), 0),
357 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy, offsetof(CGroupContext, device_policy), 0),
358 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow, 0, 0),
359 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, tasks_accounting), 0),
360 SD_BUS_PROPERTY("TasksMax", "t", NULL, offsetof(CGroupContext, tasks_max), 0),
361 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, ip_accounting), 0),
362 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access, offsetof(CGroupContext, ip_address_allow), 0),
363 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access, offsetof(CGroupContext, ip_address_deny), 0),
364 SD_BUS_PROPERTY("DisableControllers", "as", property_get_cgroup_mask, offsetof(CGroupContext, disable_controllers), 0),
365 SD_BUS_VTABLE_END
366 };
367
368 static int bus_cgroup_set_transient_property(
369 Unit *u,
370 CGroupContext *c,
371 const char *name,
372 sd_bus_message *message,
373 UnitWriteFlags flags,
374 sd_bus_error *error) {
375
376 int r;
377
378 assert(u);
379 assert(c);
380 assert(name);
381 assert(message);
382
383 flags |= UNIT_PRIVATE;
384
385 if (streq(name, "Delegate")) {
386 int b;
387
388 if (!UNIT_VTABLE(u)->can_delegate)
389 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delegation not available for unit type");
390
391 r = sd_bus_message_read(message, "b", &b);
392 if (r < 0)
393 return r;
394
395 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
396 c->delegate = b;
397 c->delegate_controllers = b ? _CGROUP_MASK_ALL : 0;
398
399 unit_write_settingf(u, flags, name, "Delegate=%s", yes_no(b));
400 }
401
402 return 1;
403
404 } else if (streq(name, "DelegateControllers")) {
405 CGroupMask mask = 0;
406
407 if (!UNIT_VTABLE(u)->can_delegate)
408 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delegation not available for unit type");
409
410 r = sd_bus_message_enter_container(message, 'a', "s");
411 if (r < 0)
412 return r;
413
414 for (;;) {
415 CGroupController cc;
416 const char *t;
417
418 r = sd_bus_message_read(message, "s", &t);
419 if (r < 0)
420 return r;
421 if (r == 0)
422 break;
423
424 cc = cgroup_controller_from_string(t);
425 if (cc < 0)
426 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown cgroup controller '%s'", t);
427
428 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
429 }
430
431 r = sd_bus_message_exit_container(message);
432 if (r < 0)
433 return r;
434
435 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
436 _cleanup_free_ char *t = NULL;
437
438 r = cg_mask_to_string(mask, &t);
439 if (r < 0)
440 return r;
441
442 c->delegate = true;
443 if (mask == 0)
444 c->delegate_controllers = 0;
445 else
446 c->delegate_controllers |= mask;
447
448 unit_write_settingf(u, flags, name, "Delegate=%s", strempty(t));
449 }
450
451 return 1;
452 }
453
454 return 0;
455 }
456
457 static int bus_cgroup_set_boolean(
458 Unit *u,
459 const char *name,
460 bool *p,
461 CGroupMask mask,
462 sd_bus_message *message,
463 UnitWriteFlags flags,
464 sd_bus_error *error) {
465
466 int b, r;
467
468 assert(p);
469
470 r = sd_bus_message_read(message, "b", &b);
471 if (r < 0)
472 return r;
473
474 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
475 *p = b;
476 unit_invalidate_cgroup(u, mask);
477 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(b));
478 }
479
480 return 1;
481 }
482
483 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
484 static int bus_cgroup_set_##function( \
485 Unit *u, \
486 const char *name, \
487 uint64_t *p, \
488 sd_bus_message *message, \
489 UnitWriteFlags flags, \
490 sd_bus_error *error) { \
491 \
492 uint64_t v; \
493 int r; \
494 \
495 assert(p); \
496 \
497 r = sd_bus_message_read(message, "t", &v); \
498 if (r < 0) \
499 return r; \
500 \
501 if (!check(v)) \
502 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
503 "Value specified in %s is out of range", name); \
504 \
505 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
506 *p = v; \
507 unit_invalidate_cgroup(u, (mask)); \
508 \
509 if (v == (val)) \
510 unit_write_settingf(u, flags, name, \
511 "%s=", name); \
512 else \
513 unit_write_settingf(u, flags, name, \
514 "%s=%" PRIu64, name, v); \
515 } \
516 \
517 return 1; \
518 }
519
520 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
521 static int bus_cgroup_set_##function( \
522 Unit *u, \
523 const char *name, \
524 uint64_t *p, \
525 sd_bus_message *message, \
526 UnitWriteFlags flags, \
527 sd_bus_error *error) { \
528 \
529 uint64_t v; \
530 int r; \
531 \
532 assert(p); \
533 \
534 r = sd_bus_message_read(message, "t", &v); \
535 if (r < 0) \
536 return r; \
537 \
538 if (v < minimum) \
539 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
540 "Value specified in %s is out of range", name); \
541 \
542 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
543 *p = v; \
544 unit_invalidate_cgroup(u, (mask)); \
545 \
546 if (v == CGROUP_LIMIT_MAX) \
547 unit_write_settingf(u, flags, name, \
548 "%s=infinity", name); \
549 else \
550 unit_write_settingf(u, flags, name, \
551 "%s=%" PRIu64, name, v); \
552 } \
553 \
554 return 1; \
555 } \
556 static int bus_cgroup_set_##function##_scale( \
557 Unit *u, \
558 const char *name, \
559 uint64_t *p, \
560 sd_bus_message *message, \
561 UnitWriteFlags flags, \
562 sd_bus_error *error) { \
563 \
564 uint64_t v; \
565 uint32_t raw; \
566 int r; \
567 \
568 assert(p); \
569 \
570 r = sd_bus_message_read(message, "u", &raw); \
571 if (r < 0) \
572 return r; \
573 \
574 v = scale(raw, UINT32_MAX); \
575 if (v < minimum || v >= UINT64_MAX) \
576 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
577 "Value specified in %s is out of range", name); \
578 \
579 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
580 const char *e; \
581 \
582 *p = v; \
583 unit_invalidate_cgroup(u, (mask)); \
584 \
585 /* Chop off suffix */ \
586 assert_se(e = endswith(name, "Scale")); \
587 name = strndupa(name, e - name); \
588 \
589 unit_write_settingf(u, flags, name, "%s=%" PRIu32 "%%", name, \
590 (uint32_t) (DIV_ROUND_UP((uint64_t) raw * 100U, (uint64_t) UINT32_MAX))); \
591 } \
592 \
593 return 1; \
594 }
595
596 #pragma GCC diagnostic push
597 #pragma GCC diagnostic ignored "-Wtype-limits"
598 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_weight, CGROUP_MASK_CPU, CGROUP_WEIGHT_IS_OK, CGROUP_WEIGHT_INVALID);
599 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares, CGROUP_MASK_CPU, CGROUP_CPU_SHARES_IS_OK, CGROUP_CPU_SHARES_INVALID);
600 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight, CGROUP_MASK_IO, CGROUP_WEIGHT_IS_OK, CGROUP_WEIGHT_INVALID);
601 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight, CGROUP_MASK_BLKIO, CGROUP_BLKIO_WEIGHT_IS_OK, CGROUP_BLKIO_WEIGHT_INVALID);
602 BUS_DEFINE_SET_CGROUP_LIMIT(memory, CGROUP_MASK_MEMORY, physical_memory_scale, 1);
603 BUS_DEFINE_SET_CGROUP_LIMIT(swap, CGROUP_MASK_MEMORY, physical_memory_scale, 0);
604 BUS_DEFINE_SET_CGROUP_LIMIT(tasks_max, CGROUP_MASK_PIDS, system_tasks_max_scale, 1);
605 #pragma GCC diagnostic pop
606
607 int bus_cgroup_set_property(
608 Unit *u,
609 CGroupContext *c,
610 const char *name,
611 sd_bus_message *message,
612 UnitWriteFlags flags,
613 sd_bus_error *error) {
614
615 CGroupIOLimitType iol_type;
616 int r;
617
618 assert(u);
619 assert(c);
620 assert(name);
621 assert(message);
622
623 flags |= UNIT_PRIVATE;
624
625 if (streq(name, "CPUAccounting"))
626 return bus_cgroup_set_boolean(u, name, &c->cpu_accounting, get_cpu_accounting_mask(), message, flags, error);
627
628 if (streq(name, "CPUWeight"))
629 return bus_cgroup_set_cpu_weight(u, name, &c->cpu_weight, message, flags, error);
630
631 if (streq(name, "StartupCPUWeight"))
632 return bus_cgroup_set_cpu_weight(u, name, &c->startup_cpu_weight, message, flags, error);
633
634 if (streq(name, "CPUShares"))
635 return bus_cgroup_set_cpu_shares(u, name, &c->cpu_shares, message, flags, error);
636
637 if (streq(name, "StartupCPUShares"))
638 return bus_cgroup_set_cpu_shares(u, name, &c->startup_cpu_shares, message, flags, error);
639
640 if (streq(name, "IOAccounting"))
641 return bus_cgroup_set_boolean(u, name, &c->io_accounting, CGROUP_MASK_IO, message, flags, error);
642
643 if (streq(name, "IOWeight"))
644 return bus_cgroup_set_io_weight(u, name, &c->io_weight, message, flags, error);
645
646 if (streq(name, "StartupIOWeight"))
647 return bus_cgroup_set_io_weight(u, name, &c->startup_io_weight, message, flags, error);
648
649 if (streq(name, "BlockIOAccounting"))
650 return bus_cgroup_set_boolean(u, name, &c->blockio_accounting, CGROUP_MASK_BLKIO, message, flags, error);
651
652 if (streq(name, "BlockIOWeight"))
653 return bus_cgroup_set_blockio_weight(u, name, &c->blockio_weight, message, flags, error);
654
655 if (streq(name, "StartupBlockIOWeight"))
656 return bus_cgroup_set_blockio_weight(u, name, &c->startup_blockio_weight, message, flags, error);
657
658 if (streq(name, "MemoryAccounting"))
659 return bus_cgroup_set_boolean(u, name, &c->memory_accounting, CGROUP_MASK_MEMORY, message, flags, error);
660
661 if (streq(name, "MemoryMin"))
662 return bus_cgroup_set_memory(u, name, &c->memory_min, message, flags, error);
663
664 if (streq(name, "MemoryLow"))
665 return bus_cgroup_set_memory(u, name, &c->memory_low, message, flags, error);
666
667 if (streq(name, "DefaultMemoryLow"))
668 return bus_cgroup_set_memory(u, name, &c->default_memory_low, message, flags, error);
669
670 if (streq(name, "MemoryHigh"))
671 return bus_cgroup_set_memory(u, name, &c->memory_high, message, flags, error);
672
673 if (streq(name, "MemorySwapMax"))
674 return bus_cgroup_set_swap(u, name, &c->memory_swap_max, message, flags, error);
675
676 if (streq(name, "MemoryMax"))
677 return bus_cgroup_set_memory(u, name, &c->memory_max, message, flags, error);
678
679 if (streq(name, "MemoryLimit"))
680 return bus_cgroup_set_memory(u, name, &c->memory_limit, message, flags, error);
681
682 if (streq(name, "MemoryMinScale"))
683 return bus_cgroup_set_memory_scale(u, name, &c->memory_min, message, flags, error);
684
685 if (streq(name, "MemoryLowScale"))
686 return bus_cgroup_set_memory_scale(u, name, &c->memory_low, message, flags, error);
687
688 if (streq(name, "DefaultMemoryLowScale"))
689 return bus_cgroup_set_memory_scale(u, name, &c->default_memory_low, message, flags, error);
690
691 if (streq(name, "MemoryHighScale"))
692 return bus_cgroup_set_memory_scale(u, name, &c->memory_high, message, flags, error);
693
694 if (streq(name, "MemorySwapMaxScale"))
695 return bus_cgroup_set_swap_scale(u, name, &c->memory_swap_max, message, flags, error);
696
697 if (streq(name, "MemoryMaxScale"))
698 return bus_cgroup_set_memory_scale(u, name, &c->memory_max, message, flags, error);
699
700 if (streq(name, "MemoryLimitScale"))
701 return bus_cgroup_set_memory_scale(u, name, &c->memory_limit, message, flags, error);
702
703 if (streq(name, "TasksAccounting"))
704 return bus_cgroup_set_boolean(u, name, &c->tasks_accounting, CGROUP_MASK_PIDS, message, flags, error);
705
706 if (streq(name, "TasksMax"))
707 return bus_cgroup_set_tasks_max(u, name, &c->tasks_max, message, flags, error);
708
709 if (streq(name, "TasksMaxScale"))
710 return bus_cgroup_set_tasks_max_scale(u, name, &c->tasks_max, message, flags, error);
711
712 if (streq(name, "CPUQuotaPerSecUSec")) {
713 uint64_t u64;
714
715 r = sd_bus_message_read(message, "t", &u64);
716 if (r < 0)
717 return r;
718
719 if (u64 <= 0)
720 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "CPUQuotaPerSecUSec= value out of range");
721
722 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
723 c->cpu_quota_per_sec_usec = u64;
724 u->warned_clamping_cpu_quota_period = false;
725 unit_invalidate_cgroup(u, CGROUP_MASK_CPU);
726
727 if (c->cpu_quota_per_sec_usec == USEC_INFINITY)
728 unit_write_setting(u, flags, "CPUQuota", "CPUQuota=");
729 else
730 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
731 * purpose here. */
732 unit_write_settingf(u, flags, "CPUQuota",
733 "CPUQuota=%0.f%%",
734 (double) (c->cpu_quota_per_sec_usec / 10000));
735 }
736
737 return 1;
738
739 } else if (streq(name, "CPUQuotaPeriodUSec")) {
740 uint64_t u64;
741
742 r = sd_bus_message_read(message, "t", &u64);
743 if (r < 0)
744 return r;
745
746 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
747 c->cpu_quota_period_usec = u64;
748 u->warned_clamping_cpu_quota_period = false;
749 unit_invalidate_cgroup(u, CGROUP_MASK_CPU);
750 if (c->cpu_quota_period_usec == USEC_INFINITY)
751 unit_write_setting(u, flags, "CPUQuotaPeriodSec", "CPUQuotaPeriodSec=");
752 else {
753 char v[FORMAT_TIMESPAN_MAX];
754 unit_write_settingf(u, flags, "CPUQuotaPeriodSec",
755 "CPUQuotaPeriodSec=%s",
756 format_timespan(v, sizeof(v), c->cpu_quota_period_usec, 1));
757 }
758 }
759
760 return 1;
761
762 } else if ((iol_type = cgroup_io_limit_type_from_string(name)) >= 0) {
763 const char *path;
764 unsigned n = 0;
765 uint64_t u64;
766
767 r = sd_bus_message_enter_container(message, 'a', "(st)");
768 if (r < 0)
769 return r;
770
771 while ((r = sd_bus_message_read(message, "(st)", &path, &u64)) > 0) {
772
773 if (!path_is_normalized(path))
774 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
775
776 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
777 CGroupIODeviceLimit *a = NULL, *b;
778
779 LIST_FOREACH(device_limits, b, c->io_device_limits) {
780 if (path_equal(path, b->path)) {
781 a = b;
782 break;
783 }
784 }
785
786 if (!a) {
787 CGroupIOLimitType type;
788
789 a = new0(CGroupIODeviceLimit, 1);
790 if (!a)
791 return -ENOMEM;
792
793 a->path = strdup(path);
794 if (!a->path) {
795 free(a);
796 return -ENOMEM;
797 }
798
799 for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++)
800 a->limits[type] = cgroup_io_limit_defaults[type];
801
802 LIST_PREPEND(device_limits, c->io_device_limits, a);
803 }
804
805 a->limits[iol_type] = u64;
806 }
807
808 n++;
809 }
810 if (r < 0)
811 return r;
812
813 r = sd_bus_message_exit_container(message);
814 if (r < 0)
815 return r;
816
817 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
818 CGroupIODeviceLimit *a;
819 _cleanup_free_ char *buf = NULL;
820 _cleanup_fclose_ FILE *f = NULL;
821 size_t size = 0;
822
823 if (n == 0) {
824 LIST_FOREACH(device_limits, a, c->io_device_limits)
825 a->limits[iol_type] = cgroup_io_limit_defaults[iol_type];
826 }
827
828 unit_invalidate_cgroup(u, CGROUP_MASK_IO);
829
830 f = open_memstream_unlocked(&buf, &size);
831 if (!f)
832 return -ENOMEM;
833
834 fprintf(f, "%s=\n", name);
835 LIST_FOREACH(device_limits, a, c->io_device_limits)
836 if (a->limits[iol_type] != cgroup_io_limit_defaults[iol_type])
837 fprintf(f, "%s=%s %" PRIu64 "\n", name, a->path, a->limits[iol_type]);
838
839 r = fflush_and_check(f);
840 if (r < 0)
841 return r;
842 unit_write_setting(u, flags, name, buf);
843 }
844
845 return 1;
846
847 } else if (streq(name, "IODeviceWeight")) {
848 const char *path;
849 uint64_t weight;
850 unsigned n = 0;
851
852 r = sd_bus_message_enter_container(message, 'a', "(st)");
853 if (r < 0)
854 return r;
855
856 while ((r = sd_bus_message_read(message, "(st)", &path, &weight)) > 0) {
857
858 if (!path_is_normalized(path))
859 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
860
861 if (!CGROUP_WEIGHT_IS_OK(weight) || weight == CGROUP_WEIGHT_INVALID)
862 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "IODeviceWeight= value out of range");
863
864 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
865 CGroupIODeviceWeight *a = NULL, *b;
866
867 LIST_FOREACH(device_weights, b, c->io_device_weights) {
868 if (path_equal(b->path, path)) {
869 a = b;
870 break;
871 }
872 }
873
874 if (!a) {
875 a = new0(CGroupIODeviceWeight, 1);
876 if (!a)
877 return -ENOMEM;
878
879 a->path = strdup(path);
880 if (!a->path) {
881 free(a);
882 return -ENOMEM;
883 }
884 LIST_PREPEND(device_weights, c->io_device_weights, a);
885 }
886
887 a->weight = weight;
888 }
889
890 n++;
891 }
892
893 r = sd_bus_message_exit_container(message);
894 if (r < 0)
895 return r;
896
897 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
898 _cleanup_free_ char *buf = NULL;
899 _cleanup_fclose_ FILE *f = NULL;
900 CGroupIODeviceWeight *a;
901 size_t size = 0;
902
903 if (n == 0) {
904 while (c->io_device_weights)
905 cgroup_context_free_io_device_weight(c, c->io_device_weights);
906 }
907
908 unit_invalidate_cgroup(u, CGROUP_MASK_IO);
909
910 f = open_memstream_unlocked(&buf, &size);
911 if (!f)
912 return -ENOMEM;
913
914 fputs("IODeviceWeight=\n", f);
915 LIST_FOREACH(device_weights, a, c->io_device_weights)
916 fprintf(f, "IODeviceWeight=%s %" PRIu64 "\n", a->path, a->weight);
917
918 r = fflush_and_check(f);
919 if (r < 0)
920 return r;
921 unit_write_setting(u, flags, name, buf);
922 }
923
924 return 1;
925
926 } else if (streq(name, "IODeviceLatencyTargetUSec")) {
927 const char *path;
928 uint64_t target;
929 unsigned n = 0;
930
931 r = sd_bus_message_enter_container(message, 'a', "(st)");
932 if (r < 0)
933 return r;
934
935 while ((r = sd_bus_message_read(message, "(st)", &path, &target)) > 0) {
936
937 if (!path_is_normalized(path))
938 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
939
940 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
941 CGroupIODeviceLatency *a = NULL, *b;
942
943 LIST_FOREACH(device_latencies, b, c->io_device_latencies) {
944 if (path_equal(b->path, path)) {
945 a = b;
946 break;
947 }
948 }
949
950 if (!a) {
951 a = new0(CGroupIODeviceLatency, 1);
952 if (!a)
953 return -ENOMEM;
954
955 a->path = strdup(path);
956 if (!a->path) {
957 free(a);
958 return -ENOMEM;
959 }
960 LIST_PREPEND(device_latencies, c->io_device_latencies, a);
961 }
962
963 a->target_usec = target;
964 }
965
966 n++;
967 }
968
969 r = sd_bus_message_exit_container(message);
970 if (r < 0)
971 return r;
972
973 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
974 _cleanup_free_ char *buf = NULL;
975 _cleanup_fclose_ FILE *f = NULL;
976 char ts[FORMAT_TIMESPAN_MAX];
977 CGroupIODeviceLatency *a;
978 size_t size = 0;
979
980 if (n == 0) {
981 while (c->io_device_latencies)
982 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
983 }
984
985 unit_invalidate_cgroup(u, CGROUP_MASK_IO);
986
987 f = open_memstream_unlocked(&buf, &size);
988 if (!f)
989 return -ENOMEM;
990
991 fputs("IODeviceLatencyTargetSec=\n", f);
992 LIST_FOREACH(device_latencies, a, c->io_device_latencies)
993 fprintf(f, "IODeviceLatencyTargetSec=%s %s\n",
994 a->path, format_timespan(ts, sizeof(ts), a->target_usec, 1));
995
996 r = fflush_and_check(f);
997 if (r < 0)
998 return r;
999 unit_write_setting(u, flags, name, buf);
1000 }
1001
1002 return 1;
1003
1004 } else if (STR_IN_SET(name, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
1005 const char *path;
1006 bool read = true;
1007 unsigned n = 0;
1008 uint64_t u64;
1009
1010 if (streq(name, "BlockIOWriteBandwidth"))
1011 read = false;
1012
1013 r = sd_bus_message_enter_container(message, 'a', "(st)");
1014 if (r < 0)
1015 return r;
1016
1017 while ((r = sd_bus_message_read(message, "(st)", &path, &u64)) > 0) {
1018
1019 if (!path_is_normalized(path))
1020 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
1021
1022 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1023 CGroupBlockIODeviceBandwidth *a = NULL, *b;
1024
1025 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
1026 if (path_equal(path, b->path)) {
1027 a = b;
1028 break;
1029 }
1030 }
1031
1032 if (!a) {
1033 a = new0(CGroupBlockIODeviceBandwidth, 1);
1034 if (!a)
1035 return -ENOMEM;
1036
1037 a->rbps = CGROUP_LIMIT_MAX;
1038 a->wbps = CGROUP_LIMIT_MAX;
1039 a->path = strdup(path);
1040 if (!a->path) {
1041 free(a);
1042 return -ENOMEM;
1043 }
1044
1045 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, a);
1046 }
1047
1048 if (read)
1049 a->rbps = u64;
1050 else
1051 a->wbps = u64;
1052 }
1053
1054 n++;
1055 }
1056 if (r < 0)
1057 return r;
1058
1059 r = sd_bus_message_exit_container(message);
1060 if (r < 0)
1061 return r;
1062
1063 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1064 CGroupBlockIODeviceBandwidth *a;
1065 _cleanup_free_ char *buf = NULL;
1066 _cleanup_fclose_ FILE *f = NULL;
1067 size_t size = 0;
1068
1069 if (n == 0) {
1070 LIST_FOREACH(device_bandwidths, a, c->blockio_device_bandwidths) {
1071 if (read)
1072 a->rbps = CGROUP_LIMIT_MAX;
1073 else
1074 a->wbps = CGROUP_LIMIT_MAX;
1075 }
1076 }
1077
1078 unit_invalidate_cgroup(u, CGROUP_MASK_BLKIO);
1079
1080 f = open_memstream_unlocked(&buf, &size);
1081 if (!f)
1082 return -ENOMEM;
1083
1084 if (read) {
1085 fputs("BlockIOReadBandwidth=\n", f);
1086 LIST_FOREACH(device_bandwidths, a, c->blockio_device_bandwidths)
1087 if (a->rbps != CGROUP_LIMIT_MAX)
1088 fprintf(f, "BlockIOReadBandwidth=%s %" PRIu64 "\n", a->path, a->rbps);
1089 } else {
1090 fputs("BlockIOWriteBandwidth=\n", f);
1091 LIST_FOREACH(device_bandwidths, a, c->blockio_device_bandwidths)
1092 if (a->wbps != CGROUP_LIMIT_MAX)
1093 fprintf(f, "BlockIOWriteBandwidth=%s %" PRIu64 "\n", a->path, a->wbps);
1094 }
1095
1096 r = fflush_and_check(f);
1097 if (r < 0)
1098 return r;
1099
1100 unit_write_setting(u, flags, name, buf);
1101 }
1102
1103 return 1;
1104
1105 } else if (streq(name, "BlockIODeviceWeight")) {
1106 const char *path;
1107 uint64_t weight;
1108 unsigned n = 0;
1109
1110 r = sd_bus_message_enter_container(message, 'a', "(st)");
1111 if (r < 0)
1112 return r;
1113
1114 while ((r = sd_bus_message_read(message, "(st)", &path, &weight)) > 0) {
1115
1116 if (!path_is_normalized(path))
1117 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
1118
1119 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight) || weight == CGROUP_BLKIO_WEIGHT_INVALID)
1120 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "BlockIODeviceWeight= out of range");
1121
1122 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1123 CGroupBlockIODeviceWeight *a = NULL, *b;
1124
1125 LIST_FOREACH(device_weights, b, c->blockio_device_weights) {
1126 if (path_equal(b->path, path)) {
1127 a = b;
1128 break;
1129 }
1130 }
1131
1132 if (!a) {
1133 a = new0(CGroupBlockIODeviceWeight, 1);
1134 if (!a)
1135 return -ENOMEM;
1136
1137 a->path = strdup(path);
1138 if (!a->path) {
1139 free(a);
1140 return -ENOMEM;
1141 }
1142 LIST_PREPEND(device_weights, c->blockio_device_weights, a);
1143 }
1144
1145 a->weight = weight;
1146 }
1147
1148 n++;
1149 }
1150
1151 r = sd_bus_message_exit_container(message);
1152 if (r < 0)
1153 return r;
1154
1155 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1156 _cleanup_free_ char *buf = NULL;
1157 _cleanup_fclose_ FILE *f = NULL;
1158 CGroupBlockIODeviceWeight *a;
1159 size_t size = 0;
1160
1161 if (n == 0) {
1162 while (c->blockio_device_weights)
1163 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
1164 }
1165
1166 unit_invalidate_cgroup(u, CGROUP_MASK_BLKIO);
1167
1168 f = open_memstream_unlocked(&buf, &size);
1169 if (!f)
1170 return -ENOMEM;
1171
1172 fputs("BlockIODeviceWeight=\n", f);
1173 LIST_FOREACH(device_weights, a, c->blockio_device_weights)
1174 fprintf(f, "BlockIODeviceWeight=%s %" PRIu64 "\n", a->path, a->weight);
1175
1176 r = fflush_and_check(f);
1177 if (r < 0)
1178 return r;
1179
1180 unit_write_setting(u, flags, name, buf);
1181 }
1182
1183 return 1;
1184
1185 } else if (streq(name, "DevicePolicy")) {
1186 const char *policy;
1187 CGroupDevicePolicy p;
1188
1189 r = sd_bus_message_read(message, "s", &policy);
1190 if (r < 0)
1191 return r;
1192
1193 p = cgroup_device_policy_from_string(policy);
1194 if (p < 0)
1195 return -EINVAL;
1196
1197 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1198 c->device_policy = p;
1199 unit_invalidate_cgroup(u, CGROUP_MASK_DEVICES);
1200 unit_write_settingf(u, flags, name, "DevicePolicy=%s", policy);
1201 }
1202
1203 return 1;
1204
1205 } else if (streq(name, "DeviceAllow")) {
1206 const char *path, *rwm;
1207 unsigned n = 0;
1208
1209 r = sd_bus_message_enter_container(message, 'a', "(ss)");
1210 if (r < 0)
1211 return r;
1212
1213 while ((r = sd_bus_message_read(message, "(ss)", &path, &rwm)) > 0) {
1214
1215 if (!valid_device_allow_pattern(path) || strpbrk(path, WHITESPACE))
1216 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "DeviceAllow= requires device node or pattern");
1217
1218 if (isempty(rwm))
1219 rwm = "rwm";
1220 else if (!in_charset(rwm, "rwm"))
1221 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "DeviceAllow= requires combination of rwm flags");
1222
1223 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1224 CGroupDeviceAllow *a = NULL, *b;
1225
1226 LIST_FOREACH(device_allow, b, c->device_allow) {
1227 if (path_equal(b->path, path)) {
1228 a = b;
1229 break;
1230 }
1231 }
1232
1233 if (!a) {
1234 a = new0(CGroupDeviceAllow, 1);
1235 if (!a)
1236 return -ENOMEM;
1237
1238 a->path = strdup(path);
1239 if (!a->path) {
1240 free(a);
1241 return -ENOMEM;
1242 }
1243
1244 LIST_PREPEND(device_allow, c->device_allow, a);
1245 }
1246
1247 a->r = !!strchr(rwm, 'r');
1248 a->w = !!strchr(rwm, 'w');
1249 a->m = !!strchr(rwm, 'm');
1250 }
1251
1252 n++;
1253 }
1254 if (r < 0)
1255 return r;
1256
1257 r = sd_bus_message_exit_container(message);
1258 if (r < 0)
1259 return r;
1260
1261 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1262 _cleanup_free_ char *buf = NULL;
1263 _cleanup_fclose_ FILE *f = NULL;
1264 CGroupDeviceAllow *a;
1265 size_t size = 0;
1266
1267 if (n == 0) {
1268 while (c->device_allow)
1269 cgroup_context_free_device_allow(c, c->device_allow);
1270 }
1271
1272 unit_invalidate_cgroup(u, CGROUP_MASK_DEVICES);
1273
1274 f = open_memstream_unlocked(&buf, &size);
1275 if (!f)
1276 return -ENOMEM;
1277
1278 fputs("DeviceAllow=\n", f);
1279 LIST_FOREACH(device_allow, a, c->device_allow)
1280 fprintf(f, "DeviceAllow=%s %s%s%s\n", a->path, a->r ? "r" : "", a->w ? "w" : "", a->m ? "m" : "");
1281
1282 r = fflush_and_check(f);
1283 if (r < 0)
1284 return r;
1285 unit_write_setting(u, flags, name, buf);
1286 }
1287
1288 return 1;
1289
1290 } else if (streq(name, "IPAccounting")) {
1291 int b;
1292
1293 r = sd_bus_message_read(message, "b", &b);
1294 if (r < 0)
1295 return r;
1296
1297 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1298 c->ip_accounting = b;
1299
1300 unit_invalidate_cgroup_bpf(u);
1301 unit_write_settingf(u, flags, name, "IPAccounting=%s", yes_no(b));
1302 }
1303
1304 return 1;
1305
1306 } else if (STR_IN_SET(name, "IPAddressAllow", "IPAddressDeny")) {
1307 IPAddressAccessItem **list;
1308 size_t n = 0;
1309
1310 list = streq(name, "IPAddressAllow") ? &c->ip_address_allow : &c->ip_address_deny;
1311
1312 r = sd_bus_message_enter_container(message, 'a', "(iayu)");
1313 if (r < 0)
1314 return r;
1315
1316 for (;;) {
1317 const void *ap;
1318 int32_t family;
1319 uint32_t prefixlen;
1320 size_t an;
1321
1322 r = sd_bus_message_enter_container(message, 'r', "iayu");
1323 if (r < 0)
1324 return r;
1325 if (r == 0)
1326 break;
1327
1328 r = sd_bus_message_read(message, "i", &family);
1329 if (r < 0)
1330 return r;
1331
1332 if (!IN_SET(family, AF_INET, AF_INET6))
1333 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= expects IPv4 or IPv6 addresses only.", name);
1334
1335 r = sd_bus_message_read_array(message, 'y', &ap, &an);
1336 if (r < 0)
1337 return r;
1338
1339 if (an != FAMILY_ADDRESS_SIZE(family))
1340 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1341 af_to_name(family), FAMILY_ADDRESS_SIZE(family), an);
1342
1343 r = sd_bus_message_read(message, "u", &prefixlen);
1344 if (r < 0)
1345 return r;
1346
1347 if (prefixlen > FAMILY_ADDRESS_SIZE(family)*8)
1348 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Prefix length %" PRIu32 " too large for address family %s.", prefixlen, af_to_name(family));
1349
1350 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1351 IPAddressAccessItem *item;
1352
1353 item = new0(IPAddressAccessItem, 1);
1354 if (!item)
1355 return -ENOMEM;
1356
1357 item->family = family;
1358 item->prefixlen = prefixlen;
1359 memcpy(&item->address, ap, an);
1360
1361 LIST_PREPEND(items, *list, item);
1362 }
1363
1364 r = sd_bus_message_exit_container(message);
1365 if (r < 0)
1366 return r;
1367
1368 n++;
1369 }
1370
1371 r = sd_bus_message_exit_container(message);
1372 if (r < 0)
1373 return r;
1374
1375 *list = ip_address_access_reduce(*list);
1376
1377 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1378 _cleanup_free_ char *buf = NULL;
1379 _cleanup_fclose_ FILE *f = NULL;
1380 IPAddressAccessItem *item;
1381 size_t size = 0;
1382
1383 if (n == 0)
1384 *list = ip_address_access_free_all(*list);
1385
1386 unit_invalidate_cgroup_bpf(u);
1387 f = open_memstream_unlocked(&buf, &size);
1388 if (!f)
1389 return -ENOMEM;
1390
1391 fputs(name, f);
1392 fputs("=\n", f);
1393
1394 LIST_FOREACH(items, item, *list) {
1395 char buffer[CONST_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1396
1397 errno = 0;
1398 if (!inet_ntop(item->family, &item->address, buffer, sizeof(buffer)))
1399 return errno > 0 ? -errno : -EINVAL;
1400
1401 fprintf(f, "%s=%s/%u\n", name, buffer, item->prefixlen);
1402 }
1403
1404 r = fflush_and_check(f);
1405 if (r < 0)
1406 return r;
1407
1408 unit_write_setting(u, flags, name, buf);
1409
1410 if (*list) {
1411 r = bpf_firewall_supported();
1412 if (r < 0)
1413 return r;
1414 if (r == BPF_FIREWALL_UNSUPPORTED) {
1415 static bool warned = false;
1416
1417 log_full(warned ? LOG_DEBUG : LOG_WARNING,
1418 "Transient unit %s configures an IP firewall, but the local system does not support BPF/cgroup firewalling.\n"
1419 "Proceeding WITHOUT firewalling in effect! (This warning is only shown for the first started transient unit using IP firewalling.)", u->id);
1420
1421 warned = true;
1422 }
1423 }
1424 }
1425
1426 return 1;
1427 }
1428
1429 if (u->transient && u->load_state == UNIT_STUB)
1430 return bus_cgroup_set_transient_property(u, c, name, message, flags, error);
1431
1432 return 0;
1433 }