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("DefaultStartupMemoryLow", "t", NULL
, offsetof(CGroupContext
, default_startup_memory_low
), 0),
466 SD_BUS_PROPERTY("DefaultMemoryMin", "t", NULL
, offsetof(CGroupContext
, default_memory_min
), 0),
467 SD_BUS_PROPERTY("MemoryMin", "t", NULL
, offsetof(CGroupContext
, memory_min
), 0),
468 SD_BUS_PROPERTY("MemoryLow", "t", NULL
, offsetof(CGroupContext
, memory_low
), 0),
469 SD_BUS_PROPERTY("StartupMemoryLow", "t", NULL
, offsetof(CGroupContext
, startup_memory_low
), 0),
470 SD_BUS_PROPERTY("MemoryHigh", "t", NULL
, offsetof(CGroupContext
, memory_high
), 0),
471 SD_BUS_PROPERTY("StartupMemoryHigh", "t", NULL
, offsetof(CGroupContext
, startup_memory_high
), 0),
472 SD_BUS_PROPERTY("MemoryMax", "t", NULL
, offsetof(CGroupContext
, memory_max
), 0),
473 SD_BUS_PROPERTY("StartupMemoryMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_max
), 0),
474 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL
, offsetof(CGroupContext
, memory_swap_max
), 0),
475 SD_BUS_PROPERTY("StartupMemorySwapMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_swap_max
), 0),
476 SD_BUS_PROPERTY("MemoryZSwapMax", "t", NULL
, offsetof(CGroupContext
, memory_zswap_max
), 0),
477 SD_BUS_PROPERTY("StartupMemoryZSwapMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_zswap_max
), 0),
478 SD_BUS_PROPERTY("MemoryLimit", "t", NULL
, offsetof(CGroupContext
, memory_limit
), 0),
479 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy
, offsetof(CGroupContext
, device_policy
), 0),
480 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow
, 0, 0),
481 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, tasks_accounting
), 0),
482 SD_BUS_PROPERTY("TasksMax", "t", bus_property_get_tasks_max
, offsetof(CGroupContext
, tasks_max
), 0),
483 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, ip_accounting
), 0),
484 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_allow
), 0),
485 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_deny
), 0),
486 SD_BUS_PROPERTY("IPIngressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_ingress
), 0),
487 SD_BUS_PROPERTY("IPEgressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_egress
), 0),
488 SD_BUS_PROPERTY("DisableControllers", "as", property_get_cgroup_mask
, offsetof(CGroupContext
, disable_controllers
), 0),
489 SD_BUS_PROPERTY("ManagedOOMSwap", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_swap
), 0),
490 SD_BUS_PROPERTY("ManagedOOMMemoryPressure", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_mem_pressure
), 0),
491 SD_BUS_PROPERTY("ManagedOOMMemoryPressureLimit", "u", NULL
, offsetof(CGroupContext
, moom_mem_pressure_limit
), 0),
492 SD_BUS_PROPERTY("ManagedOOMPreference", "s", property_get_managed_oom_preference
, offsetof(CGroupContext
, moom_preference
), 0),
493 SD_BUS_PROPERTY("BPFProgram", "a(ss)", property_get_bpf_foreign_program
, 0, 0),
494 SD_BUS_PROPERTY("SocketBindAllow", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_allow
), 0),
495 SD_BUS_PROPERTY("SocketBindDeny", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_deny
), 0),
496 SD_BUS_PROPERTY("RestrictNetworkInterfaces", "(bas)", property_get_restrict_network_interfaces
, 0, 0),
500 static int bus_cgroup_set_transient_property(
504 sd_bus_message
*message
,
505 UnitWriteFlags flags
,
506 sd_bus_error
*error
) {
515 flags
|= UNIT_PRIVATE
;
517 if (streq(name
, "Delegate")) {
520 if (!UNIT_VTABLE(u
)->can_delegate
)
521 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
523 r
= sd_bus_message_read(message
, "b", &b
);
527 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
529 c
->delegate_controllers
= b
? _CGROUP_MASK_ALL
: 0;
531 unit_write_settingf(u
, flags
, name
, "Delegate=%s", yes_no(b
));
536 } else if (STR_IN_SET(name
, "DelegateControllers", "DisableControllers")) {
539 if (streq(name
, "DelegateControllers") && !UNIT_VTABLE(u
)->can_delegate
)
540 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
542 r
= sd_bus_message_enter_container(message
, 'a', "s");
550 r
= sd_bus_message_read(message
, "s", &t
);
556 cc
= cgroup_controller_from_string(t
);
558 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown cgroup controller '%s'", t
);
560 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
563 r
= sd_bus_message_exit_container(message
);
567 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
568 _cleanup_free_
char *t
= NULL
;
570 r
= cg_mask_to_string(mask
, &t
);
574 if (streq(name
, "DelegateControllers")) {
578 c
->delegate_controllers
= 0;
580 c
->delegate_controllers
|= mask
;
582 unit_write_settingf(u
, flags
, name
, "Delegate=%s", strempty(t
));
584 } else if (streq(name
, "DisableControllers")) {
587 c
->disable_controllers
= 0;
589 c
->disable_controllers
|= mask
;
591 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, strempty(t
));
596 } else if (STR_IN_SET(name
, "IPIngressFilterPath", "IPEgressFilterPath")) {
600 filters
= streq(name
, "IPIngressFilterPath") ? &c
->ip_filters_ingress
: &c
->ip_filters_egress
;
601 r
= sd_bus_message_enter_container(message
, 'a', "s");
608 r
= sd_bus_message_read(message
, "s", &path
);
614 if (!path_is_normalized(path
) || !path_is_absolute(path
))
615 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects a normalized absolute path.", name
);
617 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && !strv_contains(*filters
, path
)) {
618 r
= strv_extend(filters
, path
);
624 r
= sd_bus_message_exit_container(message
);
628 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
629 _cleanup_free_
char *buf
= NULL
;
630 _cleanup_fclose_
FILE *f
= NULL
;
634 *filters
= strv_free(*filters
);
636 unit_invalidate_cgroup_bpf(u
);
637 f
= open_memstream_unlocked(&buf
, &size
);
644 STRV_FOREACH(entry
, *filters
)
645 fprintf(f
, "%s=%s\n", name
, *entry
);
647 r
= fflush_and_check(f
);
651 unit_write_setting(u
, flags
, name
, buf
);
654 r
= bpf_firewall_supported();
657 if (r
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
) {
658 static bool warned
= false;
660 log_full(warned
? LOG_DEBUG
: LOG_WARNING
,
661 "Transient unit %s configures an IP firewall with BPF, but the local system does not support BPF/cgroup firewalling with multiple filters.\n"
662 "Starting this unit will fail! (This warning is only shown for the first started transient unit using IP firewalling.)", u
->id
);
669 } else if (streq(name
, "BPFProgram")) {
673 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
677 while ((r
= sd_bus_message_read(message
, "(ss)", &a
, &p
)) > 0) {
678 int attach_type
= bpf_cgroup_attach_type_from_string(a
);
680 return sd_bus_error_setf(
682 SD_BUS_ERROR_INVALID_ARGS
,
683 "%s expects a valid BPF attach type, got '%s'.",
686 if (!path_is_normalized(p
) || !path_is_absolute(p
))
687 return sd_bus_error_setf(
689 SD_BUS_ERROR_INVALID_ARGS
,
690 "%s= expects a normalized absolute path.",
693 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
694 r
= cgroup_add_bpf_foreign_program(c
, attach_type
, p
);
703 r
= sd_bus_message_exit_container(message
);
707 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
708 _cleanup_free_
char *buf
= NULL
;
709 _cleanup_fclose_
FILE *f
= NULL
;
713 while (c
->bpf_foreign_programs
)
714 cgroup_context_remove_bpf_foreign_program(c
, c
->bpf_foreign_programs
);
716 f
= open_memstream_unlocked(&buf
, &size
);
723 LIST_FOREACH(programs
, fp
, c
->bpf_foreign_programs
)
724 fprintf(f
, "%s=%s:%s\n", name
,
725 bpf_cgroup_attach_type_to_string(fp
->attach_type
),
728 r
= fflush_and_check(f
);
732 unit_write_setting(u
, flags
, name
, buf
);
734 if (c
->bpf_foreign_programs
) {
735 r
= bpf_foreign_supported();
740 "Transient unit %s configures a BPF program pinned to BPF "
741 "filesystem, but the local system does not support that.\n"
742 "Starting this unit will fail!", u
->id
);
752 static int bus_cgroup_set_boolean(
757 sd_bus_message
*message
,
758 UnitWriteFlags flags
,
759 sd_bus_error
*error
) {
765 r
= sd_bus_message_read(message
, "b", &b
);
769 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
771 unit_invalidate_cgroup(u
, mask
);
772 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, yes_no(b
));
778 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
779 static int bus_cgroup_set_##function( \
783 sd_bus_message *message, \
784 UnitWriteFlags flags, \
785 sd_bus_error *error) { \
792 r = sd_bus_message_read(message, "t", &v); \
797 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
798 "Value specified in %s is out of range", name); \
800 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
802 unit_invalidate_cgroup(u, mask); \
805 unit_write_settingf(u, flags, name, \
808 unit_write_settingf(u, flags, name, \
809 "%s=%" PRIu64, name, v); \
815 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
816 static int bus_cgroup_set_##function( \
820 sd_bus_message *message, \
821 UnitWriteFlags flags, \
822 sd_bus_error *error) { \
829 r = sd_bus_message_read(message, "t", &v); \
834 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
835 "Value specified in %s is out of range", name); \
837 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
839 unit_invalidate_cgroup(u, mask); \
841 if (v == CGROUP_LIMIT_MAX) \
842 unit_write_settingf(u, flags, name, \
843 "%s=infinity", name); \
845 unit_write_settingf(u, flags, name, \
846 "%s=%" PRIu64, name, v); \
851 static int bus_cgroup_set_##function##_scale( \
855 sd_bus_message *message, \
856 UnitWriteFlags flags, \
857 sd_bus_error *error) { \
865 r = sd_bus_message_read(message, "u", &raw); \
869 v = scale(raw, UINT32_MAX); \
870 if (v < minimum || v >= UINT64_MAX) \
871 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
872 "Value specified in %s is out of range", name); \
874 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
876 unit_invalidate_cgroup(u, mask); \
878 /* Prepare to chop off suffix */ \
879 assert_se(endswith(name, "Scale")); \
881 int scaled = UINT32_SCALE_TO_PERMYRIAD(raw); \
882 unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR, \
883 (int)(strlen(name) - strlen("Scale")), name, \
884 PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled)); \
890 DISABLE_WARNING_TYPE_LIMITS
;
891 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares
, CGROUP_MASK_CPU
, CGROUP_CPU_SHARES_IS_OK
, CGROUP_CPU_SHARES_INVALID
);
892 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight
, CGROUP_MASK_IO
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
893 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight
, CGROUP_MASK_BLKIO
, CGROUP_BLKIO_WEIGHT_IS_OK
, CGROUP_BLKIO_WEIGHT_INVALID
);
894 BUS_DEFINE_SET_CGROUP_LIMIT(memory
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 1);
895 BUS_DEFINE_SET_CGROUP_LIMIT(memory_protection
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
896 BUS_DEFINE_SET_CGROUP_LIMIT(swap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
897 BUS_DEFINE_SET_CGROUP_LIMIT(zswap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
900 static int bus_cgroup_set_cpu_weight(
904 sd_bus_message
*message
,
905 UnitWriteFlags flags
,
906 sd_bus_error
*error
) {
910 r
= sd_bus_message_read(message
, "t", &v
);
913 if (!CGROUP_WEIGHT_IS_OK(v
) && v
!= CGROUP_WEIGHT_IDLE
)
914 return sd_bus_error_setf(
915 error
, SD_BUS_ERROR_INVALID_ARGS
, "Value specified in %s is out of range", name
);
916 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
918 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
919 if (v
== CGROUP_WEIGHT_INVALID
)
920 unit_write_settingf(u
, flags
, name
, "%s=", name
);
921 else if (v
== CGROUP_WEIGHT_IDLE
)
922 unit_write_settingf(u
, flags
, name
, "%s=idle", name
);
924 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu64
, name
, v
);
929 static int bus_cgroup_set_tasks_max(
933 sd_bus_message
*message
,
934 UnitWriteFlags flags
,
935 sd_bus_error
*error
) {
942 r
= sd_bus_message_read(message
, "t", &v
);
947 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
948 "Value specified in %s is out of range", name
);
950 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
951 *p
= (TasksMax
) { .value
= v
, .scale
= 0 }; /* When .scale==0, .value is the absolute value */
952 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
954 if (v
== CGROUP_LIMIT_MAX
)
955 unit_write_settingf(u
, flags
, name
,
956 "%s=infinity", name
);
958 unit_write_settingf(u
, flags
, name
,
959 "%s=%" PRIu64
, name
, v
);
965 static int bus_cgroup_set_tasks_max_scale(
969 sd_bus_message
*message
,
970 UnitWriteFlags flags
,
971 sd_bus_error
*error
) {
978 r
= sd_bus_message_read(message
, "u", &v
);
982 if (v
< 1 || v
>= UINT32_MAX
)
983 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
984 "Value specified in %s is out of range", name
);
986 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
987 *p
= (TasksMax
) { v
, UINT32_MAX
}; /* .scale is not 0, so this is interpreted as v/UINT32_MAX. */
988 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
990 uint32_t scaled
= DIV_ROUND_UP((uint64_t) v
* 100U, (uint64_t) UINT32_MAX
);
991 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu32
".%" PRIu32
"%%", "TasksMax",
992 scaled
/ 10, scaled
% 10);
998 int bus_cgroup_set_property(
1002 sd_bus_message
*message
,
1003 UnitWriteFlags flags
,
1004 sd_bus_error
*error
) {
1006 CGroupIOLimitType iol_type
;
1014 flags
|= UNIT_PRIVATE
;
1016 if (streq(name
, "CPUAccounting"))
1017 return bus_cgroup_set_boolean(u
, name
, &c
->cpu_accounting
, get_cpu_accounting_mask(), message
, flags
, error
);
1019 if (streq(name
, "CPUWeight"))
1020 return bus_cgroup_set_cpu_weight(u
, name
, &c
->cpu_weight
, message
, flags
, error
);
1022 if (streq(name
, "StartupCPUWeight"))
1023 return bus_cgroup_set_cpu_weight(u
, name
, &c
->startup_cpu_weight
, message
, flags
, error
);
1025 if (streq(name
, "CPUShares"))
1026 return bus_cgroup_set_cpu_shares(u
, name
, &c
->cpu_shares
, message
, flags
, error
);
1028 if (streq(name
, "StartupCPUShares"))
1029 return bus_cgroup_set_cpu_shares(u
, name
, &c
->startup_cpu_shares
, message
, flags
, error
);
1031 if (streq(name
, "IOAccounting"))
1032 return bus_cgroup_set_boolean(u
, name
, &c
->io_accounting
, CGROUP_MASK_IO
, message
, flags
, error
);
1034 if (streq(name
, "IOWeight"))
1035 return bus_cgroup_set_io_weight(u
, name
, &c
->io_weight
, message
, flags
, error
);
1037 if (streq(name
, "StartupIOWeight"))
1038 return bus_cgroup_set_io_weight(u
, name
, &c
->startup_io_weight
, message
, flags
, error
);
1040 if (streq(name
, "BlockIOAccounting"))
1041 return bus_cgroup_set_boolean(u
, name
, &c
->blockio_accounting
, CGROUP_MASK_BLKIO
, message
, flags
, error
);
1043 if (streq(name
, "BlockIOWeight"))
1044 return bus_cgroup_set_blockio_weight(u
, name
, &c
->blockio_weight
, message
, flags
, error
);
1046 if (streq(name
, "StartupBlockIOWeight"))
1047 return bus_cgroup_set_blockio_weight(u
, name
, &c
->startup_blockio_weight
, message
, flags
, error
);
1049 if (streq(name
, "MemoryAccounting"))
1050 return bus_cgroup_set_boolean(u
, name
, &c
->memory_accounting
, CGROUP_MASK_MEMORY
, message
, flags
, error
);
1052 if (streq(name
, "MemoryMin")) {
1053 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_min
, message
, flags
, error
);
1055 c
->memory_min_set
= true;
1059 if (streq(name
, "MemoryLow")) {
1060 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_low
, message
, flags
, error
);
1062 c
->memory_low_set
= true;
1066 if (streq(name
, "StartupMemoryLow")) {
1067 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->startup_memory_low
, message
, flags
, error
);
1069 c
->startup_memory_low_set
= true;
1073 if (streq(name
, "DefaultMemoryMin")) {
1074 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1076 c
->default_memory_min_set
= true;
1080 if (streq(name
, "DefaultMemoryLow")) {
1081 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1083 c
->default_memory_low_set
= true;
1087 if (streq(name
, "DefaultStartupMemoryLow")) {
1088 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_startup_memory_low
, message
, flags
, error
);
1090 c
->default_startup_memory_low_set
= true;
1094 if (streq(name
, "MemoryHigh"))
1095 return bus_cgroup_set_memory(u
, name
, &c
->memory_high
, message
, flags
, error
);
1097 if (streq(name
, "StartupMemoryHigh")) {
1098 r
= bus_cgroup_set_memory(u
, name
, &c
->startup_memory_high
, message
, flags
, error
);
1100 c
->startup_memory_high_set
= true;
1104 if (streq(name
, "MemorySwapMax"))
1105 return bus_cgroup_set_swap(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1107 if (streq(name
, "StartupMemorySwapMax")) {
1108 r
= bus_cgroup_set_swap(u
, name
, &c
->startup_memory_swap_max
, message
, flags
, error
);
1110 c
->startup_memory_swap_max_set
= true;
1114 if (streq(name
, "MemoryZSwapMax"))
1115 return bus_cgroup_set_zswap(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1117 if (streq(name
, "StartupMemoryZSwapMax")) {
1118 r
= bus_cgroup_set_zswap(u
, name
, &c
->startup_memory_zswap_max
, message
, flags
, error
);
1120 c
->startup_memory_zswap_max_set
= true;
1124 if (streq(name
, "MemoryMax"))
1125 return bus_cgroup_set_memory(u
, name
, &c
->memory_max
, message
, flags
, error
);
1127 if (streq(name
, "StartupMemoryMax")) {
1128 r
= bus_cgroup_set_memory(u
, name
, &c
->startup_memory_max
, message
, flags
, error
);
1130 c
->startup_memory_max_set
= true;
1134 if (streq(name
, "MemoryLimit"))
1135 return bus_cgroup_set_memory(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1137 if (streq(name
, "MemoryMinScale")) {
1138 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_min
, message
, flags
, error
);
1140 c
->memory_min_set
= true;
1144 if (streq(name
, "MemoryLowScale")) {
1145 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_low
, message
, flags
, error
);
1147 c
->memory_low_set
= true;
1151 if (streq(name
, "DefaultMemoryMinScale")) {
1152 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1154 c
->default_memory_min_set
= true;
1158 if (streq(name
, "DefaultMemoryLowScale")) {
1159 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1161 c
->default_memory_low_set
= true;
1165 if (streq(name
, "MemoryHighScale"))
1166 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_high
, message
, flags
, error
);
1168 if (streq(name
, "MemorySwapMaxScale"))
1169 return bus_cgroup_set_swap_scale(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1171 if (streq(name
, "MemoryZSwapMaxScale"))
1172 return bus_cgroup_set_zswap_scale(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1174 if (streq(name
, "MemoryMaxScale"))
1175 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_max
, message
, flags
, error
);
1177 if (streq(name
, "MemoryLimitScale"))
1178 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1180 if (streq(name
, "TasksAccounting"))
1181 return bus_cgroup_set_boolean(u
, name
, &c
->tasks_accounting
, CGROUP_MASK_PIDS
, message
, flags
, error
);
1183 if (streq(name
, "TasksMax"))
1184 return bus_cgroup_set_tasks_max(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1186 if (streq(name
, "TasksMaxScale"))
1187 return bus_cgroup_set_tasks_max_scale(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1189 if (streq(name
, "CPUQuotaPerSecUSec")) {
1192 r
= sd_bus_message_read(message
, "t", &u64
);
1197 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "CPUQuotaPerSecUSec= value out of range");
1199 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1200 c
->cpu_quota_per_sec_usec
= u64
;
1201 u
->warned_clamping_cpu_quota_period
= false;
1202 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1204 if (c
->cpu_quota_per_sec_usec
== USEC_INFINITY
)
1205 unit_write_setting(u
, flags
, "CPUQuota", "CPUQuota=");
1207 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
1209 unit_write_settingf(u
, flags
, "CPUQuota",
1211 (double) (c
->cpu_quota_per_sec_usec
/ 10000));
1216 } else if (streq(name
, "CPUQuotaPeriodUSec")) {
1219 r
= sd_bus_message_read(message
, "t", &u64
);
1223 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1224 c
->cpu_quota_period_usec
= u64
;
1225 u
->warned_clamping_cpu_quota_period
= false;
1226 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1227 if (c
->cpu_quota_period_usec
== USEC_INFINITY
)
1228 unit_write_setting(u
, flags
, "CPUQuotaPeriodSec", "CPUQuotaPeriodSec=");
1230 unit_write_settingf(u
, flags
, "CPUQuotaPeriodSec",
1231 "CPUQuotaPeriodSec=%s",
1232 FORMAT_TIMESPAN(c
->cpu_quota_period_usec
, 1));
1237 } else if (STR_IN_SET(name
, "AllowedCPUs", "StartupAllowedCPUs", "AllowedMemoryNodes", "StartupAllowedMemoryNodes")) {
1240 _cleanup_(cpu_set_reset
) CPUSet new_set
= {};
1242 r
= sd_bus_message_read_array(message
, 'y', &a
, &n
);
1246 r
= cpu_set_from_dbus(a
, n
, &new_set
);
1250 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1251 _cleanup_free_
char *setstr
= NULL
;
1254 setstr
= cpu_set_to_range_string(&new_set
);
1258 if (streq(name
, "AllowedCPUs"))
1259 set
= &c
->cpuset_cpus
;
1260 else if (streq(name
, "StartupAllowedCPUs"))
1261 set
= &c
->startup_cpuset_cpus
;
1262 else if (streq(name
, "AllowedMemoryNodes"))
1263 set
= &c
->cpuset_mems
;
1264 else if (streq(name
, "StartupAllowedMemoryNodes"))
1265 set
= &c
->startup_cpuset_mems
;
1271 new_set
= (CPUSet
) {};
1273 unit_invalidate_cgroup(u
, CGROUP_MASK_CPUSET
);
1274 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, setstr
);
1279 } else if ((iol_type
= cgroup_io_limit_type_from_string(name
)) >= 0) {
1284 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1288 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1290 if (!path_is_normalized(path
))
1291 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1293 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1294 CGroupIODeviceLimit
*a
= NULL
;
1296 LIST_FOREACH(device_limits
, b
, c
->io_device_limits
)
1297 if (path_equal(path
, b
->path
)) {
1303 CGroupIOLimitType type
;
1305 a
= new0(CGroupIODeviceLimit
, 1);
1309 a
->path
= strdup(path
);
1315 for (type
= 0; type
< _CGROUP_IO_LIMIT_TYPE_MAX
; type
++)
1316 a
->limits
[type
] = cgroup_io_limit_defaults
[type
];
1318 LIST_PREPEND(device_limits
, c
->io_device_limits
, a
);
1321 a
->limits
[iol_type
] = u64
;
1329 r
= sd_bus_message_exit_container(message
);
1333 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1334 _cleanup_free_
char *buf
= NULL
;
1335 _cleanup_fclose_
FILE *f
= NULL
;
1339 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1340 a
->limits
[iol_type
] = cgroup_io_limit_defaults
[iol_type
];
1342 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1344 f
= open_memstream_unlocked(&buf
, &size
);
1348 fprintf(f
, "%s=\n", name
);
1349 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1350 if (a
->limits
[iol_type
] != cgroup_io_limit_defaults
[iol_type
])
1351 fprintf(f
, "%s=%s %" PRIu64
"\n", name
, a
->path
, a
->limits
[iol_type
]);
1353 r
= fflush_and_check(f
);
1356 unit_write_setting(u
, flags
, name
, buf
);
1361 } else if (streq(name
, "IODeviceWeight")) {
1366 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1370 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1372 if (!path_is_normalized(path
))
1373 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1375 if (!CGROUP_WEIGHT_IS_OK(weight
) || weight
== CGROUP_WEIGHT_INVALID
)
1376 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "IODeviceWeight= value out of range");
1378 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1379 CGroupIODeviceWeight
*a
= NULL
;
1381 LIST_FOREACH(device_weights
, b
, c
->io_device_weights
)
1382 if (path_equal(b
->path
, path
)) {
1388 a
= new0(CGroupIODeviceWeight
, 1);
1392 a
->path
= strdup(path
);
1397 LIST_PREPEND(device_weights
, c
->io_device_weights
, a
);
1406 r
= sd_bus_message_exit_container(message
);
1410 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1411 _cleanup_free_
char *buf
= NULL
;
1412 _cleanup_fclose_
FILE *f
= NULL
;
1416 while (c
->io_device_weights
)
1417 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
1419 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1421 f
= open_memstream_unlocked(&buf
, &size
);
1425 fputs("IODeviceWeight=\n", f
);
1426 LIST_FOREACH(device_weights
, a
, c
->io_device_weights
)
1427 fprintf(f
, "IODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1429 r
= fflush_and_check(f
);
1432 unit_write_setting(u
, flags
, name
, buf
);
1437 } else if (streq(name
, "IODeviceLatencyTargetUSec")) {
1442 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1446 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &target
)) > 0) {
1448 if (!path_is_normalized(path
))
1449 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1451 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1452 CGroupIODeviceLatency
*a
= NULL
;
1454 LIST_FOREACH(device_latencies
, b
, c
->io_device_latencies
)
1455 if (path_equal(b
->path
, path
)) {
1461 a
= new0(CGroupIODeviceLatency
, 1);
1465 a
->path
= strdup(path
);
1470 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, a
);
1473 a
->target_usec
= target
;
1479 r
= sd_bus_message_exit_container(message
);
1483 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1484 _cleanup_free_
char *buf
= NULL
;
1485 _cleanup_fclose_
FILE *f
= NULL
;
1489 while (c
->io_device_latencies
)
1490 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
1492 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1494 f
= open_memstream_unlocked(&buf
, &size
);
1498 fputs("IODeviceLatencyTargetSec=\n", f
);
1499 LIST_FOREACH(device_latencies
, a
, c
->io_device_latencies
)
1500 fprintf(f
, "IODeviceLatencyTargetSec=%s %s\n",
1501 a
->path
, FORMAT_TIMESPAN(a
->target_usec
, 1));
1503 r
= fflush_and_check(f
);
1506 unit_write_setting(u
, flags
, name
, buf
);
1511 } else if (STR_IN_SET(name
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
1517 read
= streq(name
, "BlockIOReadBandwidth");
1519 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1523 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1525 if (!path_is_normalized(path
))
1526 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1528 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1529 CGroupBlockIODeviceBandwidth
*a
= NULL
;
1531 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
)
1532 if (path_equal(path
, b
->path
)) {
1538 a
= new0(CGroupBlockIODeviceBandwidth
, 1);
1542 a
->rbps
= CGROUP_LIMIT_MAX
;
1543 a
->wbps
= CGROUP_LIMIT_MAX
;
1544 a
->path
= strdup(path
);
1550 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, a
);
1564 r
= sd_bus_message_exit_container(message
);
1568 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1569 _cleanup_free_
char *buf
= NULL
;
1570 _cleanup_fclose_
FILE *f
= NULL
;
1574 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
) {
1576 a
->rbps
= CGROUP_LIMIT_MAX
;
1578 a
->wbps
= CGROUP_LIMIT_MAX
;
1581 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1583 f
= open_memstream_unlocked(&buf
, &size
);
1588 fputs("BlockIOReadBandwidth=\n", f
);
1589 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1590 if (a
->rbps
!= CGROUP_LIMIT_MAX
)
1591 fprintf(f
, "BlockIOReadBandwidth=%s %" PRIu64
"\n", a
->path
, a
->rbps
);
1593 fputs("BlockIOWriteBandwidth=\n", f
);
1594 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1595 if (a
->wbps
!= CGROUP_LIMIT_MAX
)
1596 fprintf(f
, "BlockIOWriteBandwidth=%s %" PRIu64
"\n", a
->path
, a
->wbps
);
1599 r
= fflush_and_check(f
);
1603 unit_write_setting(u
, flags
, name
, buf
);
1608 } else if (streq(name
, "BlockIODeviceWeight")) {
1613 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1617 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1619 if (!path_is_normalized(path
))
1620 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1622 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight
) || weight
== CGROUP_BLKIO_WEIGHT_INVALID
)
1623 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "BlockIODeviceWeight= out of range");
1625 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1626 CGroupBlockIODeviceWeight
*a
= NULL
;
1628 LIST_FOREACH(device_weights
, b
, c
->blockio_device_weights
)
1629 if (path_equal(b
->path
, path
)) {
1635 a
= new0(CGroupBlockIODeviceWeight
, 1);
1639 a
->path
= strdup(path
);
1644 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, a
);
1653 r
= sd_bus_message_exit_container(message
);
1657 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1658 _cleanup_free_
char *buf
= NULL
;
1659 _cleanup_fclose_
FILE *f
= NULL
;
1663 while (c
->blockio_device_weights
)
1664 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
1666 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1668 f
= open_memstream_unlocked(&buf
, &size
);
1672 fputs("BlockIODeviceWeight=\n", f
);
1673 LIST_FOREACH(device_weights
, a
, c
->blockio_device_weights
)
1674 fprintf(f
, "BlockIODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1676 r
= fflush_and_check(f
);
1680 unit_write_setting(u
, flags
, name
, buf
);
1685 } else if (streq(name
, "DevicePolicy")) {
1687 CGroupDevicePolicy p
;
1689 r
= sd_bus_message_read(message
, "s", &policy
);
1693 p
= cgroup_device_policy_from_string(policy
);
1697 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1698 c
->device_policy
= p
;
1699 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1700 unit_write_settingf(u
, flags
, name
, "DevicePolicy=%s", policy
);
1705 } else if (streq(name
, "DeviceAllow")) {
1706 const char *path
, *rwm
;
1709 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
1713 while ((r
= sd_bus_message_read(message
, "(ss)", &path
, &rwm
)) > 0) {
1715 if (!valid_device_allow_pattern(path
) || strpbrk(path
, WHITESPACE
))
1716 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires device node or pattern");
1720 else if (!in_charset(rwm
, "rwm"))
1721 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires combination of rwm flags");
1723 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1724 CGroupDeviceAllow
*a
= NULL
;
1726 LIST_FOREACH(device_allow
, b
, c
->device_allow
)
1727 if (path_equal(b
->path
, path
)) {
1733 a
= new0(CGroupDeviceAllow
, 1);
1737 a
->path
= strdup(path
);
1743 LIST_PREPEND(device_allow
, c
->device_allow
, a
);
1746 a
->r
= strchr(rwm
, 'r');
1747 a
->w
= strchr(rwm
, 'w');
1748 a
->m
= strchr(rwm
, 'm');
1756 r
= sd_bus_message_exit_container(message
);
1760 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1761 _cleanup_free_
char *buf
= NULL
;
1762 _cleanup_fclose_
FILE *f
= NULL
;
1766 while (c
->device_allow
)
1767 cgroup_context_free_device_allow(c
, c
->device_allow
);
1769 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1771 f
= open_memstream_unlocked(&buf
, &size
);
1775 fputs("DeviceAllow=\n", f
);
1776 LIST_FOREACH(device_allow
, a
, c
->device_allow
)
1777 fprintf(f
, "DeviceAllow=%s %s%s%s\n", a
->path
, a
->r
? "r" : "", a
->w
? "w" : "", a
->m
? "m" : "");
1779 r
= fflush_and_check(f
);
1782 unit_write_setting(u
, flags
, name
, buf
);
1787 } else if (streq(name
, "IPAccounting")) {
1790 r
= sd_bus_message_read(message
, "b", &b
);
1794 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1795 c
->ip_accounting
= b
;
1797 unit_invalidate_cgroup_bpf(u
);
1798 unit_write_settingf(u
, flags
, name
, "IPAccounting=%s", yes_no(b
));
1803 } else if (STR_IN_SET(name
, "IPAddressAllow", "IPAddressDeny")) {
1804 _cleanup_set_free_ Set
*new_prefixes
= NULL
;
1807 r
= sd_bus_message_enter_container(message
, 'a', "(iayu)");
1817 r
= sd_bus_message_enter_container(message
, 'r', "iayu");
1823 r
= sd_bus_message_read(message
, "i", &family
);
1827 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1828 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects IPv4 or IPv6 addresses only.", name
);
1830 r
= sd_bus_message_read_array(message
, 'y', &ap
, &an
);
1834 if (an
!= FAMILY_ADDRESS_SIZE(family
))
1835 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1836 af_to_name(family
), FAMILY_ADDRESS_SIZE(family
), an
);
1838 r
= sd_bus_message_read(message
, "u", &prefixlen
);
1842 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
)*8)
1843 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
));
1845 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1846 struct in_addr_prefix prefix
= {
1848 .prefixlen
= prefixlen
,
1851 memcpy(&prefix
.address
, ap
, an
);
1853 r
= in_addr_prefix_add(&new_prefixes
, &prefix
);
1858 r
= sd_bus_message_exit_container(message
);
1865 r
= sd_bus_message_exit_container(message
);
1869 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1870 _cleanup_free_
char *buf
= NULL
;
1871 _cleanup_fclose_
FILE *f
= NULL
;
1876 unit_invalidate_cgroup_bpf(u
);
1877 f
= open_memstream_unlocked(&buf
, &size
);
1881 prefixes
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow
: &c
->ip_address_deny
;
1882 reduced
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow_reduced
: &c
->ip_address_deny_reduced
;
1886 *prefixes
= set_free(*prefixes
);
1892 r
= in_addr_prefixes_merge(prefixes
, new_prefixes
);
1896 const struct in_addr_prefix
*p
;
1897 SET_FOREACH(p
, new_prefixes
)
1898 fprintf(f
, "%s=%s\n", name
,
1899 IN_ADDR_PREFIX_TO_STRING(p
->family
, &p
->address
, p
->prefixlen
));
1902 r
= fflush_and_check(f
);
1906 unit_write_setting(u
, flags
, name
, buf
);
1912 if (STR_IN_SET(name
, "ManagedOOMSwap", "ManagedOOMMemoryPressure")) {
1913 ManagedOOMMode
*cgroup_mode
= streq(name
, "ManagedOOMSwap") ? &c
->moom_swap
: &c
->moom_mem_pressure
;
1917 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
1918 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
1920 r
= sd_bus_message_read(message
, "s", &mode
);
1924 m
= managed_oom_mode_from_string(mode
);
1928 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1930 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, mode
);
1933 (void) manager_varlink_send_managed_oom_update(u
);
1937 if (streq(name
, "ManagedOOMMemoryPressureLimit")) {
1940 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
1941 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
1943 r
= sd_bus_message_read(message
, "u", &v
);
1947 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1948 c
->moom_mem_pressure_limit
= v
;
1949 unit_write_settingf(u
, flags
, name
,
1950 "ManagedOOMMemoryPressureLimit=" PERMYRIAD_AS_PERCENT_FORMAT_STR
,
1951 PERMYRIAD_AS_PERCENT_FORMAT_VAL(UINT32_SCALE_TO_PERMYRIAD(v
)));
1954 if (c
->moom_mem_pressure
== MANAGED_OOM_KILL
)
1955 (void) manager_varlink_send_managed_oom_update(u
);
1960 if (streq(name
, "ManagedOOMPreference")) {
1961 ManagedOOMPreference p
;
1964 r
= sd_bus_message_read(message
, "s", &pref
);
1968 p
= managed_oom_preference_from_string(pref
);
1972 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1973 c
->moom_preference
= p
;
1974 unit_write_settingf(u
, flags
, name
, "ManagedOOMPreference=%s", pref
);
1979 if (STR_IN_SET(name
, "SocketBindAllow", "SocketBindDeny")) {
1980 CGroupSocketBindItem
**list
;
1981 uint16_t nr_ports
, port_min
;
1983 int32_t family
, ip_protocol
;
1985 list
= streq(name
, "SocketBindAllow") ? &c
->socket_bind_allow
: &c
->socket_bind_deny
;
1987 r
= sd_bus_message_enter_container(message
, 'a', "(iiqq)");
1991 while ((r
= sd_bus_message_read(message
, "(iiqq)", &family
, &ip_protocol
, &nr_ports
, &port_min
)) > 0) {
1993 if (!IN_SET(family
, AF_UNSPEC
, AF_INET
, AF_INET6
))
1994 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects INET or INET6 family, if specified.", name
);
1996 if (!IN_SET(ip_protocol
, 0, IPPROTO_TCP
, IPPROTO_UDP
))
1997 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects TCP or UDP protocol, if specified.", name
);
1999 if (port_min
+ (uint32_t) nr_ports
> (1 << 16))
2000 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects maximum port value lesser than 65536.", name
);
2002 if (port_min
== 0 && nr_ports
!= 0)
2003 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects port range starting with positive value.", name
);
2005 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2006 _cleanup_free_ CGroupSocketBindItem
*item
= NULL
;
2008 item
= new(CGroupSocketBindItem
, 1);
2012 *item
= (CGroupSocketBindItem
) {
2013 .address_family
= family
,
2014 .ip_protocol
= ip_protocol
,
2015 .nr_ports
= nr_ports
,
2016 .port_min
= port_min
2019 LIST_PREPEND(socket_bind_items
, *list
, TAKE_PTR(item
));
2026 r
= sd_bus_message_exit_container(message
);
2030 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2031 _cleanup_free_
char *buf
= NULL
;
2032 _cleanup_fclose_
FILE *f
= NULL
;
2036 cgroup_context_remove_socket_bind(list
);
2038 if ((u
->manager
->cgroup_supported
& CGROUP_MASK_BPF_SOCKET_BIND
) == 0)
2040 "Unit %s configures source compiled BPF programs "
2041 "but the local system does not support that.\n"
2042 "Starting this unit will fail!", u
->id
);
2045 f
= open_memstream_unlocked(&buf
, &size
);
2049 fprintf(f
, "%s:", name
);
2051 LIST_FOREACH(socket_bind_items
, item
, *list
)
2052 cgroup_context_dump_socket_bind_item(item
, f
);
2056 r
= fflush_and_check(f
);
2060 unit_write_setting(u
, flags
, name
, buf
);
2065 if (streq(name
, "RestrictNetworkInterfaces")) {
2067 _cleanup_strv_free_
char **l
= NULL
;
2069 r
= sd_bus_message_enter_container(message
, 'r', "bas");
2073 r
= sd_bus_message_read(message
, "b", &is_allow_list
);
2077 r
= sd_bus_message_read_strv(message
, &l
);
2081 r
= sd_bus_message_exit_container(message
);
2085 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2086 _cleanup_free_
char *joined
= NULL
;
2088 if (strv_isempty(l
)) {
2089 c
->restrict_network_interfaces_is_allow_list
= false;
2090 c
->restrict_network_interfaces
= set_free(c
->restrict_network_interfaces
);
2092 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2096 if (set_isempty(c
->restrict_network_interfaces
))
2097 c
->restrict_network_interfaces_is_allow_list
= is_allow_list
;
2099 STRV_FOREACH(s
, l
) {
2100 if (!ifname_valid(*s
)) {
2101 log_full(LOG_WARNING
, "Invalid interface name, ignoring: %s", *s
);
2104 if (c
->restrict_network_interfaces_is_allow_list
!= (bool) is_allow_list
)
2105 free(set_remove(c
->restrict_network_interfaces
, *s
));
2107 r
= set_put_strdup(&c
->restrict_network_interfaces
, *s
);
2113 joined
= strv_join(l
, " ");
2117 unit_write_settingf(u
, flags
, name
, "%s=%s%s", name
, is_allow_list
? "" : "~", joined
);
2123 if (streq(name
, "DisableControllers") || (u
->transient
&& u
->load_state
== UNIT_STUB
))
2124 return bus_cgroup_set_transient_property(u
, c
, name
, message
, flags
, error
);