]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus-cgroup.c
cgroup: Readd some plumbing for DefaultMemoryMin
[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("DefaultMemoryMin", "t", NULL, offsetof(CGroupContext, default_memory_min), 0),
352 SD_BUS_PROPERTY("MemoryMin", "t", NULL, offsetof(CGroupContext, memory_min), 0),
353 SD_BUS_PROPERTY("MemoryLow", "t", NULL, offsetof(CGroupContext, memory_low), 0),
354 SD_BUS_PROPERTY("MemoryHigh", "t", NULL, offsetof(CGroupContext, memory_high), 0),
355 SD_BUS_PROPERTY("MemoryMax", "t", NULL, offsetof(CGroupContext, memory_max), 0),
356 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL, offsetof(CGroupContext, memory_swap_max), 0),
357 SD_BUS_PROPERTY("MemoryLimit", "t", NULL, offsetof(CGroupContext, memory_limit), 0),
358 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy, offsetof(CGroupContext, device_policy), 0),
359 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow, 0, 0),
360 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, tasks_accounting), 0),
361 SD_BUS_PROPERTY("TasksMax", "t", NULL, offsetof(CGroupContext, tasks_max), 0),
362 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, ip_accounting), 0),
363 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access, offsetof(CGroupContext, ip_address_allow), 0),
364 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access, offsetof(CGroupContext, ip_address_deny), 0),
365 SD_BUS_PROPERTY("DisableControllers", "as", property_get_cgroup_mask, offsetof(CGroupContext, disable_controllers), 0),
366 SD_BUS_VTABLE_END
367 };
368
369 static int bus_cgroup_set_transient_property(
370 Unit *u,
371 CGroupContext *c,
372 const char *name,
373 sd_bus_message *message,
374 UnitWriteFlags flags,
375 sd_bus_error *error) {
376
377 int r;
378
379 assert(u);
380 assert(c);
381 assert(name);
382 assert(message);
383
384 flags |= UNIT_PRIVATE;
385
386 if (streq(name, "Delegate")) {
387 int b;
388
389 if (!UNIT_VTABLE(u)->can_delegate)
390 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delegation not available for unit type");
391
392 r = sd_bus_message_read(message, "b", &b);
393 if (r < 0)
394 return r;
395
396 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
397 c->delegate = b;
398 c->delegate_controllers = b ? _CGROUP_MASK_ALL : 0;
399
400 unit_write_settingf(u, flags, name, "Delegate=%s", yes_no(b));
401 }
402
403 return 1;
404
405 } else if (STR_IN_SET(name, "DelegateControllers", "DisableControllers")) {
406 CGroupMask mask = 0;
407
408 if (streq(name, "DelegateControllers") && !UNIT_VTABLE(u)->can_delegate)
409 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delegation not available for unit type");
410
411 r = sd_bus_message_enter_container(message, 'a', "s");
412 if (r < 0)
413 return r;
414
415 for (;;) {
416 CGroupController cc;
417 const char *t;
418
419 r = sd_bus_message_read(message, "s", &t);
420 if (r < 0)
421 return r;
422 if (r == 0)
423 break;
424
425 cc = cgroup_controller_from_string(t);
426 if (cc < 0)
427 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown cgroup controller '%s'", t);
428
429 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
430 }
431
432 r = sd_bus_message_exit_container(message);
433 if (r < 0)
434 return r;
435
436 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
437 _cleanup_free_ char *t = NULL;
438
439 r = cg_mask_to_string(mask, &t);
440 if (r < 0)
441 return r;
442
443 if (streq(name, "DelegateControllers")) {
444
445 c->delegate = true;
446 if (mask == 0)
447 c->delegate_controllers = 0;
448 else
449 c->delegate_controllers |= mask;
450
451 unit_write_settingf(u, flags, name, "Delegate=%s", strempty(t));
452
453 } else if (streq(name, "DisableControllers")) {
454
455 if (mask == 0)
456 c->disable_controllers = 0;
457 else
458 c->disable_controllers |= mask;
459
460 unit_write_settingf(u, flags, name, "%s=%s", name, strempty(t));
461 }
462 }
463
464 return 1;
465 }
466
467 return 0;
468 }
469
470 static int bus_cgroup_set_boolean(
471 Unit *u,
472 const char *name,
473 bool *p,
474 CGroupMask mask,
475 sd_bus_message *message,
476 UnitWriteFlags flags,
477 sd_bus_error *error) {
478
479 int b, r;
480
481 assert(p);
482
483 r = sd_bus_message_read(message, "b", &b);
484 if (r < 0)
485 return r;
486
487 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
488 *p = b;
489 unit_invalidate_cgroup(u, mask);
490 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(b));
491 }
492
493 return 1;
494 }
495
496 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
497 static int bus_cgroup_set_##function( \
498 Unit *u, \
499 const char *name, \
500 uint64_t *p, \
501 sd_bus_message *message, \
502 UnitWriteFlags flags, \
503 sd_bus_error *error) { \
504 \
505 uint64_t v; \
506 int r; \
507 \
508 assert(p); \
509 \
510 r = sd_bus_message_read(message, "t", &v); \
511 if (r < 0) \
512 return r; \
513 \
514 if (!check(v)) \
515 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
516 "Value specified in %s is out of range", name); \
517 \
518 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
519 *p = v; \
520 unit_invalidate_cgroup(u, (mask)); \
521 \
522 if (v == (val)) \
523 unit_write_settingf(u, flags, name, \
524 "%s=", name); \
525 else \
526 unit_write_settingf(u, flags, name, \
527 "%s=%" PRIu64, name, v); \
528 } \
529 \
530 return 1; \
531 }
532
533 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
534 static int bus_cgroup_set_##function( \
535 Unit *u, \
536 const char *name, \
537 uint64_t *p, \
538 sd_bus_message *message, \
539 UnitWriteFlags flags, \
540 sd_bus_error *error) { \
541 \
542 uint64_t v; \
543 int r; \
544 \
545 assert(p); \
546 \
547 r = sd_bus_message_read(message, "t", &v); \
548 if (r < 0) \
549 return r; \
550 \
551 if (v < minimum) \
552 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
553 "Value specified in %s is out of range", name); \
554 \
555 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
556 *p = v; \
557 unit_invalidate_cgroup(u, (mask)); \
558 \
559 if (v == CGROUP_LIMIT_MAX) \
560 unit_write_settingf(u, flags, name, \
561 "%s=infinity", name); \
562 else \
563 unit_write_settingf(u, flags, name, \
564 "%s=%" PRIu64, name, v); \
565 } \
566 \
567 return 1; \
568 } \
569 static int bus_cgroup_set_##function##_scale( \
570 Unit *u, \
571 const char *name, \
572 uint64_t *p, \
573 sd_bus_message *message, \
574 UnitWriteFlags flags, \
575 sd_bus_error *error) { \
576 \
577 uint64_t v; \
578 uint32_t raw; \
579 int r; \
580 \
581 assert(p); \
582 \
583 r = sd_bus_message_read(message, "u", &raw); \
584 if (r < 0) \
585 return r; \
586 \
587 v = scale(raw, UINT32_MAX); \
588 if (v < minimum || v >= UINT64_MAX) \
589 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
590 "Value specified in %s is out of range", name); \
591 \
592 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
593 const char *e; \
594 \
595 *p = v; \
596 unit_invalidate_cgroup(u, (mask)); \
597 \
598 /* Chop off suffix */ \
599 assert_se(e = endswith(name, "Scale")); \
600 name = strndupa(name, e - name); \
601 \
602 unit_write_settingf(u, flags, name, "%s=%" PRIu32 "%%", name, \
603 (uint32_t) (DIV_ROUND_UP((uint64_t) raw * 100U, (uint64_t) UINT32_MAX))); \
604 } \
605 \
606 return 1; \
607 }
608
609 #pragma GCC diagnostic push
610 #pragma GCC diagnostic ignored "-Wtype-limits"
611 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_weight, CGROUP_MASK_CPU, CGROUP_WEIGHT_IS_OK, CGROUP_WEIGHT_INVALID);
612 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares, CGROUP_MASK_CPU, CGROUP_CPU_SHARES_IS_OK, CGROUP_CPU_SHARES_INVALID);
613 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight, CGROUP_MASK_IO, CGROUP_WEIGHT_IS_OK, CGROUP_WEIGHT_INVALID);
614 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight, CGROUP_MASK_BLKIO, CGROUP_BLKIO_WEIGHT_IS_OK, CGROUP_BLKIO_WEIGHT_INVALID);
615 BUS_DEFINE_SET_CGROUP_LIMIT(memory, CGROUP_MASK_MEMORY, physical_memory_scale, 1);
616 BUS_DEFINE_SET_CGROUP_LIMIT(swap, CGROUP_MASK_MEMORY, physical_memory_scale, 0);
617 BUS_DEFINE_SET_CGROUP_LIMIT(tasks_max, CGROUP_MASK_PIDS, system_tasks_max_scale, 1);
618 #pragma GCC diagnostic pop
619
620 int bus_cgroup_set_property(
621 Unit *u,
622 CGroupContext *c,
623 const char *name,
624 sd_bus_message *message,
625 UnitWriteFlags flags,
626 sd_bus_error *error) {
627
628 CGroupIOLimitType iol_type;
629 int r;
630
631 assert(u);
632 assert(c);
633 assert(name);
634 assert(message);
635
636 flags |= UNIT_PRIVATE;
637
638 if (streq(name, "CPUAccounting"))
639 return bus_cgroup_set_boolean(u, name, &c->cpu_accounting, get_cpu_accounting_mask(), message, flags, error);
640
641 if (streq(name, "CPUWeight"))
642 return bus_cgroup_set_cpu_weight(u, name, &c->cpu_weight, message, flags, error);
643
644 if (streq(name, "StartupCPUWeight"))
645 return bus_cgroup_set_cpu_weight(u, name, &c->startup_cpu_weight, message, flags, error);
646
647 if (streq(name, "CPUShares"))
648 return bus_cgroup_set_cpu_shares(u, name, &c->cpu_shares, message, flags, error);
649
650 if (streq(name, "StartupCPUShares"))
651 return bus_cgroup_set_cpu_shares(u, name, &c->startup_cpu_shares, message, flags, error);
652
653 if (streq(name, "IOAccounting"))
654 return bus_cgroup_set_boolean(u, name, &c->io_accounting, CGROUP_MASK_IO, message, flags, error);
655
656 if (streq(name, "IOWeight"))
657 return bus_cgroup_set_io_weight(u, name, &c->io_weight, message, flags, error);
658
659 if (streq(name, "StartupIOWeight"))
660 return bus_cgroup_set_io_weight(u, name, &c->startup_io_weight, message, flags, error);
661
662 if (streq(name, "BlockIOAccounting"))
663 return bus_cgroup_set_boolean(u, name, &c->blockio_accounting, CGROUP_MASK_BLKIO, message, flags, error);
664
665 if (streq(name, "BlockIOWeight"))
666 return bus_cgroup_set_blockio_weight(u, name, &c->blockio_weight, message, flags, error);
667
668 if (streq(name, "StartupBlockIOWeight"))
669 return bus_cgroup_set_blockio_weight(u, name, &c->startup_blockio_weight, message, flags, error);
670
671 if (streq(name, "MemoryAccounting"))
672 return bus_cgroup_set_boolean(u, name, &c->memory_accounting, CGROUP_MASK_MEMORY, message, flags, error);
673
674 if (streq(name, "MemoryMin"))
675 return bus_cgroup_set_memory(u, name, &c->memory_min, message, flags, error);
676
677 if (streq(name, "MemoryLow"))
678 return bus_cgroup_set_memory(u, name, &c->memory_low, message, flags, error);
679
680 if (streq(name, "DefaultMemoryMin"))
681 return bus_cgroup_set_memory(u, name, &c->default_memory_min, message, flags, error);
682
683 if (streq(name, "DefaultMemoryLow"))
684 return bus_cgroup_set_memory(u, name, &c->default_memory_low, message, flags, error);
685
686 if (streq(name, "MemoryHigh"))
687 return bus_cgroup_set_memory(u, name, &c->memory_high, message, flags, error);
688
689 if (streq(name, "MemorySwapMax"))
690 return bus_cgroup_set_swap(u, name, &c->memory_swap_max, message, flags, error);
691
692 if (streq(name, "MemoryMax"))
693 return bus_cgroup_set_memory(u, name, &c->memory_max, message, flags, error);
694
695 if (streq(name, "MemoryLimit"))
696 return bus_cgroup_set_memory(u, name, &c->memory_limit, message, flags, error);
697
698 if (streq(name, "MemoryMinScale"))
699 return bus_cgroup_set_memory_scale(u, name, &c->memory_min, message, flags, error);
700
701 if (streq(name, "MemoryLowScale"))
702 return bus_cgroup_set_memory_scale(u, name, &c->memory_low, message, flags, error);
703
704 if (streq(name, "DefaultMemoryMinScale"))
705 return bus_cgroup_set_memory_scale(u, name, &c->default_memory_min, message, flags, error);
706
707 if (streq(name, "DefaultMemoryLowScale"))
708 return bus_cgroup_set_memory_scale(u, name, &c->default_memory_low, message, flags, error);
709
710 if (streq(name, "MemoryHighScale"))
711 return bus_cgroup_set_memory_scale(u, name, &c->memory_high, message, flags, error);
712
713 if (streq(name, "MemorySwapMaxScale"))
714 return bus_cgroup_set_swap_scale(u, name, &c->memory_swap_max, message, flags, error);
715
716 if (streq(name, "MemoryMaxScale"))
717 return bus_cgroup_set_memory_scale(u, name, &c->memory_max, message, flags, error);
718
719 if (streq(name, "MemoryLimitScale"))
720 return bus_cgroup_set_memory_scale(u, name, &c->memory_limit, message, flags, error);
721
722 if (streq(name, "TasksAccounting"))
723 return bus_cgroup_set_boolean(u, name, &c->tasks_accounting, CGROUP_MASK_PIDS, message, flags, error);
724
725 if (streq(name, "TasksMax"))
726 return bus_cgroup_set_tasks_max(u, name, &c->tasks_max, message, flags, error);
727
728 if (streq(name, "TasksMaxScale"))
729 return bus_cgroup_set_tasks_max_scale(u, name, &c->tasks_max, message, flags, error);
730
731 if (streq(name, "CPUQuotaPerSecUSec")) {
732 uint64_t u64;
733
734 r = sd_bus_message_read(message, "t", &u64);
735 if (r < 0)
736 return r;
737
738 if (u64 <= 0)
739 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "CPUQuotaPerSecUSec= value out of range");
740
741 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
742 c->cpu_quota_per_sec_usec = u64;
743 u->warned_clamping_cpu_quota_period = false;
744 unit_invalidate_cgroup(u, CGROUP_MASK_CPU);
745
746 if (c->cpu_quota_per_sec_usec == USEC_INFINITY)
747 unit_write_setting(u, flags, "CPUQuota", "CPUQuota=");
748 else
749 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
750 * purpose here. */
751 unit_write_settingf(u, flags, "CPUQuota",
752 "CPUQuota=%0.f%%",
753 (double) (c->cpu_quota_per_sec_usec / 10000));
754 }
755
756 return 1;
757
758 } else if (streq(name, "CPUQuotaPeriodUSec")) {
759 uint64_t u64;
760
761 r = sd_bus_message_read(message, "t", &u64);
762 if (r < 0)
763 return r;
764
765 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
766 c->cpu_quota_period_usec = u64;
767 u->warned_clamping_cpu_quota_period = false;
768 unit_invalidate_cgroup(u, CGROUP_MASK_CPU);
769 if (c->cpu_quota_period_usec == USEC_INFINITY)
770 unit_write_setting(u, flags, "CPUQuotaPeriodSec", "CPUQuotaPeriodSec=");
771 else {
772 char v[FORMAT_TIMESPAN_MAX];
773 unit_write_settingf(u, flags, "CPUQuotaPeriodSec",
774 "CPUQuotaPeriodSec=%s",
775 format_timespan(v, sizeof(v), c->cpu_quota_period_usec, 1));
776 }
777 }
778
779 return 1;
780
781 } else if ((iol_type = cgroup_io_limit_type_from_string(name)) >= 0) {
782 const char *path;
783 unsigned n = 0;
784 uint64_t u64;
785
786 r = sd_bus_message_enter_container(message, 'a', "(st)");
787 if (r < 0)
788 return r;
789
790 while ((r = sd_bus_message_read(message, "(st)", &path, &u64)) > 0) {
791
792 if (!path_is_normalized(path))
793 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
794
795 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
796 CGroupIODeviceLimit *a = NULL, *b;
797
798 LIST_FOREACH(device_limits, b, c->io_device_limits) {
799 if (path_equal(path, b->path)) {
800 a = b;
801 break;
802 }
803 }
804
805 if (!a) {
806 CGroupIOLimitType type;
807
808 a = new0(CGroupIODeviceLimit, 1);
809 if (!a)
810 return -ENOMEM;
811
812 a->path = strdup(path);
813 if (!a->path) {
814 free(a);
815 return -ENOMEM;
816 }
817
818 for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++)
819 a->limits[type] = cgroup_io_limit_defaults[type];
820
821 LIST_PREPEND(device_limits, c->io_device_limits, a);
822 }
823
824 a->limits[iol_type] = u64;
825 }
826
827 n++;
828 }
829 if (r < 0)
830 return r;
831
832 r = sd_bus_message_exit_container(message);
833 if (r < 0)
834 return r;
835
836 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
837 CGroupIODeviceLimit *a;
838 _cleanup_free_ char *buf = NULL;
839 _cleanup_fclose_ FILE *f = NULL;
840 size_t size = 0;
841
842 if (n == 0) {
843 LIST_FOREACH(device_limits, a, c->io_device_limits)
844 a->limits[iol_type] = cgroup_io_limit_defaults[iol_type];
845 }
846
847 unit_invalidate_cgroup(u, CGROUP_MASK_IO);
848
849 f = open_memstream_unlocked(&buf, &size);
850 if (!f)
851 return -ENOMEM;
852
853 fprintf(f, "%s=\n", name);
854 LIST_FOREACH(device_limits, a, c->io_device_limits)
855 if (a->limits[iol_type] != cgroup_io_limit_defaults[iol_type])
856 fprintf(f, "%s=%s %" PRIu64 "\n", name, a->path, a->limits[iol_type]);
857
858 r = fflush_and_check(f);
859 if (r < 0)
860 return r;
861 unit_write_setting(u, flags, name, buf);
862 }
863
864 return 1;
865
866 } else if (streq(name, "IODeviceWeight")) {
867 const char *path;
868 uint64_t weight;
869 unsigned n = 0;
870
871 r = sd_bus_message_enter_container(message, 'a', "(st)");
872 if (r < 0)
873 return r;
874
875 while ((r = sd_bus_message_read(message, "(st)", &path, &weight)) > 0) {
876
877 if (!path_is_normalized(path))
878 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
879
880 if (!CGROUP_WEIGHT_IS_OK(weight) || weight == CGROUP_WEIGHT_INVALID)
881 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "IODeviceWeight= value out of range");
882
883 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
884 CGroupIODeviceWeight *a = NULL, *b;
885
886 LIST_FOREACH(device_weights, b, c->io_device_weights) {
887 if (path_equal(b->path, path)) {
888 a = b;
889 break;
890 }
891 }
892
893 if (!a) {
894 a = new0(CGroupIODeviceWeight, 1);
895 if (!a)
896 return -ENOMEM;
897
898 a->path = strdup(path);
899 if (!a->path) {
900 free(a);
901 return -ENOMEM;
902 }
903 LIST_PREPEND(device_weights, c->io_device_weights, a);
904 }
905
906 a->weight = weight;
907 }
908
909 n++;
910 }
911
912 r = sd_bus_message_exit_container(message);
913 if (r < 0)
914 return r;
915
916 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
917 _cleanup_free_ char *buf = NULL;
918 _cleanup_fclose_ FILE *f = NULL;
919 CGroupIODeviceWeight *a;
920 size_t size = 0;
921
922 if (n == 0) {
923 while (c->io_device_weights)
924 cgroup_context_free_io_device_weight(c, c->io_device_weights);
925 }
926
927 unit_invalidate_cgroup(u, CGROUP_MASK_IO);
928
929 f = open_memstream_unlocked(&buf, &size);
930 if (!f)
931 return -ENOMEM;
932
933 fputs("IODeviceWeight=\n", f);
934 LIST_FOREACH(device_weights, a, c->io_device_weights)
935 fprintf(f, "IODeviceWeight=%s %" PRIu64 "\n", a->path, a->weight);
936
937 r = fflush_and_check(f);
938 if (r < 0)
939 return r;
940 unit_write_setting(u, flags, name, buf);
941 }
942
943 return 1;
944
945 } else if (streq(name, "IODeviceLatencyTargetUSec")) {
946 const char *path;
947 uint64_t target;
948 unsigned n = 0;
949
950 r = sd_bus_message_enter_container(message, 'a', "(st)");
951 if (r < 0)
952 return r;
953
954 while ((r = sd_bus_message_read(message, "(st)", &path, &target)) > 0) {
955
956 if (!path_is_normalized(path))
957 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
958
959 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
960 CGroupIODeviceLatency *a = NULL, *b;
961
962 LIST_FOREACH(device_latencies, b, c->io_device_latencies) {
963 if (path_equal(b->path, path)) {
964 a = b;
965 break;
966 }
967 }
968
969 if (!a) {
970 a = new0(CGroupIODeviceLatency, 1);
971 if (!a)
972 return -ENOMEM;
973
974 a->path = strdup(path);
975 if (!a->path) {
976 free(a);
977 return -ENOMEM;
978 }
979 LIST_PREPEND(device_latencies, c->io_device_latencies, a);
980 }
981
982 a->target_usec = target;
983 }
984
985 n++;
986 }
987
988 r = sd_bus_message_exit_container(message);
989 if (r < 0)
990 return r;
991
992 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
993 _cleanup_free_ char *buf = NULL;
994 _cleanup_fclose_ FILE *f = NULL;
995 char ts[FORMAT_TIMESPAN_MAX];
996 CGroupIODeviceLatency *a;
997 size_t size = 0;
998
999 if (n == 0) {
1000 while (c->io_device_latencies)
1001 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
1002 }
1003
1004 unit_invalidate_cgroup(u, CGROUP_MASK_IO);
1005
1006 f = open_memstream_unlocked(&buf, &size);
1007 if (!f)
1008 return -ENOMEM;
1009
1010 fputs("IODeviceLatencyTargetSec=\n", f);
1011 LIST_FOREACH(device_latencies, a, c->io_device_latencies)
1012 fprintf(f, "IODeviceLatencyTargetSec=%s %s\n",
1013 a->path, format_timespan(ts, sizeof(ts), a->target_usec, 1));
1014
1015 r = fflush_and_check(f);
1016 if (r < 0)
1017 return r;
1018 unit_write_setting(u, flags, name, buf);
1019 }
1020
1021 return 1;
1022
1023 } else if (STR_IN_SET(name, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
1024 const char *path;
1025 bool read = true;
1026 unsigned n = 0;
1027 uint64_t u64;
1028
1029 if (streq(name, "BlockIOWriteBandwidth"))
1030 read = false;
1031
1032 r = sd_bus_message_enter_container(message, 'a', "(st)");
1033 if (r < 0)
1034 return r;
1035
1036 while ((r = sd_bus_message_read(message, "(st)", &path, &u64)) > 0) {
1037
1038 if (!path_is_normalized(path))
1039 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
1040
1041 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1042 CGroupBlockIODeviceBandwidth *a = NULL, *b;
1043
1044 LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) {
1045 if (path_equal(path, b->path)) {
1046 a = b;
1047 break;
1048 }
1049 }
1050
1051 if (!a) {
1052 a = new0(CGroupBlockIODeviceBandwidth, 1);
1053 if (!a)
1054 return -ENOMEM;
1055
1056 a->rbps = CGROUP_LIMIT_MAX;
1057 a->wbps = CGROUP_LIMIT_MAX;
1058 a->path = strdup(path);
1059 if (!a->path) {
1060 free(a);
1061 return -ENOMEM;
1062 }
1063
1064 LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, a);
1065 }
1066
1067 if (read)
1068 a->rbps = u64;
1069 else
1070 a->wbps = u64;
1071 }
1072
1073 n++;
1074 }
1075 if (r < 0)
1076 return r;
1077
1078 r = sd_bus_message_exit_container(message);
1079 if (r < 0)
1080 return r;
1081
1082 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1083 CGroupBlockIODeviceBandwidth *a;
1084 _cleanup_free_ char *buf = NULL;
1085 _cleanup_fclose_ FILE *f = NULL;
1086 size_t size = 0;
1087
1088 if (n == 0) {
1089 LIST_FOREACH(device_bandwidths, a, c->blockio_device_bandwidths) {
1090 if (read)
1091 a->rbps = CGROUP_LIMIT_MAX;
1092 else
1093 a->wbps = CGROUP_LIMIT_MAX;
1094 }
1095 }
1096
1097 unit_invalidate_cgroup(u, CGROUP_MASK_BLKIO);
1098
1099 f = open_memstream_unlocked(&buf, &size);
1100 if (!f)
1101 return -ENOMEM;
1102
1103 if (read) {
1104 fputs("BlockIOReadBandwidth=\n", f);
1105 LIST_FOREACH(device_bandwidths, a, c->blockio_device_bandwidths)
1106 if (a->rbps != CGROUP_LIMIT_MAX)
1107 fprintf(f, "BlockIOReadBandwidth=%s %" PRIu64 "\n", a->path, a->rbps);
1108 } else {
1109 fputs("BlockIOWriteBandwidth=\n", f);
1110 LIST_FOREACH(device_bandwidths, a, c->blockio_device_bandwidths)
1111 if (a->wbps != CGROUP_LIMIT_MAX)
1112 fprintf(f, "BlockIOWriteBandwidth=%s %" PRIu64 "\n", a->path, a->wbps);
1113 }
1114
1115 r = fflush_and_check(f);
1116 if (r < 0)
1117 return r;
1118
1119 unit_write_setting(u, flags, name, buf);
1120 }
1121
1122 return 1;
1123
1124 } else if (streq(name, "BlockIODeviceWeight")) {
1125 const char *path;
1126 uint64_t weight;
1127 unsigned n = 0;
1128
1129 r = sd_bus_message_enter_container(message, 'a', "(st)");
1130 if (r < 0)
1131 return r;
1132
1133 while ((r = sd_bus_message_read(message, "(st)", &path, &weight)) > 0) {
1134
1135 if (!path_is_normalized(path))
1136 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
1137
1138 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight) || weight == CGROUP_BLKIO_WEIGHT_INVALID)
1139 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "BlockIODeviceWeight= out of range");
1140
1141 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1142 CGroupBlockIODeviceWeight *a = NULL, *b;
1143
1144 LIST_FOREACH(device_weights, b, c->blockio_device_weights) {
1145 if (path_equal(b->path, path)) {
1146 a = b;
1147 break;
1148 }
1149 }
1150
1151 if (!a) {
1152 a = new0(CGroupBlockIODeviceWeight, 1);
1153 if (!a)
1154 return -ENOMEM;
1155
1156 a->path = strdup(path);
1157 if (!a->path) {
1158 free(a);
1159 return -ENOMEM;
1160 }
1161 LIST_PREPEND(device_weights, c->blockio_device_weights, a);
1162 }
1163
1164 a->weight = weight;
1165 }
1166
1167 n++;
1168 }
1169
1170 r = sd_bus_message_exit_container(message);
1171 if (r < 0)
1172 return r;
1173
1174 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1175 _cleanup_free_ char *buf = NULL;
1176 _cleanup_fclose_ FILE *f = NULL;
1177 CGroupBlockIODeviceWeight *a;
1178 size_t size = 0;
1179
1180 if (n == 0) {
1181 while (c->blockio_device_weights)
1182 cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights);
1183 }
1184
1185 unit_invalidate_cgroup(u, CGROUP_MASK_BLKIO);
1186
1187 f = open_memstream_unlocked(&buf, &size);
1188 if (!f)
1189 return -ENOMEM;
1190
1191 fputs("BlockIODeviceWeight=\n", f);
1192 LIST_FOREACH(device_weights, a, c->blockio_device_weights)
1193 fprintf(f, "BlockIODeviceWeight=%s %" PRIu64 "\n", a->path, a->weight);
1194
1195 r = fflush_and_check(f);
1196 if (r < 0)
1197 return r;
1198
1199 unit_write_setting(u, flags, name, buf);
1200 }
1201
1202 return 1;
1203
1204 } else if (streq(name, "DevicePolicy")) {
1205 const char *policy;
1206 CGroupDevicePolicy p;
1207
1208 r = sd_bus_message_read(message, "s", &policy);
1209 if (r < 0)
1210 return r;
1211
1212 p = cgroup_device_policy_from_string(policy);
1213 if (p < 0)
1214 return -EINVAL;
1215
1216 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1217 c->device_policy = p;
1218 unit_invalidate_cgroup(u, CGROUP_MASK_DEVICES);
1219 unit_write_settingf(u, flags, name, "DevicePolicy=%s", policy);
1220 }
1221
1222 return 1;
1223
1224 } else if (streq(name, "DeviceAllow")) {
1225 const char *path, *rwm;
1226 unsigned n = 0;
1227
1228 r = sd_bus_message_enter_container(message, 'a', "(ss)");
1229 if (r < 0)
1230 return r;
1231
1232 while ((r = sd_bus_message_read(message, "(ss)", &path, &rwm)) > 0) {
1233
1234 if (!valid_device_allow_pattern(path) || strpbrk(path, WHITESPACE))
1235 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "DeviceAllow= requires device node or pattern");
1236
1237 if (isempty(rwm))
1238 rwm = "rwm";
1239 else if (!in_charset(rwm, "rwm"))
1240 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "DeviceAllow= requires combination of rwm flags");
1241
1242 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1243 CGroupDeviceAllow *a = NULL, *b;
1244
1245 LIST_FOREACH(device_allow, b, c->device_allow) {
1246 if (path_equal(b->path, path)) {
1247 a = b;
1248 break;
1249 }
1250 }
1251
1252 if (!a) {
1253 a = new0(CGroupDeviceAllow, 1);
1254 if (!a)
1255 return -ENOMEM;
1256
1257 a->path = strdup(path);
1258 if (!a->path) {
1259 free(a);
1260 return -ENOMEM;
1261 }
1262
1263 LIST_PREPEND(device_allow, c->device_allow, a);
1264 }
1265
1266 a->r = !!strchr(rwm, 'r');
1267 a->w = !!strchr(rwm, 'w');
1268 a->m = !!strchr(rwm, 'm');
1269 }
1270
1271 n++;
1272 }
1273 if (r < 0)
1274 return r;
1275
1276 r = sd_bus_message_exit_container(message);
1277 if (r < 0)
1278 return r;
1279
1280 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1281 _cleanup_free_ char *buf = NULL;
1282 _cleanup_fclose_ FILE *f = NULL;
1283 CGroupDeviceAllow *a;
1284 size_t size = 0;
1285
1286 if (n == 0) {
1287 while (c->device_allow)
1288 cgroup_context_free_device_allow(c, c->device_allow);
1289 }
1290
1291 unit_invalidate_cgroup(u, CGROUP_MASK_DEVICES);
1292
1293 f = open_memstream_unlocked(&buf, &size);
1294 if (!f)
1295 return -ENOMEM;
1296
1297 fputs("DeviceAllow=\n", f);
1298 LIST_FOREACH(device_allow, a, c->device_allow)
1299 fprintf(f, "DeviceAllow=%s %s%s%s\n", a->path, a->r ? "r" : "", a->w ? "w" : "", a->m ? "m" : "");
1300
1301 r = fflush_and_check(f);
1302 if (r < 0)
1303 return r;
1304 unit_write_setting(u, flags, name, buf);
1305 }
1306
1307 return 1;
1308
1309 } else if (streq(name, "IPAccounting")) {
1310 int b;
1311
1312 r = sd_bus_message_read(message, "b", &b);
1313 if (r < 0)
1314 return r;
1315
1316 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1317 c->ip_accounting = b;
1318
1319 unit_invalidate_cgroup_bpf(u);
1320 unit_write_settingf(u, flags, name, "IPAccounting=%s", yes_no(b));
1321 }
1322
1323 return 1;
1324
1325 } else if (STR_IN_SET(name, "IPAddressAllow", "IPAddressDeny")) {
1326 IPAddressAccessItem **list;
1327 size_t n = 0;
1328
1329 list = streq(name, "IPAddressAllow") ? &c->ip_address_allow : &c->ip_address_deny;
1330
1331 r = sd_bus_message_enter_container(message, 'a', "(iayu)");
1332 if (r < 0)
1333 return r;
1334
1335 for (;;) {
1336 const void *ap;
1337 int32_t family;
1338 uint32_t prefixlen;
1339 size_t an;
1340
1341 r = sd_bus_message_enter_container(message, 'r', "iayu");
1342 if (r < 0)
1343 return r;
1344 if (r == 0)
1345 break;
1346
1347 r = sd_bus_message_read(message, "i", &family);
1348 if (r < 0)
1349 return r;
1350
1351 if (!IN_SET(family, AF_INET, AF_INET6))
1352 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s= expects IPv4 or IPv6 addresses only.", name);
1353
1354 r = sd_bus_message_read_array(message, 'y', &ap, &an);
1355 if (r < 0)
1356 return r;
1357
1358 if (an != FAMILY_ADDRESS_SIZE(family))
1359 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1360 af_to_name(family), FAMILY_ADDRESS_SIZE(family), an);
1361
1362 r = sd_bus_message_read(message, "u", &prefixlen);
1363 if (r < 0)
1364 return r;
1365
1366 if (prefixlen > FAMILY_ADDRESS_SIZE(family)*8)
1367 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));
1368
1369 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1370 IPAddressAccessItem *item;
1371
1372 item = new0(IPAddressAccessItem, 1);
1373 if (!item)
1374 return -ENOMEM;
1375
1376 item->family = family;
1377 item->prefixlen = prefixlen;
1378 memcpy(&item->address, ap, an);
1379
1380 LIST_PREPEND(items, *list, item);
1381 }
1382
1383 r = sd_bus_message_exit_container(message);
1384 if (r < 0)
1385 return r;
1386
1387 n++;
1388 }
1389
1390 r = sd_bus_message_exit_container(message);
1391 if (r < 0)
1392 return r;
1393
1394 *list = ip_address_access_reduce(*list);
1395
1396 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1397 _cleanup_free_ char *buf = NULL;
1398 _cleanup_fclose_ FILE *f = NULL;
1399 IPAddressAccessItem *item;
1400 size_t size = 0;
1401
1402 if (n == 0)
1403 *list = ip_address_access_free_all(*list);
1404
1405 unit_invalidate_cgroup_bpf(u);
1406 f = open_memstream_unlocked(&buf, &size);
1407 if (!f)
1408 return -ENOMEM;
1409
1410 fputs(name, f);
1411 fputs("=\n", f);
1412
1413 LIST_FOREACH(items, item, *list) {
1414 char buffer[CONST_MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1415
1416 errno = 0;
1417 if (!inet_ntop(item->family, &item->address, buffer, sizeof(buffer)))
1418 return errno > 0 ? -errno : -EINVAL;
1419
1420 fprintf(f, "%s=%s/%u\n", name, buffer, item->prefixlen);
1421 }
1422
1423 r = fflush_and_check(f);
1424 if (r < 0)
1425 return r;
1426
1427 unit_write_setting(u, flags, name, buf);
1428
1429 if (*list) {
1430 r = bpf_firewall_supported();
1431 if (r < 0)
1432 return r;
1433 if (r == BPF_FIREWALL_UNSUPPORTED) {
1434 static bool warned = false;
1435
1436 log_full(warned ? LOG_DEBUG : LOG_WARNING,
1437 "Transient unit %s configures an IP firewall, but the local system does not support BPF/cgroup firewalling.\n"
1438 "Proceeding WITHOUT firewalling in effect! (This warning is only shown for the first started transient unit using IP firewalling.)", u->id);
1439
1440 warned = true;
1441 }
1442 }
1443 }
1444
1445 return 1;
1446 }
1447
1448 if (streq(name, "DisableControllers") || (u->transient && u->load_state == UNIT_STUB))
1449 return bus_cgroup_set_transient_property(u, c, name, message, flags, error);
1450
1451 return 0;
1452 }