1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #include "bpf-firewall.h"
10 #include "cgroup-util.h"
12 #include "dbus-cgroup.h"
13 #include "dbus-util.h"
16 #include "path-util.h"
18 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_cgroup_device_policy
, cgroup_device_policy
, CGroupDevicePolicy
);
20 static int property_get_delegate_controllers(
23 const char *interface
,
25 sd_bus_message
*reply
,
27 sd_bus_error
*error
) {
29 CGroupContext
*c
= userdata
;
38 return sd_bus_message_append(reply
, "as", 0);
40 r
= sd_bus_message_open_container(reply
, 'a', "s");
44 for (cc
= 0; cc
< _CGROUP_CONTROLLER_MAX
; cc
++) {
45 if ((c
->delegate_controllers
& CGROUP_CONTROLLER_TO_MASK(cc
)) == 0)
48 r
= sd_bus_message_append(reply
, "s", cgroup_controller_to_string(cc
));
53 return sd_bus_message_close_container(reply
);
56 static int property_get_io_device_weight(
59 const char *interface
,
61 sd_bus_message
*reply
,
63 sd_bus_error
*error
) {
65 CGroupContext
*c
= userdata
;
66 CGroupIODeviceWeight
*w
;
73 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
77 LIST_FOREACH(device_weights
, w
, c
->io_device_weights
) {
78 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
83 return sd_bus_message_close_container(reply
);
86 static int property_get_io_device_limits(
89 const char *interface
,
91 sd_bus_message
*reply
,
93 sd_bus_error
*error
) {
95 CGroupContext
*c
= userdata
;
96 CGroupIODeviceLimit
*l
;
103 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
107 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
) {
108 CGroupIOLimitType type
;
110 type
= cgroup_io_limit_type_from_string(property
);
111 if (type
< 0 || l
->limits
[type
] == cgroup_io_limit_defaults
[type
])
114 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->limits
[type
]);
119 return sd_bus_message_close_container(reply
);
122 static int property_get_blockio_device_weight(
125 const char *interface
,
126 const char *property
,
127 sd_bus_message
*reply
,
129 sd_bus_error
*error
) {
131 CGroupContext
*c
= userdata
;
132 CGroupBlockIODeviceWeight
*w
;
139 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
143 LIST_FOREACH(device_weights
, w
, c
->blockio_device_weights
) {
144 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
149 return sd_bus_message_close_container(reply
);
152 static int property_get_blockio_device_bandwidths(
155 const char *interface
,
156 const char *property
,
157 sd_bus_message
*reply
,
159 sd_bus_error
*error
) {
161 CGroupContext
*c
= userdata
;
162 CGroupBlockIODeviceBandwidth
*b
;
169 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
173 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
176 if (streq(property
, "BlockIOReadBandwidth"))
181 if (v
== CGROUP_LIMIT_MAX
)
184 r
= sd_bus_message_append(reply
, "(st)", b
->path
, v
);
189 return sd_bus_message_close_container(reply
);
192 static int property_get_device_allow(
195 const char *interface
,
196 const char *property
,
197 sd_bus_message
*reply
,
199 sd_bus_error
*error
) {
201 CGroupContext
*c
= userdata
;
202 CGroupDeviceAllow
*a
;
209 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
213 LIST_FOREACH(device_allow
, a
, c
->device_allow
) {
226 r
= sd_bus_message_append(reply
, "(ss)", a
->path
, rwm
);
231 return sd_bus_message_close_container(reply
);
234 static int property_get_ip_address_access(
237 const char *interface
,
238 const char *property
,
239 sd_bus_message
*reply
,
241 sd_bus_error
*error
) {
243 IPAddressAccessItem
** items
= userdata
, *i
;
246 r
= sd_bus_message_open_container(reply
, 'a', "(iayu)");
250 LIST_FOREACH(items
, i
, *items
) {
252 r
= sd_bus_message_open_container(reply
, 'r', "iayu");
256 r
= sd_bus_message_append(reply
, "i", i
->family
);
260 r
= sd_bus_message_append_array(reply
, 'y', &i
->address
, FAMILY_ADDRESS_SIZE(i
->family
));
264 r
= sd_bus_message_append(reply
, "u", (uint32_t) i
->prefixlen
);
268 r
= sd_bus_message_close_container(reply
);
273 return sd_bus_message_close_container(reply
);
276 const sd_bus_vtable bus_cgroup_vtable
[] = {
277 SD_BUS_VTABLE_START(0),
278 SD_BUS_PROPERTY("Delegate", "b", bus_property_get_bool
, offsetof(CGroupContext
, delegate
), 0),
279 SD_BUS_PROPERTY("DelegateControllers", "as", property_get_delegate_controllers
, 0, 0),
280 SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, cpu_accounting
), 0),
281 SD_BUS_PROPERTY("CPUWeight", "t", NULL
, offsetof(CGroupContext
, cpu_weight
), 0),
282 SD_BUS_PROPERTY("StartupCPUWeight", "t", NULL
, offsetof(CGroupContext
, startup_cpu_weight
), 0),
283 SD_BUS_PROPERTY("CPUShares", "t", NULL
, offsetof(CGroupContext
, cpu_shares
), 0),
284 SD_BUS_PROPERTY("StartupCPUShares", "t", NULL
, offsetof(CGroupContext
, startup_cpu_shares
), 0),
285 SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_per_sec_usec
), 0),
286 SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, io_accounting
), 0),
287 SD_BUS_PROPERTY("IOWeight", "t", NULL
, offsetof(CGroupContext
, io_weight
), 0),
288 SD_BUS_PROPERTY("StartupIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_io_weight
), 0),
289 SD_BUS_PROPERTY("IODeviceWeight", "a(st)", property_get_io_device_weight
, 0, 0),
290 SD_BUS_PROPERTY("IOReadBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
291 SD_BUS_PROPERTY("IOWriteBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
292 SD_BUS_PROPERTY("IOReadIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
293 SD_BUS_PROPERTY("IOWriteIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
294 SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, blockio_accounting
), 0),
295 SD_BUS_PROPERTY("BlockIOWeight", "t", NULL
, offsetof(CGroupContext
, blockio_weight
), 0),
296 SD_BUS_PROPERTY("StartupBlockIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_blockio_weight
), 0),
297 SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight
, 0, 0),
298 SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
299 SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
300 SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, memory_accounting
), 0),
301 SD_BUS_PROPERTY("MemoryMin", "t", NULL
, offsetof(CGroupContext
, memory_min
), 0),
302 SD_BUS_PROPERTY("MemoryLow", "t", NULL
, offsetof(CGroupContext
, memory_low
), 0),
303 SD_BUS_PROPERTY("MemoryHigh", "t", NULL
, offsetof(CGroupContext
, memory_high
), 0),
304 SD_BUS_PROPERTY("MemoryMax", "t", NULL
, offsetof(CGroupContext
, memory_max
), 0),
305 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL
, offsetof(CGroupContext
, memory_swap_max
), 0),
306 SD_BUS_PROPERTY("MemoryLimit", "t", NULL
, offsetof(CGroupContext
, memory_limit
), 0),
307 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy
, offsetof(CGroupContext
, device_policy
), 0),
308 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow
, 0, 0),
309 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, tasks_accounting
), 0),
310 SD_BUS_PROPERTY("TasksMax", "t", NULL
, offsetof(CGroupContext
, tasks_max
), 0),
311 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, ip_accounting
), 0),
312 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_allow
), 0),
313 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_deny
), 0),
317 static int bus_cgroup_set_transient_property(
321 sd_bus_message
*message
,
322 UnitWriteFlags flags
,
323 sd_bus_error
*error
) {
332 flags
|= UNIT_PRIVATE
;
334 if (streq(name
, "Delegate")) {
337 if (!UNIT_VTABLE(u
)->can_delegate
)
338 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
340 r
= sd_bus_message_read(message
, "b", &b
);
344 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
346 c
->delegate_controllers
= b
? _CGROUP_MASK_ALL
: 0;
348 unit_write_settingf(u
, flags
, name
, "Delegate=%s", yes_no(b
));
353 } else if (streq(name
, "DelegateControllers")) {
356 if (!UNIT_VTABLE(u
)->can_delegate
)
357 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
359 r
= sd_bus_message_enter_container(message
, 'a', "s");
367 r
= sd_bus_message_read(message
, "s", &t
);
373 cc
= cgroup_controller_from_string(t
);
375 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown cgroup controller '%s'", t
);
377 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
380 r
= sd_bus_message_exit_container(message
);
384 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
385 _cleanup_free_
char *t
= NULL
;
387 r
= cg_mask_to_string(mask
, &t
);
393 c
->delegate_controllers
= 0;
395 c
->delegate_controllers
|= mask
;
397 unit_write_settingf(u
, flags
, name
, "Delegate=%s", strempty(t
));
406 static int bus_cgroup_set_boolean(
411 sd_bus_message
*message
,
412 UnitWriteFlags flags
,
413 sd_bus_error
*error
) {
419 r
= sd_bus_message_read(message
, "b", &b
);
423 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
425 unit_invalidate_cgroup(u
, mask
);
426 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, yes_no(b
));
432 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
433 static int bus_cgroup_set_##function( \
437 sd_bus_message *message, \
438 UnitWriteFlags flags, \
439 sd_bus_error *error) { \
446 r = sd_bus_message_read(message, "t", &v); \
451 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
452 "Value specified in %s is out of range", name); \
454 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
456 unit_invalidate_cgroup(u, (mask)); \
459 unit_write_settingf(u, flags, name, \
462 unit_write_settingf(u, flags, name, \
463 "%s=%" PRIu64, name, v); \
469 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
470 static int bus_cgroup_set_##function( \
474 sd_bus_message *message, \
475 UnitWriteFlags flags, \
476 sd_bus_error *error) { \
483 r = sd_bus_message_read(message, "t", &v); \
488 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
489 "Value specified in %s is out of range", name); \
491 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
493 unit_invalidate_cgroup(u, (mask)); \
495 if (v == CGROUP_LIMIT_MAX) \
496 unit_write_settingf(u, flags, name, \
497 "%s=infinity", name); \
499 unit_write_settingf(u, flags, name, \
500 "%s=%" PRIu64, name, v); \
505 static int bus_cgroup_set_##function##_scale( \
509 sd_bus_message *message, \
510 UnitWriteFlags flags, \
511 sd_bus_error *error) { \
519 r = sd_bus_message_read(message, "u", &raw); \
523 v = scale(raw, UINT32_MAX); \
524 if (v < minimum || v >= UINT64_MAX) \
525 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
526 "Value specified in %s is out of range", name); \
528 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
532 unit_invalidate_cgroup(u, (mask)); \
534 /* Chop off suffix */ \
535 assert_se(e = endswith(name, "Scale")); \
536 name = strndupa(name, e - name); \
538 unit_write_settingf(u, flags, name, "%s=%" PRIu32 "%%", name, \
539 (uint32_t) (DIV_ROUND_UP((uint64_t) raw * 100U, (uint64_t) UINT32_MAX))); \
545 #pragma GCC diagnostic push
546 #pragma GCC diagnostic ignored "-Wtype-limits"
547 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_weight
, CGROUP_MASK_CPU
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
548 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares
, CGROUP_MASK_CPU
, CGROUP_CPU_SHARES_IS_OK
, CGROUP_CPU_SHARES_INVALID
);
549 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight
, CGROUP_MASK_IO
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
550 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight
, CGROUP_MASK_BLKIO
, CGROUP_BLKIO_WEIGHT_IS_OK
, CGROUP_BLKIO_WEIGHT_INVALID
);
551 BUS_DEFINE_SET_CGROUP_LIMIT(memory
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 1);
552 BUS_DEFINE_SET_CGROUP_LIMIT(swap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
553 BUS_DEFINE_SET_CGROUP_LIMIT(tasks_max
, CGROUP_MASK_PIDS
, system_tasks_max_scale
, 1);
554 #pragma GCC diagnostic pop
556 int bus_cgroup_set_property(
560 sd_bus_message
*message
,
561 UnitWriteFlags flags
,
562 sd_bus_error
*error
) {
564 CGroupIOLimitType iol_type
;
572 flags
|= UNIT_PRIVATE
;
574 if (streq(name
, "CPUAccounting"))
575 return bus_cgroup_set_boolean(u
, name
, &c
->cpu_accounting
, CGROUP_MASK_CPUACCT
|CGROUP_MASK_CPU
, message
, flags
, error
);
577 if (streq(name
, "CPUWeight"))
578 return bus_cgroup_set_cpu_weight(u
, name
, &c
->cpu_weight
, message
, flags
, error
);
580 if (streq(name
, "StartupCPUWeight"))
581 return bus_cgroup_set_cpu_weight(u
, name
, &c
->startup_cpu_weight
, message
, flags
, error
);
583 if (streq(name
, "CPUShares"))
584 return bus_cgroup_set_cpu_shares(u
, name
, &c
->cpu_shares
, message
, flags
, error
);
586 if (streq(name
, "StartupCPUShares"))
587 return bus_cgroup_set_cpu_shares(u
, name
, &c
->startup_cpu_shares
, message
, flags
, error
);
589 if (streq(name
, "IOAccounting"))
590 return bus_cgroup_set_boolean(u
, name
, &c
->io_accounting
, CGROUP_MASK_IO
, message
, flags
, error
);
592 if (streq(name
, "IOWeight"))
593 return bus_cgroup_set_io_weight(u
, name
, &c
->io_weight
, message
, flags
, error
);
595 if (streq(name
, "StartupIOWeight"))
596 return bus_cgroup_set_io_weight(u
, name
, &c
->startup_io_weight
, message
, flags
, error
);
598 if (streq(name
, "BlockIOAccounting"))
599 return bus_cgroup_set_boolean(u
, name
, &c
->blockio_accounting
, CGROUP_MASK_BLKIO
, message
, flags
, error
);
601 if (streq(name
, "BlockIOWeight"))
602 return bus_cgroup_set_blockio_weight(u
, name
, &c
->blockio_weight
, message
, flags
, error
);
604 if (streq(name
, "StartupBlockIOWeight"))
605 return bus_cgroup_set_blockio_weight(u
, name
, &c
->startup_blockio_weight
, message
, flags
, error
);
607 if (streq(name
, "MemoryAccounting"))
608 return bus_cgroup_set_boolean(u
, name
, &c
->memory_accounting
, CGROUP_MASK_MEMORY
, message
, flags
, error
);
610 if (streq(name
, "MemoryMin"))
611 return bus_cgroup_set_memory(u
, name
, &c
->memory_min
, message
, flags
, error
);
613 if (streq(name
, "MemoryLow"))
614 return bus_cgroup_set_memory(u
, name
, &c
->memory_low
, message
, flags
, error
);
616 if (streq(name
, "MemoryHigh"))
617 return bus_cgroup_set_memory(u
, name
, &c
->memory_high
, message
, flags
, error
);
619 if (streq(name
, "MemorySwapMax"))
620 return bus_cgroup_set_swap(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
622 if (streq(name
, "MemoryMax"))
623 return bus_cgroup_set_memory(u
, name
, &c
->memory_max
, message
, flags
, error
);
625 if (streq(name
, "MemoryLimit"))
626 return bus_cgroup_set_memory(u
, name
, &c
->memory_limit
, message
, flags
, error
);
628 if (streq(name
, "MemoryMinScale"))
629 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_min
, message
, flags
, error
);
631 if (streq(name
, "MemoryLowScale"))
632 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_low
, message
, flags
, error
);
634 if (streq(name
, "MemoryHighScale"))
635 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_high
, message
, flags
, error
);
637 if (streq(name
, "MemorySwapMaxScale"))
638 return bus_cgroup_set_swap_scale(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
640 if (streq(name
, "MemoryMaxScale"))
641 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_max
, message
, flags
, error
);
643 if (streq(name
, "MemoryLimitScale"))
644 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_limit
, message
, flags
, error
);
646 if (streq(name
, "TasksAccounting"))
647 return bus_cgroup_set_boolean(u
, name
, &c
->tasks_accounting
, CGROUP_MASK_PIDS
, message
, flags
, error
);
649 if (streq(name
, "TasksMax"))
650 return bus_cgroup_set_tasks_max(u
, name
, &c
->tasks_max
, message
, flags
, error
);
652 if (streq(name
, "TasksMaxScale"))
653 return bus_cgroup_set_tasks_max_scale(u
, name
, &c
->tasks_max
, message
, flags
, error
);
655 if (streq(name
, "CPUQuotaPerSecUSec")) {
658 r
= sd_bus_message_read(message
, "t", &u64
);
663 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "CPUQuotaPerSecUSec= value out of range");
665 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
666 c
->cpu_quota_per_sec_usec
= u64
;
667 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
669 if (c
->cpu_quota_per_sec_usec
== USEC_INFINITY
)
670 unit_write_setting(u
, flags
, "CPUQuota", "CPUQuota=");
672 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
674 unit_write_settingf(u
, flags
, "CPUQuota",
676 (double) (c
->cpu_quota_per_sec_usec
/ 10000));
681 } else if ((iol_type
= cgroup_io_limit_type_from_string(name
)) >= 0) {
686 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
690 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
692 if (!path_is_normalized(path
))
693 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
695 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
696 CGroupIODeviceLimit
*a
= NULL
, *b
;
698 LIST_FOREACH(device_limits
, b
, c
->io_device_limits
) {
699 if (path_equal(path
, b
->path
)) {
706 CGroupIOLimitType type
;
708 a
= new0(CGroupIODeviceLimit
, 1);
712 a
->path
= strdup(path
);
718 for (type
= 0; type
< _CGROUP_IO_LIMIT_TYPE_MAX
; type
++)
719 a
->limits
[type
] = cgroup_io_limit_defaults
[type
];
721 LIST_PREPEND(device_limits
, c
->io_device_limits
, a
);
724 a
->limits
[iol_type
] = u64
;
732 r
= sd_bus_message_exit_container(message
);
736 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
737 CGroupIODeviceLimit
*a
;
738 _cleanup_free_
char *buf
= NULL
;
739 _cleanup_fclose_
FILE *f
= NULL
;
743 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
744 a
->limits
[iol_type
] = cgroup_io_limit_defaults
[iol_type
];
747 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
749 f
= open_memstream(&buf
, &size
);
753 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
755 fprintf(f
, "%s=\n", name
);
756 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
757 if (a
->limits
[iol_type
] != cgroup_io_limit_defaults
[iol_type
])
758 fprintf(f
, "%s=%s %" PRIu64
"\n", name
, a
->path
, a
->limits
[iol_type
]);
760 r
= fflush_and_check(f
);
763 unit_write_setting(u
, flags
, name
, buf
);
768 } else if (streq(name
, "IODeviceWeight")) {
773 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
777 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
779 if (!path_is_normalized(path
))
780 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
782 if (!CGROUP_WEIGHT_IS_OK(weight
) || weight
== CGROUP_WEIGHT_INVALID
)
783 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IODeviceWeight= value out of range");
785 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
786 CGroupIODeviceWeight
*a
= NULL
, *b
;
788 LIST_FOREACH(device_weights
, b
, c
->io_device_weights
) {
789 if (path_equal(b
->path
, path
)) {
796 a
= new0(CGroupIODeviceWeight
, 1);
800 a
->path
= strdup(path
);
805 LIST_PREPEND(device_weights
, c
->io_device_weights
, a
);
814 r
= sd_bus_message_exit_container(message
);
818 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
819 _cleanup_free_
char *buf
= NULL
;
820 _cleanup_fclose_
FILE *f
= NULL
;
821 CGroupIODeviceWeight
*a
;
825 while (c
->io_device_weights
)
826 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
829 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
831 f
= open_memstream(&buf
, &size
);
835 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
837 fputs("IODeviceWeight=\n", f
);
838 LIST_FOREACH(device_weights
, a
, c
->io_device_weights
)
839 fprintf(f
, "IODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
841 r
= fflush_and_check(f
);
844 unit_write_setting(u
, flags
, name
, buf
);
849 } else if (STR_IN_SET(name
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
855 if (streq(name
, "BlockIOWriteBandwidth"))
858 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
862 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
864 if (!path_is_normalized(path
))
865 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
867 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
868 CGroupBlockIODeviceBandwidth
*a
= NULL
, *b
;
870 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
871 if (path_equal(path
, b
->path
)) {
878 a
= new0(CGroupBlockIODeviceBandwidth
, 1);
882 a
->rbps
= CGROUP_LIMIT_MAX
;
883 a
->wbps
= CGROUP_LIMIT_MAX
;
884 a
->path
= strdup(path
);
890 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, a
);
904 r
= sd_bus_message_exit_container(message
);
908 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
909 CGroupBlockIODeviceBandwidth
*a
;
910 _cleanup_free_
char *buf
= NULL
;
911 _cleanup_fclose_
FILE *f
= NULL
;
915 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
) {
917 a
->rbps
= CGROUP_LIMIT_MAX
;
919 a
->wbps
= CGROUP_LIMIT_MAX
;
923 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
925 f
= open_memstream(&buf
, &size
);
929 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
932 fputs("BlockIOReadBandwidth=\n", f
);
933 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
934 if (a
->rbps
!= CGROUP_LIMIT_MAX
)
935 fprintf(f
, "BlockIOReadBandwidth=%s %" PRIu64
"\n", a
->path
, a
->rbps
);
937 fputs("BlockIOWriteBandwidth=\n", f
);
938 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
939 if (a
->wbps
!= CGROUP_LIMIT_MAX
)
940 fprintf(f
, "BlockIOWriteBandwidth=%s %" PRIu64
"\n", a
->path
, a
->wbps
);
943 r
= fflush_and_check(f
);
947 unit_write_setting(u
, flags
, name
, buf
);
952 } else if (streq(name
, "BlockIODeviceWeight")) {
957 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
961 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
963 if (!path_is_normalized(path
))
964 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
966 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight
) || weight
== CGROUP_BLKIO_WEIGHT_INVALID
)
967 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "BlockIODeviceWeight= out of range");
969 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
970 CGroupBlockIODeviceWeight
*a
= NULL
, *b
;
972 LIST_FOREACH(device_weights
, b
, c
->blockio_device_weights
) {
973 if (path_equal(b
->path
, path
)) {
980 a
= new0(CGroupBlockIODeviceWeight
, 1);
984 a
->path
= strdup(path
);
989 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, a
);
998 r
= sd_bus_message_exit_container(message
);
1002 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1003 _cleanup_free_
char *buf
= NULL
;
1004 _cleanup_fclose_
FILE *f
= NULL
;
1005 CGroupBlockIODeviceWeight
*a
;
1009 while (c
->blockio_device_weights
)
1010 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
1013 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1015 f
= open_memstream(&buf
, &size
);
1019 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1021 fputs("BlockIODeviceWeight=\n", f
);
1022 LIST_FOREACH(device_weights
, a
, c
->blockio_device_weights
)
1023 fprintf(f
, "BlockIODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1025 r
= fflush_and_check(f
);
1029 unit_write_setting(u
, flags
, name
, buf
);
1034 } else if (streq(name
, "DevicePolicy")) {
1036 CGroupDevicePolicy p
;
1038 r
= sd_bus_message_read(message
, "s", &policy
);
1042 p
= cgroup_device_policy_from_string(policy
);
1046 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1047 c
->device_policy
= p
;
1048 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1049 unit_write_settingf(u
, flags
, name
, "DevicePolicy=%s", policy
);
1054 } else if (streq(name
, "DeviceAllow")) {
1055 const char *path
, *rwm
;
1058 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
1062 while ((r
= sd_bus_message_read(message
, "(ss)", &path
, &rwm
)) > 0) {
1064 if (!valid_device_allow_pattern(path
) || strpbrk(path
, WHITESPACE
))
1065 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires device node or pattern");
1069 else if (!in_charset(rwm
, "rwm"))
1070 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires combination of rwm flags");
1072 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1073 CGroupDeviceAllow
*a
= NULL
, *b
;
1075 LIST_FOREACH(device_allow
, b
, c
->device_allow
) {
1076 if (path_equal(b
->path
, path
)) {
1083 a
= new0(CGroupDeviceAllow
, 1);
1087 a
->path
= strdup(path
);
1093 LIST_PREPEND(device_allow
, c
->device_allow
, a
);
1096 a
->r
= !!strchr(rwm
, 'r');
1097 a
->w
= !!strchr(rwm
, 'w');
1098 a
->m
= !!strchr(rwm
, 'm');
1106 r
= sd_bus_message_exit_container(message
);
1110 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1111 _cleanup_free_
char *buf
= NULL
;
1112 _cleanup_fclose_
FILE *f
= NULL
;
1113 CGroupDeviceAllow
*a
;
1117 while (c
->device_allow
)
1118 cgroup_context_free_device_allow(c
, c
->device_allow
);
1121 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1123 f
= open_memstream(&buf
, &size
);
1127 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1129 fputs("DeviceAllow=\n", f
);
1130 LIST_FOREACH(device_allow
, a
, c
->device_allow
)
1131 fprintf(f
, "DeviceAllow=%s %s%s%s\n", a
->path
, a
->r
? "r" : "", a
->w
? "w" : "", a
->m
? "m" : "");
1133 r
= fflush_and_check(f
);
1136 unit_write_setting(u
, flags
, name
, buf
);
1141 } else if (streq(name
, "IPAccounting")) {
1144 r
= sd_bus_message_read(message
, "b", &b
);
1148 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1149 c
->ip_accounting
= b
;
1151 unit_invalidate_cgroup_bpf(u
);
1152 unit_write_settingf(u
, flags
, name
, "IPAccounting=%s", yes_no(b
));
1157 } else if (STR_IN_SET(name
, "IPAddressAllow", "IPAddressDeny")) {
1158 IPAddressAccessItem
**list
;
1161 list
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow
: &c
->ip_address_deny
;
1163 r
= sd_bus_message_enter_container(message
, 'a', "(iayu)");
1173 r
= sd_bus_message_enter_container(message
, 'r', "iayu");
1179 r
= sd_bus_message_read(message
, "i", &family
);
1183 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1184 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects IPv4 or IPv6 addresses only.", name
);
1186 r
= sd_bus_message_read_array(message
, 'y', &ap
, &an
);
1190 if (an
!= FAMILY_ADDRESS_SIZE(family
))
1191 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1192 af_to_name(family
), FAMILY_ADDRESS_SIZE(family
), an
);
1194 r
= sd_bus_message_read(message
, "u", &prefixlen
);
1198 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
)*8)
1199 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
));
1201 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1202 IPAddressAccessItem
*item
;
1204 item
= new0(IPAddressAccessItem
, 1);
1208 item
->family
= family
;
1209 item
->prefixlen
= prefixlen
;
1210 memcpy(&item
->address
, ap
, an
);
1212 LIST_PREPEND(items
, *list
, item
);
1215 r
= sd_bus_message_exit_container(message
);
1222 r
= sd_bus_message_exit_container(message
);
1226 *list
= ip_address_access_reduce(*list
);
1228 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1229 _cleanup_free_
char *buf
= NULL
;
1230 _cleanup_fclose_
FILE *f
= NULL
;
1231 IPAddressAccessItem
*item
;
1235 *list
= ip_address_access_free_all(*list
);
1237 unit_invalidate_cgroup_bpf(u
);
1238 f
= open_memstream(&buf
, &size
);
1242 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1247 LIST_FOREACH(items
, item
, *list
) {
1248 char buffer
[CONST_MAX(INET_ADDRSTRLEN
, INET6_ADDRSTRLEN
)];
1251 if (!inet_ntop(item
->family
, &item
->address
, buffer
, sizeof(buffer
)))
1252 return errno
> 0 ? -errno
: -EINVAL
;
1254 fprintf(f
, "%s=%s/%u\n", name
, buffer
, item
->prefixlen
);
1257 r
= fflush_and_check(f
);
1261 unit_write_setting(u
, flags
, name
, buf
);
1264 r
= bpf_firewall_supported();
1267 if (r
== BPF_FIREWALL_UNSUPPORTED
) {
1268 static bool warned
= false;
1270 log_full(warned
? LOG_DEBUG
: LOG_WARNING
,
1271 "Transient unit %s configures an IP firewall, but the local system does not support BPF/cgroup firewalling.\n"
1272 "Proceeding WITHOUT firewalling in effect! (This warning is only shown for the first started transient unit using IP firewalling.)", u
->id
);
1282 if (u
->transient
&& u
->load_state
== UNIT_STUB
)
1283 return bus_cgroup_set_transient_property(u
, c
, name
, message
, flags
, error
);