1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "bpf-firewall.h"
8 #include "bpf-foreign.h"
9 #include "bus-get-properties.h"
10 #include "cgroup-util.h"
12 #include "core-varlink.h"
13 #include "dbus-cgroup.h"
14 #include "dbus-util.h"
15 #include "errno-util.h"
18 #include "in-addr-prefix-util.h"
19 #include "ip-protocol-list.h"
20 #include "limits-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "percent-util.h"
24 #include "socket-util.h"
26 BUS_DEFINE_PROPERTY_GET(bus_property_get_tasks_max
, "t", TasksMax
, tasks_max_resolve
);
28 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_cgroup_device_policy
, cgroup_device_policy
, CGroupDevicePolicy
);
29 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_mode
, managed_oom_mode
, ManagedOOMMode
);
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_preference
, managed_oom_preference
, ManagedOOMPreference
);
32 static int property_get_cgroup_mask(
35 const char *interface
,
37 sd_bus_message
*reply
,
39 sd_bus_error
*error
) {
41 CGroupMask
*mask
= userdata
;
47 r
= sd_bus_message_open_container(reply
, 'a', "s");
51 for (CGroupController ctrl
= 0; ctrl
< _CGROUP_CONTROLLER_MAX
; ctrl
++) {
52 if ((*mask
& CGROUP_CONTROLLER_TO_MASK(ctrl
)) == 0)
55 r
= sd_bus_message_append(reply
, "s", cgroup_controller_to_string(ctrl
));
60 return sd_bus_message_close_container(reply
);
63 static int property_get_delegate_controllers(
66 const char *interface
,
68 sd_bus_message
*reply
,
70 sd_bus_error
*error
) {
72 CGroupContext
*c
= ASSERT_PTR(userdata
);
78 return sd_bus_message_append(reply
, "as", 0);
80 return property_get_cgroup_mask(bus
, path
, interface
, property
, reply
, &c
->delegate_controllers
, error
);
83 static int property_get_cpuset(
86 const char *interface
,
88 sd_bus_message
*reply
,
90 sd_bus_error
*error
) {
92 CPUSet
*cpus
= ASSERT_PTR(userdata
);
93 _cleanup_free_
uint8_t *array
= NULL
;
99 (void) cpu_set_to_dbus(cpus
, &array
, &allocated
);
100 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
103 static int property_get_io_device_weight(
106 const char *interface
,
107 const char *property
,
108 sd_bus_message
*reply
,
110 sd_bus_error
*error
) {
112 CGroupContext
*c
= ASSERT_PTR(userdata
);
118 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
122 LIST_FOREACH(device_weights
, w
, c
->io_device_weights
) {
123 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
128 return sd_bus_message_close_container(reply
);
131 static int property_get_io_device_limits(
134 const char *interface
,
135 const char *property
,
136 sd_bus_message
*reply
,
138 sd_bus_error
*error
) {
140 CGroupContext
*c
= ASSERT_PTR(userdata
);
146 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
150 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
) {
151 CGroupIOLimitType type
;
153 type
= cgroup_io_limit_type_from_string(property
);
154 if (type
< 0 || l
->limits
[type
] == cgroup_io_limit_defaults
[type
])
157 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->limits
[type
]);
162 return sd_bus_message_close_container(reply
);
165 static int property_get_io_device_latency(
168 const char *interface
,
169 const char *property
,
170 sd_bus_message
*reply
,
172 sd_bus_error
*error
) {
174 CGroupContext
*c
= ASSERT_PTR(userdata
);
180 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
184 LIST_FOREACH(device_latencies
, l
, c
->io_device_latencies
) {
185 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->target_usec
);
190 return sd_bus_message_close_container(reply
);
193 static int property_get_blockio_device_weight(
196 const char *interface
,
197 const char *property
,
198 sd_bus_message
*reply
,
200 sd_bus_error
*error
) {
202 CGroupContext
*c
= ASSERT_PTR(userdata
);
208 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
212 LIST_FOREACH(device_weights
, w
, c
->blockio_device_weights
) {
213 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
218 return sd_bus_message_close_container(reply
);
221 static int property_get_blockio_device_bandwidths(
224 const char *interface
,
225 const char *property
,
226 sd_bus_message
*reply
,
228 sd_bus_error
*error
) {
230 CGroupContext
*c
= ASSERT_PTR(userdata
);
236 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
240 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
243 if (streq(property
, "BlockIOReadBandwidth"))
248 if (v
== CGROUP_LIMIT_MAX
)
251 r
= sd_bus_message_append(reply
, "(st)", b
->path
, v
);
256 return sd_bus_message_close_container(reply
);
259 static int property_get_device_allow(
262 const char *interface
,
263 const char *property
,
264 sd_bus_message
*reply
,
266 sd_bus_error
*error
) {
268 CGroupContext
*c
= ASSERT_PTR(userdata
);
274 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
278 LIST_FOREACH(device_allow
, a
, c
->device_allow
) {
291 r
= sd_bus_message_append(reply
, "(ss)", a
->path
, rwm
);
296 return sd_bus_message_close_container(reply
);
299 static int property_get_ip_address_access(
302 const char *interface
,
303 const char *property
,
304 sd_bus_message
*reply
,
306 sd_bus_error
*error
) {
308 Set
**prefixes
= ASSERT_PTR(userdata
);
309 struct in_addr_prefix
*i
;
312 r
= sd_bus_message_open_container(reply
, 'a', "(iayu)");
316 SET_FOREACH(i
, *prefixes
) {
318 r
= sd_bus_message_open_container(reply
, 'r', "iayu");
322 r
= sd_bus_message_append(reply
, "i", i
->family
);
326 r
= sd_bus_message_append_array(reply
, 'y', &i
->address
, FAMILY_ADDRESS_SIZE(i
->family
));
330 r
= sd_bus_message_append(reply
, "u", (uint32_t) i
->prefixlen
);
334 r
= sd_bus_message_close_container(reply
);
339 return sd_bus_message_close_container(reply
);
342 static int property_get_bpf_foreign_program(
345 const char *interface
,
346 const char *property
,
347 sd_bus_message
*reply
,
349 sd_bus_error
*error
) {
350 CGroupContext
*c
= userdata
;
353 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
357 LIST_FOREACH(programs
, p
, c
->bpf_foreign_programs
) {
358 const char *attach_type
= bpf_cgroup_attach_type_to_string(p
->attach_type
);
360 r
= sd_bus_message_append(reply
, "(ss)", attach_type
, p
->bpffs_path
);
365 return sd_bus_message_close_container(reply
);
368 static int property_get_socket_bind(
371 const char *interface
,
372 const char *property
,
373 sd_bus_message
*reply
,
375 sd_bus_error
*error
) {
377 CGroupSocketBindItem
**items
= ASSERT_PTR(userdata
);
380 r
= sd_bus_message_open_container(reply
, 'a', "(iiqq)");
384 LIST_FOREACH(socket_bind_items
, i
, *items
) {
385 r
= sd_bus_message_append(reply
, "(iiqq)", i
->address_family
, i
->ip_protocol
, i
->nr_ports
, i
->port_min
);
390 return sd_bus_message_close_container(reply
);
393 static int property_get_restrict_network_interfaces(
396 const char *interface
,
397 const char *property
,
398 sd_bus_message
*reply
,
400 sd_bus_error
*error
) {
402 CGroupContext
*c
= ASSERT_PTR(userdata
);
408 r
= sd_bus_message_open_container(reply
, 'r', "bas");
412 r
= sd_bus_message_append(reply
, "b", c
->restrict_network_interfaces_is_allow_list
);
416 r
= sd_bus_message_open_container(reply
, 'a', "s");
420 SET_FOREACH(iface
, c
->restrict_network_interfaces
) {
421 r
= sd_bus_message_append(reply
, "s", iface
);
426 r
= sd_bus_message_close_container(reply
);
430 return sd_bus_message_close_container(reply
);
433 const sd_bus_vtable bus_cgroup_vtable
[] = {
434 SD_BUS_VTABLE_START(0),
435 SD_BUS_PROPERTY("Delegate", "b", bus_property_get_bool
, offsetof(CGroupContext
, delegate
), 0),
436 SD_BUS_PROPERTY("DelegateControllers", "as", property_get_delegate_controllers
, 0, 0),
437 SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, cpu_accounting
), 0),
438 SD_BUS_PROPERTY("CPUWeight", "t", NULL
, offsetof(CGroupContext
, cpu_weight
), 0),
439 SD_BUS_PROPERTY("StartupCPUWeight", "t", NULL
, offsetof(CGroupContext
, startup_cpu_weight
), 0),
440 SD_BUS_PROPERTY("CPUShares", "t", NULL
, offsetof(CGroupContext
, cpu_shares
), 0),
441 SD_BUS_PROPERTY("StartupCPUShares", "t", NULL
, offsetof(CGroupContext
, startup_cpu_shares
), 0),
442 SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_per_sec_usec
), 0),
443 SD_BUS_PROPERTY("CPUQuotaPeriodUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_period_usec
), 0),
444 SD_BUS_PROPERTY("AllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_cpus
), 0),
445 SD_BUS_PROPERTY("StartupAllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_cpus
), 0),
446 SD_BUS_PROPERTY("AllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_mems
), 0),
447 SD_BUS_PROPERTY("StartupAllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_mems
), 0),
448 SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, io_accounting
), 0),
449 SD_BUS_PROPERTY("IOWeight", "t", NULL
, offsetof(CGroupContext
, io_weight
), 0),
450 SD_BUS_PROPERTY("StartupIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_io_weight
), 0),
451 SD_BUS_PROPERTY("IODeviceWeight", "a(st)", property_get_io_device_weight
, 0, 0),
452 SD_BUS_PROPERTY("IOReadBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
453 SD_BUS_PROPERTY("IOWriteBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
454 SD_BUS_PROPERTY("IOReadIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
455 SD_BUS_PROPERTY("IOWriteIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
456 SD_BUS_PROPERTY("IODeviceLatencyTargetUSec", "a(st)", property_get_io_device_latency
, 0, 0),
457 SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, blockio_accounting
), 0),
458 SD_BUS_PROPERTY("BlockIOWeight", "t", NULL
, offsetof(CGroupContext
, blockio_weight
), 0),
459 SD_BUS_PROPERTY("StartupBlockIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_blockio_weight
), 0),
460 SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight
, 0, 0),
461 SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
462 SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
463 SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, memory_accounting
), 0),
464 SD_BUS_PROPERTY("DefaultMemoryLow", "t", NULL
, offsetof(CGroupContext
, default_memory_low
), 0),
465 SD_BUS_PROPERTY("DefaultMemoryMin", "t", NULL
, offsetof(CGroupContext
, default_memory_min
), 0),
466 SD_BUS_PROPERTY("MemoryMin", "t", NULL
, offsetof(CGroupContext
, memory_min
), 0),
467 SD_BUS_PROPERTY("MemoryLow", "t", NULL
, offsetof(CGroupContext
, memory_low
), 0),
468 SD_BUS_PROPERTY("MemoryHigh", "t", NULL
, offsetof(CGroupContext
, memory_high
), 0),
469 SD_BUS_PROPERTY("MemoryMax", "t", NULL
, offsetof(CGroupContext
, memory_max
), 0),
470 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL
, offsetof(CGroupContext
, memory_swap_max
), 0),
471 SD_BUS_PROPERTY("MemoryZSwapMax", "t", NULL
, offsetof(CGroupContext
, memory_zswap_max
), 0),
472 SD_BUS_PROPERTY("MemoryLimit", "t", NULL
, offsetof(CGroupContext
, memory_limit
), 0),
473 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy
, offsetof(CGroupContext
, device_policy
), 0),
474 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow
, 0, 0),
475 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, tasks_accounting
), 0),
476 SD_BUS_PROPERTY("TasksMax", "t", bus_property_get_tasks_max
, offsetof(CGroupContext
, tasks_max
), 0),
477 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, ip_accounting
), 0),
478 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_allow
), 0),
479 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_deny
), 0),
480 SD_BUS_PROPERTY("IPIngressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_ingress
), 0),
481 SD_BUS_PROPERTY("IPEgressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_egress
), 0),
482 SD_BUS_PROPERTY("DisableControllers", "as", property_get_cgroup_mask
, offsetof(CGroupContext
, disable_controllers
), 0),
483 SD_BUS_PROPERTY("ManagedOOMSwap", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_swap
), 0),
484 SD_BUS_PROPERTY("ManagedOOMMemoryPressure", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_mem_pressure
), 0),
485 SD_BUS_PROPERTY("ManagedOOMMemoryPressureLimit", "u", NULL
, offsetof(CGroupContext
, moom_mem_pressure_limit
), 0),
486 SD_BUS_PROPERTY("ManagedOOMPreference", "s", property_get_managed_oom_preference
, offsetof(CGroupContext
, moom_preference
), 0),
487 SD_BUS_PROPERTY("BPFProgram", "a(ss)", property_get_bpf_foreign_program
, 0, 0),
488 SD_BUS_PROPERTY("SocketBindAllow", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_allow
), 0),
489 SD_BUS_PROPERTY("SocketBindDeny", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_deny
), 0),
490 SD_BUS_PROPERTY("RestrictNetworkInterfaces", "(bas)", property_get_restrict_network_interfaces
, 0, 0),
494 static int bus_cgroup_set_transient_property(
498 sd_bus_message
*message
,
499 UnitWriteFlags flags
,
500 sd_bus_error
*error
) {
509 flags
|= UNIT_PRIVATE
;
511 if (streq(name
, "Delegate")) {
514 if (!UNIT_VTABLE(u
)->can_delegate
)
515 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
517 r
= sd_bus_message_read(message
, "b", &b
);
521 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
523 c
->delegate_controllers
= b
? _CGROUP_MASK_ALL
: 0;
525 unit_write_settingf(u
, flags
, name
, "Delegate=%s", yes_no(b
));
530 } else if (STR_IN_SET(name
, "DelegateControllers", "DisableControllers")) {
533 if (streq(name
, "DelegateControllers") && !UNIT_VTABLE(u
)->can_delegate
)
534 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
536 r
= sd_bus_message_enter_container(message
, 'a', "s");
544 r
= sd_bus_message_read(message
, "s", &t
);
550 cc
= cgroup_controller_from_string(t
);
552 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown cgroup controller '%s'", t
);
554 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
557 r
= sd_bus_message_exit_container(message
);
561 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
562 _cleanup_free_
char *t
= NULL
;
564 r
= cg_mask_to_string(mask
, &t
);
568 if (streq(name
, "DelegateControllers")) {
572 c
->delegate_controllers
= 0;
574 c
->delegate_controllers
|= mask
;
576 unit_write_settingf(u
, flags
, name
, "Delegate=%s", strempty(t
));
578 } else if (streq(name
, "DisableControllers")) {
581 c
->disable_controllers
= 0;
583 c
->disable_controllers
|= mask
;
585 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, strempty(t
));
590 } else if (STR_IN_SET(name
, "IPIngressFilterPath", "IPEgressFilterPath")) {
594 filters
= streq(name
, "IPIngressFilterPath") ? &c
->ip_filters_ingress
: &c
->ip_filters_egress
;
595 r
= sd_bus_message_enter_container(message
, 'a', "s");
602 r
= sd_bus_message_read(message
, "s", &path
);
608 if (!path_is_normalized(path
) || !path_is_absolute(path
))
609 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects a normalized absolute path.", name
);
611 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && !strv_contains(*filters
, path
)) {
612 r
= strv_extend(filters
, path
);
618 r
= sd_bus_message_exit_container(message
);
622 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
623 _cleanup_free_
char *buf
= NULL
;
624 _cleanup_fclose_
FILE *f
= NULL
;
628 *filters
= strv_free(*filters
);
630 unit_invalidate_cgroup_bpf(u
);
631 f
= open_memstream_unlocked(&buf
, &size
);
638 STRV_FOREACH(entry
, *filters
)
639 fprintf(f
, "%s=%s\n", name
, *entry
);
641 r
= fflush_and_check(f
);
645 unit_write_setting(u
, flags
, name
, buf
);
648 r
= bpf_firewall_supported();
651 if (r
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
) {
652 static bool warned
= false;
654 log_full(warned
? LOG_DEBUG
: LOG_WARNING
,
655 "Transient unit %s configures an IP firewall with BPF, but the local system does not support BPF/cgroup firewalling with multiple filters.\n"
656 "Starting this unit will fail! (This warning is only shown for the first started transient unit using IP firewalling.)", u
->id
);
663 } else if (streq(name
, "BPFProgram")) {
667 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
671 while ((r
= sd_bus_message_read(message
, "(ss)", &a
, &p
)) > 0) {
672 int attach_type
= bpf_cgroup_attach_type_from_string(a
);
674 return sd_bus_error_setf(
676 SD_BUS_ERROR_INVALID_ARGS
,
677 "%s expects a valid BPF attach type, got '%s'.",
680 if (!path_is_normalized(p
) || !path_is_absolute(p
))
681 return sd_bus_error_setf(
683 SD_BUS_ERROR_INVALID_ARGS
,
684 "%s= expects a normalized absolute path.",
687 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
688 r
= cgroup_add_bpf_foreign_program(c
, attach_type
, p
);
697 r
= sd_bus_message_exit_container(message
);
701 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
702 _cleanup_free_
char *buf
= NULL
;
703 _cleanup_fclose_
FILE *f
= NULL
;
707 while (c
->bpf_foreign_programs
)
708 cgroup_context_remove_bpf_foreign_program(c
, c
->bpf_foreign_programs
);
710 f
= open_memstream_unlocked(&buf
, &size
);
717 LIST_FOREACH(programs
, fp
, c
->bpf_foreign_programs
)
718 fprintf(f
, "%s=%s:%s\n", name
,
719 bpf_cgroup_attach_type_to_string(fp
->attach_type
),
722 r
= fflush_and_check(f
);
726 unit_write_setting(u
, flags
, name
, buf
);
728 if (c
->bpf_foreign_programs
) {
729 r
= bpf_foreign_supported();
734 "Transient unit %s configures a BPF program pinned to BPF "
735 "filesystem, but the local system does not support that.\n"
736 "Starting this unit will fail!", u
->id
);
746 static int bus_cgroup_set_boolean(
751 sd_bus_message
*message
,
752 UnitWriteFlags flags
,
753 sd_bus_error
*error
) {
759 r
= sd_bus_message_read(message
, "b", &b
);
763 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
765 unit_invalidate_cgroup(u
, mask
);
766 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, yes_no(b
));
772 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
773 static int bus_cgroup_set_##function( \
777 sd_bus_message *message, \
778 UnitWriteFlags flags, \
779 sd_bus_error *error) { \
786 r = sd_bus_message_read(message, "t", &v); \
791 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
792 "Value specified in %s is out of range", name); \
794 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
796 unit_invalidate_cgroup(u, mask); \
799 unit_write_settingf(u, flags, name, \
802 unit_write_settingf(u, flags, name, \
803 "%s=%" PRIu64, name, v); \
809 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
810 static int bus_cgroup_set_##function( \
814 sd_bus_message *message, \
815 UnitWriteFlags flags, \
816 sd_bus_error *error) { \
823 r = sd_bus_message_read(message, "t", &v); \
828 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
829 "Value specified in %s is out of range", name); \
831 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
833 unit_invalidate_cgroup(u, mask); \
835 if (v == CGROUP_LIMIT_MAX) \
836 unit_write_settingf(u, flags, name, \
837 "%s=infinity", name); \
839 unit_write_settingf(u, flags, name, \
840 "%s=%" PRIu64, name, v); \
845 static int bus_cgroup_set_##function##_scale( \
849 sd_bus_message *message, \
850 UnitWriteFlags flags, \
851 sd_bus_error *error) { \
859 r = sd_bus_message_read(message, "u", &raw); \
863 v = scale(raw, UINT32_MAX); \
864 if (v < minimum || v >= UINT64_MAX) \
865 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
866 "Value specified in %s is out of range", name); \
868 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
870 unit_invalidate_cgroup(u, mask); \
872 /* Prepare to chop off suffix */ \
873 assert_se(endswith(name, "Scale")); \
875 int scaled = UINT32_SCALE_TO_PERMYRIAD(raw); \
876 unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR, \
877 (int)(strlen(name) - strlen("Scale")), name, \
878 PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled)); \
884 DISABLE_WARNING_TYPE_LIMITS
;
885 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares
, CGROUP_MASK_CPU
, CGROUP_CPU_SHARES_IS_OK
, CGROUP_CPU_SHARES_INVALID
);
886 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight
, CGROUP_MASK_IO
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
887 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight
, CGROUP_MASK_BLKIO
, CGROUP_BLKIO_WEIGHT_IS_OK
, CGROUP_BLKIO_WEIGHT_INVALID
);
888 BUS_DEFINE_SET_CGROUP_LIMIT(memory
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 1);
889 BUS_DEFINE_SET_CGROUP_LIMIT(memory_protection
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
890 BUS_DEFINE_SET_CGROUP_LIMIT(swap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
891 BUS_DEFINE_SET_CGROUP_LIMIT(zswap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
894 static int bus_cgroup_set_cpu_weight(
898 sd_bus_message
*message
,
899 UnitWriteFlags flags
,
900 sd_bus_error
*error
) {
904 r
= sd_bus_message_read(message
, "t", &v
);
907 if (!CGROUP_WEIGHT_IS_OK(v
) && v
!= CGROUP_WEIGHT_IDLE
)
908 return sd_bus_error_setf(
909 error
, SD_BUS_ERROR_INVALID_ARGS
, "Value specified in %s is out of range", name
);
910 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
912 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
913 if (v
== CGROUP_WEIGHT_INVALID
)
914 unit_write_settingf(u
, flags
, name
, "%s=", name
);
915 else if (v
== CGROUP_WEIGHT_IDLE
)
916 unit_write_settingf(u
, flags
, name
, "%s=idle", name
);
918 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu64
, name
, v
);
923 static int bus_cgroup_set_tasks_max(
927 sd_bus_message
*message
,
928 UnitWriteFlags flags
,
929 sd_bus_error
*error
) {
936 r
= sd_bus_message_read(message
, "t", &v
);
941 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
942 "Value specified in %s is out of range", name
);
944 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
945 *p
= (TasksMax
) { .value
= v
, .scale
= 0 }; /* When .scale==0, .value is the absolute value */
946 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
948 if (v
== CGROUP_LIMIT_MAX
)
949 unit_write_settingf(u
, flags
, name
,
950 "%s=infinity", name
);
952 unit_write_settingf(u
, flags
, name
,
953 "%s=%" PRIu64
, name
, v
);
959 static int bus_cgroup_set_tasks_max_scale(
963 sd_bus_message
*message
,
964 UnitWriteFlags flags
,
965 sd_bus_error
*error
) {
972 r
= sd_bus_message_read(message
, "u", &v
);
976 if (v
< 1 || v
>= UINT32_MAX
)
977 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
978 "Value specified in %s is out of range", name
);
980 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
981 *p
= (TasksMax
) { v
, UINT32_MAX
}; /* .scale is not 0, so this is interpreted as v/UINT32_MAX. */
982 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
984 uint32_t scaled
= DIV_ROUND_UP((uint64_t) v
* 100U, (uint64_t) UINT32_MAX
);
985 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu32
".%" PRIu32
"%%", "TasksMax",
986 scaled
/ 10, scaled
% 10);
992 int bus_cgroup_set_property(
996 sd_bus_message
*message
,
997 UnitWriteFlags flags
,
998 sd_bus_error
*error
) {
1000 CGroupIOLimitType iol_type
;
1008 flags
|= UNIT_PRIVATE
;
1010 if (streq(name
, "CPUAccounting"))
1011 return bus_cgroup_set_boolean(u
, name
, &c
->cpu_accounting
, get_cpu_accounting_mask(), message
, flags
, error
);
1013 if (streq(name
, "CPUWeight"))
1014 return bus_cgroup_set_cpu_weight(u
, name
, &c
->cpu_weight
, message
, flags
, error
);
1016 if (streq(name
, "StartupCPUWeight"))
1017 return bus_cgroup_set_cpu_weight(u
, name
, &c
->startup_cpu_weight
, message
, flags
, error
);
1019 if (streq(name
, "CPUShares"))
1020 return bus_cgroup_set_cpu_shares(u
, name
, &c
->cpu_shares
, message
, flags
, error
);
1022 if (streq(name
, "StartupCPUShares"))
1023 return bus_cgroup_set_cpu_shares(u
, name
, &c
->startup_cpu_shares
, message
, flags
, error
);
1025 if (streq(name
, "IOAccounting"))
1026 return bus_cgroup_set_boolean(u
, name
, &c
->io_accounting
, CGROUP_MASK_IO
, message
, flags
, error
);
1028 if (streq(name
, "IOWeight"))
1029 return bus_cgroup_set_io_weight(u
, name
, &c
->io_weight
, message
, flags
, error
);
1031 if (streq(name
, "StartupIOWeight"))
1032 return bus_cgroup_set_io_weight(u
, name
, &c
->startup_io_weight
, message
, flags
, error
);
1034 if (streq(name
, "BlockIOAccounting"))
1035 return bus_cgroup_set_boolean(u
, name
, &c
->blockio_accounting
, CGROUP_MASK_BLKIO
, message
, flags
, error
);
1037 if (streq(name
, "BlockIOWeight"))
1038 return bus_cgroup_set_blockio_weight(u
, name
, &c
->blockio_weight
, message
, flags
, error
);
1040 if (streq(name
, "StartupBlockIOWeight"))
1041 return bus_cgroup_set_blockio_weight(u
, name
, &c
->startup_blockio_weight
, message
, flags
, error
);
1043 if (streq(name
, "MemoryAccounting"))
1044 return bus_cgroup_set_boolean(u
, name
, &c
->memory_accounting
, CGROUP_MASK_MEMORY
, message
, flags
, error
);
1046 if (streq(name
, "MemoryMin")) {
1047 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_min
, message
, flags
, error
);
1049 c
->memory_min_set
= true;
1053 if (streq(name
, "MemoryLow")) {
1054 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_low
, message
, flags
, error
);
1056 c
->memory_low_set
= true;
1060 if (streq(name
, "DefaultMemoryMin")) {
1061 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1063 c
->default_memory_min_set
= true;
1067 if (streq(name
, "DefaultMemoryLow")) {
1068 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1070 c
->default_memory_low_set
= true;
1074 if (streq(name
, "MemoryHigh"))
1075 return bus_cgroup_set_memory(u
, name
, &c
->memory_high
, message
, flags
, error
);
1077 if (streq(name
, "MemorySwapMax"))
1078 return bus_cgroup_set_swap(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1080 if (streq(name
, "MemoryZSwapMax"))
1081 return bus_cgroup_set_zswap(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1083 if (streq(name
, "MemoryMax"))
1084 return bus_cgroup_set_memory(u
, name
, &c
->memory_max
, message
, flags
, error
);
1086 if (streq(name
, "MemoryLimit"))
1087 return bus_cgroup_set_memory(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1089 if (streq(name
, "MemoryMinScale")) {
1090 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_min
, message
, flags
, error
);
1092 c
->memory_min_set
= true;
1096 if (streq(name
, "MemoryLowScale")) {
1097 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_low
, message
, flags
, error
);
1099 c
->memory_low_set
= true;
1103 if (streq(name
, "DefaultMemoryMinScale")) {
1104 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1106 c
->default_memory_min_set
= true;
1110 if (streq(name
, "DefaultMemoryLowScale")) {
1111 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1113 c
->default_memory_low_set
= true;
1117 if (streq(name
, "MemoryHighScale"))
1118 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_high
, message
, flags
, error
);
1120 if (streq(name
, "MemorySwapMaxScale"))
1121 return bus_cgroup_set_swap_scale(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1123 if (streq(name
, "MemoryZSwapMaxScale"))
1124 return bus_cgroup_set_zswap_scale(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1126 if (streq(name
, "MemoryMaxScale"))
1127 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_max
, message
, flags
, error
);
1129 if (streq(name
, "MemoryLimitScale"))
1130 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1132 if (streq(name
, "TasksAccounting"))
1133 return bus_cgroup_set_boolean(u
, name
, &c
->tasks_accounting
, CGROUP_MASK_PIDS
, message
, flags
, error
);
1135 if (streq(name
, "TasksMax"))
1136 return bus_cgroup_set_tasks_max(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1138 if (streq(name
, "TasksMaxScale"))
1139 return bus_cgroup_set_tasks_max_scale(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1141 if (streq(name
, "CPUQuotaPerSecUSec")) {
1144 r
= sd_bus_message_read(message
, "t", &u64
);
1149 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "CPUQuotaPerSecUSec= value out of range");
1151 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1152 c
->cpu_quota_per_sec_usec
= u64
;
1153 u
->warned_clamping_cpu_quota_period
= false;
1154 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1156 if (c
->cpu_quota_per_sec_usec
== USEC_INFINITY
)
1157 unit_write_setting(u
, flags
, "CPUQuota", "CPUQuota=");
1159 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
1161 unit_write_settingf(u
, flags
, "CPUQuota",
1163 (double) (c
->cpu_quota_per_sec_usec
/ 10000));
1168 } else if (streq(name
, "CPUQuotaPeriodUSec")) {
1171 r
= sd_bus_message_read(message
, "t", &u64
);
1175 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1176 c
->cpu_quota_period_usec
= u64
;
1177 u
->warned_clamping_cpu_quota_period
= false;
1178 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1179 if (c
->cpu_quota_period_usec
== USEC_INFINITY
)
1180 unit_write_setting(u
, flags
, "CPUQuotaPeriodSec", "CPUQuotaPeriodSec=");
1182 unit_write_settingf(u
, flags
, "CPUQuotaPeriodSec",
1183 "CPUQuotaPeriodSec=%s",
1184 FORMAT_TIMESPAN(c
->cpu_quota_period_usec
, 1));
1189 } else if (STR_IN_SET(name
, "AllowedCPUs", "StartupAllowedCPUs", "AllowedMemoryNodes", "StartupAllowedMemoryNodes")) {
1192 _cleanup_(cpu_set_reset
) CPUSet new_set
= {};
1194 r
= sd_bus_message_read_array(message
, 'y', &a
, &n
);
1198 r
= cpu_set_from_dbus(a
, n
, &new_set
);
1202 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1203 _cleanup_free_
char *setstr
= NULL
;
1206 setstr
= cpu_set_to_range_string(&new_set
);
1210 if (streq(name
, "AllowedCPUs"))
1211 set
= &c
->cpuset_cpus
;
1212 else if (streq(name
, "StartupAllowedCPUs"))
1213 set
= &c
->startup_cpuset_cpus
;
1214 else if (streq(name
, "AllowedMemoryNodes"))
1215 set
= &c
->cpuset_mems
;
1216 else if (streq(name
, "StartupAllowedMemoryNodes"))
1217 set
= &c
->startup_cpuset_mems
;
1223 new_set
= (CPUSet
) {};
1225 unit_invalidate_cgroup(u
, CGROUP_MASK_CPUSET
);
1226 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, setstr
);
1231 } else if ((iol_type
= cgroup_io_limit_type_from_string(name
)) >= 0) {
1236 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1240 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1242 if (!path_is_normalized(path
))
1243 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1245 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1246 CGroupIODeviceLimit
*a
= NULL
;
1248 LIST_FOREACH(device_limits
, b
, c
->io_device_limits
)
1249 if (path_equal(path
, b
->path
)) {
1255 CGroupIOLimitType type
;
1257 a
= new0(CGroupIODeviceLimit
, 1);
1261 a
->path
= strdup(path
);
1267 for (type
= 0; type
< _CGROUP_IO_LIMIT_TYPE_MAX
; type
++)
1268 a
->limits
[type
] = cgroup_io_limit_defaults
[type
];
1270 LIST_PREPEND(device_limits
, c
->io_device_limits
, a
);
1273 a
->limits
[iol_type
] = u64
;
1281 r
= sd_bus_message_exit_container(message
);
1285 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1286 _cleanup_free_
char *buf
= NULL
;
1287 _cleanup_fclose_
FILE *f
= NULL
;
1291 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1292 a
->limits
[iol_type
] = cgroup_io_limit_defaults
[iol_type
];
1294 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1296 f
= open_memstream_unlocked(&buf
, &size
);
1300 fprintf(f
, "%s=\n", name
);
1301 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1302 if (a
->limits
[iol_type
] != cgroup_io_limit_defaults
[iol_type
])
1303 fprintf(f
, "%s=%s %" PRIu64
"\n", name
, a
->path
, a
->limits
[iol_type
]);
1305 r
= fflush_and_check(f
);
1308 unit_write_setting(u
, flags
, name
, buf
);
1313 } else if (streq(name
, "IODeviceWeight")) {
1318 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1322 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1324 if (!path_is_normalized(path
))
1325 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1327 if (!CGROUP_WEIGHT_IS_OK(weight
) || weight
== CGROUP_WEIGHT_INVALID
)
1328 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "IODeviceWeight= value out of range");
1330 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1331 CGroupIODeviceWeight
*a
= NULL
;
1333 LIST_FOREACH(device_weights
, b
, c
->io_device_weights
)
1334 if (path_equal(b
->path
, path
)) {
1340 a
= new0(CGroupIODeviceWeight
, 1);
1344 a
->path
= strdup(path
);
1349 LIST_PREPEND(device_weights
, c
->io_device_weights
, a
);
1358 r
= sd_bus_message_exit_container(message
);
1362 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1363 _cleanup_free_
char *buf
= NULL
;
1364 _cleanup_fclose_
FILE *f
= NULL
;
1368 while (c
->io_device_weights
)
1369 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
1371 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1373 f
= open_memstream_unlocked(&buf
, &size
);
1377 fputs("IODeviceWeight=\n", f
);
1378 LIST_FOREACH(device_weights
, a
, c
->io_device_weights
)
1379 fprintf(f
, "IODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1381 r
= fflush_and_check(f
);
1384 unit_write_setting(u
, flags
, name
, buf
);
1389 } else if (streq(name
, "IODeviceLatencyTargetUSec")) {
1394 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1398 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &target
)) > 0) {
1400 if (!path_is_normalized(path
))
1401 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1403 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1404 CGroupIODeviceLatency
*a
= NULL
;
1406 LIST_FOREACH(device_latencies
, b
, c
->io_device_latencies
)
1407 if (path_equal(b
->path
, path
)) {
1413 a
= new0(CGroupIODeviceLatency
, 1);
1417 a
->path
= strdup(path
);
1422 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, a
);
1425 a
->target_usec
= target
;
1431 r
= sd_bus_message_exit_container(message
);
1435 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1436 _cleanup_free_
char *buf
= NULL
;
1437 _cleanup_fclose_
FILE *f
= NULL
;
1441 while (c
->io_device_latencies
)
1442 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
1444 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1446 f
= open_memstream_unlocked(&buf
, &size
);
1450 fputs("IODeviceLatencyTargetSec=\n", f
);
1451 LIST_FOREACH(device_latencies
, a
, c
->io_device_latencies
)
1452 fprintf(f
, "IODeviceLatencyTargetSec=%s %s\n",
1453 a
->path
, FORMAT_TIMESPAN(a
->target_usec
, 1));
1455 r
= fflush_and_check(f
);
1458 unit_write_setting(u
, flags
, name
, buf
);
1463 } else if (STR_IN_SET(name
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
1469 read
= streq(name
, "BlockIOReadBandwidth");
1471 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1475 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1477 if (!path_is_normalized(path
))
1478 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1480 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1481 CGroupBlockIODeviceBandwidth
*a
= NULL
;
1483 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
)
1484 if (path_equal(path
, b
->path
)) {
1490 a
= new0(CGroupBlockIODeviceBandwidth
, 1);
1494 a
->rbps
= CGROUP_LIMIT_MAX
;
1495 a
->wbps
= CGROUP_LIMIT_MAX
;
1496 a
->path
= strdup(path
);
1502 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, a
);
1516 r
= sd_bus_message_exit_container(message
);
1520 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1521 _cleanup_free_
char *buf
= NULL
;
1522 _cleanup_fclose_
FILE *f
= NULL
;
1526 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
) {
1528 a
->rbps
= CGROUP_LIMIT_MAX
;
1530 a
->wbps
= CGROUP_LIMIT_MAX
;
1533 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1535 f
= open_memstream_unlocked(&buf
, &size
);
1540 fputs("BlockIOReadBandwidth=\n", f
);
1541 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1542 if (a
->rbps
!= CGROUP_LIMIT_MAX
)
1543 fprintf(f
, "BlockIOReadBandwidth=%s %" PRIu64
"\n", a
->path
, a
->rbps
);
1545 fputs("BlockIOWriteBandwidth=\n", f
);
1546 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1547 if (a
->wbps
!= CGROUP_LIMIT_MAX
)
1548 fprintf(f
, "BlockIOWriteBandwidth=%s %" PRIu64
"\n", a
->path
, a
->wbps
);
1551 r
= fflush_and_check(f
);
1555 unit_write_setting(u
, flags
, name
, buf
);
1560 } else if (streq(name
, "BlockIODeviceWeight")) {
1565 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1569 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1571 if (!path_is_normalized(path
))
1572 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1574 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight
) || weight
== CGROUP_BLKIO_WEIGHT_INVALID
)
1575 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "BlockIODeviceWeight= out of range");
1577 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1578 CGroupBlockIODeviceWeight
*a
= NULL
;
1580 LIST_FOREACH(device_weights
, b
, c
->blockio_device_weights
)
1581 if (path_equal(b
->path
, path
)) {
1587 a
= new0(CGroupBlockIODeviceWeight
, 1);
1591 a
->path
= strdup(path
);
1596 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, a
);
1605 r
= sd_bus_message_exit_container(message
);
1609 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1610 _cleanup_free_
char *buf
= NULL
;
1611 _cleanup_fclose_
FILE *f
= NULL
;
1615 while (c
->blockio_device_weights
)
1616 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
1618 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1620 f
= open_memstream_unlocked(&buf
, &size
);
1624 fputs("BlockIODeviceWeight=\n", f
);
1625 LIST_FOREACH(device_weights
, a
, c
->blockio_device_weights
)
1626 fprintf(f
, "BlockIODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1628 r
= fflush_and_check(f
);
1632 unit_write_setting(u
, flags
, name
, buf
);
1637 } else if (streq(name
, "DevicePolicy")) {
1639 CGroupDevicePolicy p
;
1641 r
= sd_bus_message_read(message
, "s", &policy
);
1645 p
= cgroup_device_policy_from_string(policy
);
1649 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1650 c
->device_policy
= p
;
1651 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1652 unit_write_settingf(u
, flags
, name
, "DevicePolicy=%s", policy
);
1657 } else if (streq(name
, "DeviceAllow")) {
1658 const char *path
, *rwm
;
1661 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
1665 while ((r
= sd_bus_message_read(message
, "(ss)", &path
, &rwm
)) > 0) {
1667 if (!valid_device_allow_pattern(path
) || strpbrk(path
, WHITESPACE
))
1668 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires device node or pattern");
1672 else if (!in_charset(rwm
, "rwm"))
1673 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires combination of rwm flags");
1675 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1676 CGroupDeviceAllow
*a
= NULL
;
1678 LIST_FOREACH(device_allow
, b
, c
->device_allow
)
1679 if (path_equal(b
->path
, path
)) {
1685 a
= new0(CGroupDeviceAllow
, 1);
1689 a
->path
= strdup(path
);
1695 LIST_PREPEND(device_allow
, c
->device_allow
, a
);
1698 a
->r
= strchr(rwm
, 'r');
1699 a
->w
= strchr(rwm
, 'w');
1700 a
->m
= strchr(rwm
, 'm');
1708 r
= sd_bus_message_exit_container(message
);
1712 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1713 _cleanup_free_
char *buf
= NULL
;
1714 _cleanup_fclose_
FILE *f
= NULL
;
1718 while (c
->device_allow
)
1719 cgroup_context_free_device_allow(c
, c
->device_allow
);
1721 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1723 f
= open_memstream_unlocked(&buf
, &size
);
1727 fputs("DeviceAllow=\n", f
);
1728 LIST_FOREACH(device_allow
, a
, c
->device_allow
)
1729 fprintf(f
, "DeviceAllow=%s %s%s%s\n", a
->path
, a
->r
? "r" : "", a
->w
? "w" : "", a
->m
? "m" : "");
1731 r
= fflush_and_check(f
);
1734 unit_write_setting(u
, flags
, name
, buf
);
1739 } else if (streq(name
, "IPAccounting")) {
1742 r
= sd_bus_message_read(message
, "b", &b
);
1746 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1747 c
->ip_accounting
= b
;
1749 unit_invalidate_cgroup_bpf(u
);
1750 unit_write_settingf(u
, flags
, name
, "IPAccounting=%s", yes_no(b
));
1755 } else if (STR_IN_SET(name
, "IPAddressAllow", "IPAddressDeny")) {
1756 _cleanup_set_free_ Set
*new_prefixes
= NULL
;
1759 r
= sd_bus_message_enter_container(message
, 'a', "(iayu)");
1769 r
= sd_bus_message_enter_container(message
, 'r', "iayu");
1775 r
= sd_bus_message_read(message
, "i", &family
);
1779 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1780 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects IPv4 or IPv6 addresses only.", name
);
1782 r
= sd_bus_message_read_array(message
, 'y', &ap
, &an
);
1786 if (an
!= FAMILY_ADDRESS_SIZE(family
))
1787 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1788 af_to_name(family
), FAMILY_ADDRESS_SIZE(family
), an
);
1790 r
= sd_bus_message_read(message
, "u", &prefixlen
);
1794 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
)*8)
1795 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
));
1797 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1798 struct in_addr_prefix prefix
= {
1800 .prefixlen
= prefixlen
,
1803 memcpy(&prefix
.address
, ap
, an
);
1805 r
= in_addr_prefix_add(&new_prefixes
, &prefix
);
1810 r
= sd_bus_message_exit_container(message
);
1817 r
= sd_bus_message_exit_container(message
);
1821 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1822 _cleanup_free_
char *buf
= NULL
;
1823 _cleanup_fclose_
FILE *f
= NULL
;
1828 unit_invalidate_cgroup_bpf(u
);
1829 f
= open_memstream_unlocked(&buf
, &size
);
1833 prefixes
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow
: &c
->ip_address_deny
;
1834 reduced
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow_reduced
: &c
->ip_address_deny_reduced
;
1838 *prefixes
= set_free(*prefixes
);
1844 r
= in_addr_prefixes_merge(prefixes
, new_prefixes
);
1848 const struct in_addr_prefix
*p
;
1849 SET_FOREACH(p
, new_prefixes
)
1850 fprintf(f
, "%s=%s\n", name
,
1851 IN_ADDR_PREFIX_TO_STRING(p
->family
, &p
->address
, p
->prefixlen
));
1854 r
= fflush_and_check(f
);
1858 unit_write_setting(u
, flags
, name
, buf
);
1864 if (STR_IN_SET(name
, "ManagedOOMSwap", "ManagedOOMMemoryPressure")) {
1865 ManagedOOMMode
*cgroup_mode
= streq(name
, "ManagedOOMSwap") ? &c
->moom_swap
: &c
->moom_mem_pressure
;
1869 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
1870 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
1872 r
= sd_bus_message_read(message
, "s", &mode
);
1876 m
= managed_oom_mode_from_string(mode
);
1880 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1882 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, mode
);
1885 (void) manager_varlink_send_managed_oom_update(u
);
1889 if (streq(name
, "ManagedOOMMemoryPressureLimit")) {
1892 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
1893 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
1895 r
= sd_bus_message_read(message
, "u", &v
);
1899 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1900 c
->moom_mem_pressure_limit
= v
;
1901 unit_write_settingf(u
, flags
, name
,
1902 "ManagedOOMMemoryPressureLimit=" PERMYRIAD_AS_PERCENT_FORMAT_STR
,
1903 PERMYRIAD_AS_PERCENT_FORMAT_VAL(UINT32_SCALE_TO_PERMYRIAD(v
)));
1906 if (c
->moom_mem_pressure
== MANAGED_OOM_KILL
)
1907 (void) manager_varlink_send_managed_oom_update(u
);
1912 if (streq(name
, "ManagedOOMPreference")) {
1913 ManagedOOMPreference p
;
1916 r
= sd_bus_message_read(message
, "s", &pref
);
1920 p
= managed_oom_preference_from_string(pref
);
1924 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1925 c
->moom_preference
= p
;
1926 unit_write_settingf(u
, flags
, name
, "ManagedOOMPreference=%s", pref
);
1931 if (STR_IN_SET(name
, "SocketBindAllow", "SocketBindDeny")) {
1932 CGroupSocketBindItem
**list
;
1933 uint16_t nr_ports
, port_min
;
1935 int32_t family
, ip_protocol
;
1937 list
= streq(name
, "SocketBindAllow") ? &c
->socket_bind_allow
: &c
->socket_bind_deny
;
1939 r
= sd_bus_message_enter_container(message
, 'a', "(iiqq)");
1943 while ((r
= sd_bus_message_read(message
, "(iiqq)", &family
, &ip_protocol
, &nr_ports
, &port_min
)) > 0) {
1945 if (!IN_SET(family
, AF_UNSPEC
, AF_INET
, AF_INET6
))
1946 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects INET or INET6 family, if specified.", name
);
1948 if (!IN_SET(ip_protocol
, 0, IPPROTO_TCP
, IPPROTO_UDP
))
1949 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects TCP or UDP protocol, if specified.", name
);
1951 if (port_min
+ (uint32_t) nr_ports
> (1 << 16))
1952 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects maximum port value lesser than 65536.", name
);
1954 if (port_min
== 0 && nr_ports
!= 0)
1955 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects port range starting with positive value.", name
);
1957 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1958 _cleanup_free_ CGroupSocketBindItem
*item
= NULL
;
1960 item
= new(CGroupSocketBindItem
, 1);
1964 *item
= (CGroupSocketBindItem
) {
1965 .address_family
= family
,
1966 .ip_protocol
= ip_protocol
,
1967 .nr_ports
= nr_ports
,
1968 .port_min
= port_min
1971 LIST_PREPEND(socket_bind_items
, *list
, TAKE_PTR(item
));
1978 r
= sd_bus_message_exit_container(message
);
1982 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1983 _cleanup_free_
char *buf
= NULL
;
1984 _cleanup_fclose_
FILE *f
= NULL
;
1988 cgroup_context_remove_socket_bind(list
);
1990 if ((u
->manager
->cgroup_supported
& CGROUP_MASK_BPF_SOCKET_BIND
) == 0)
1992 "Unit %s configures source compiled BPF programs "
1993 "but the local system does not support that.\n"
1994 "Starting this unit will fail!", u
->id
);
1997 f
= open_memstream_unlocked(&buf
, &size
);
2001 fprintf(f
, "%s:", name
);
2003 LIST_FOREACH(socket_bind_items
, item
, *list
)
2004 cgroup_context_dump_socket_bind_item(item
, f
);
2008 r
= fflush_and_check(f
);
2012 unit_write_setting(u
, flags
, name
, buf
);
2017 if (streq(name
, "RestrictNetworkInterfaces")) {
2019 _cleanup_strv_free_
char **l
= NULL
;
2021 r
= sd_bus_message_enter_container(message
, 'r', "bas");
2025 r
= sd_bus_message_read(message
, "b", &is_allow_list
);
2029 r
= sd_bus_message_read_strv(message
, &l
);
2033 r
= sd_bus_message_exit_container(message
);
2037 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2038 _cleanup_free_
char *joined
= NULL
;
2040 if (strv_isempty(l
)) {
2041 c
->restrict_network_interfaces_is_allow_list
= false;
2042 c
->restrict_network_interfaces
= set_free(c
->restrict_network_interfaces
);
2044 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2048 if (set_isempty(c
->restrict_network_interfaces
))
2049 c
->restrict_network_interfaces_is_allow_list
= is_allow_list
;
2051 STRV_FOREACH(s
, l
) {
2052 if (!ifname_valid(*s
)) {
2053 log_full(LOG_WARNING
, "Invalid interface name, ignoring: %s", *s
);
2056 if (c
->restrict_network_interfaces_is_allow_list
!= (bool) is_allow_list
)
2057 free(set_remove(c
->restrict_network_interfaces
, *s
));
2059 r
= set_put_strdup(&c
->restrict_network_interfaces
, *s
);
2065 joined
= strv_join(l
, " ");
2069 unit_write_settingf(u
, flags
, name
, "%s=%s%s", name
, is_allow_list
? "" : "~", joined
);
2075 if (streq(name
, "DisableControllers") || (u
->transient
&& u
->load_state
== UNIT_STUB
))
2076 return bus_cgroup_set_transient_property(u
, c
, name
, message
, flags
, error
);