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 "memstream-util.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "percent-util.h"
25 #include "socket-util.h"
27 BUS_DEFINE_PROPERTY_GET(bus_property_get_tasks_max
, "t", TasksMax
, tasks_max_resolve
);
28 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_cgroup_pressure_watch
, cgroup_pressure_watch
, CGroupPressureWatch
);
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_cgroup_device_policy
, cgroup_device_policy
, CGroupDevicePolicy
);
31 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_mode
, managed_oom_mode
, ManagedOOMMode
);
32 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_preference
, managed_oom_preference
, ManagedOOMPreference
);
34 static int property_get_cgroup_mask(
37 const char *interface
,
39 sd_bus_message
*reply
,
41 sd_bus_error
*error
) {
43 CGroupMask
*mask
= userdata
;
49 r
= sd_bus_message_open_container(reply
, 'a', "s");
53 for (CGroupController ctrl
= 0; ctrl
< _CGROUP_CONTROLLER_MAX
; ctrl
++) {
54 if ((*mask
& CGROUP_CONTROLLER_TO_MASK(ctrl
)) == 0)
57 r
= sd_bus_message_append(reply
, "s", cgroup_controller_to_string(ctrl
));
62 return sd_bus_message_close_container(reply
);
65 static int property_get_delegate_controllers(
68 const char *interface
,
70 sd_bus_message
*reply
,
72 sd_bus_error
*error
) {
74 CGroupContext
*c
= ASSERT_PTR(userdata
);
80 return sd_bus_message_append(reply
, "as", 0);
82 return property_get_cgroup_mask(bus
, path
, interface
, property
, reply
, &c
->delegate_controllers
, error
);
85 static int property_get_cpuset(
88 const char *interface
,
90 sd_bus_message
*reply
,
92 sd_bus_error
*error
) {
94 CPUSet
*cpus
= ASSERT_PTR(userdata
);
95 _cleanup_free_
uint8_t *array
= NULL
;
101 (void) cpu_set_to_dbus(cpus
, &array
, &allocated
);
102 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
105 static int property_get_io_device_weight(
108 const char *interface
,
109 const char *property
,
110 sd_bus_message
*reply
,
112 sd_bus_error
*error
) {
114 CGroupContext
*c
= ASSERT_PTR(userdata
);
120 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
124 LIST_FOREACH(device_weights
, w
, c
->io_device_weights
) {
125 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
130 return sd_bus_message_close_container(reply
);
133 static int property_get_io_device_limits(
136 const char *interface
,
137 const char *property
,
138 sd_bus_message
*reply
,
140 sd_bus_error
*error
) {
142 CGroupContext
*c
= ASSERT_PTR(userdata
);
148 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
152 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
) {
153 CGroupIOLimitType type
;
155 type
= cgroup_io_limit_type_from_string(property
);
156 if (type
< 0 || l
->limits
[type
] == cgroup_io_limit_defaults
[type
])
159 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->limits
[type
]);
164 return sd_bus_message_close_container(reply
);
167 static int property_get_io_device_latency(
170 const char *interface
,
171 const char *property
,
172 sd_bus_message
*reply
,
174 sd_bus_error
*error
) {
176 CGroupContext
*c
= ASSERT_PTR(userdata
);
182 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
186 LIST_FOREACH(device_latencies
, l
, c
->io_device_latencies
) {
187 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->target_usec
);
192 return sd_bus_message_close_container(reply
);
195 static int property_get_blockio_device_weight(
198 const char *interface
,
199 const char *property
,
200 sd_bus_message
*reply
,
202 sd_bus_error
*error
) {
204 CGroupContext
*c
= ASSERT_PTR(userdata
);
210 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
214 LIST_FOREACH(device_weights
, w
, c
->blockio_device_weights
) {
215 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
220 return sd_bus_message_close_container(reply
);
223 static int property_get_blockio_device_bandwidths(
226 const char *interface
,
227 const char *property
,
228 sd_bus_message
*reply
,
230 sd_bus_error
*error
) {
232 CGroupContext
*c
= ASSERT_PTR(userdata
);
238 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
242 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
245 if (streq(property
, "BlockIOReadBandwidth"))
250 if (v
== CGROUP_LIMIT_MAX
)
253 r
= sd_bus_message_append(reply
, "(st)", b
->path
, v
);
258 return sd_bus_message_close_container(reply
);
261 static int property_get_device_allow(
264 const char *interface
,
265 const char *property
,
266 sd_bus_message
*reply
,
268 sd_bus_error
*error
) {
270 CGroupContext
*c
= ASSERT_PTR(userdata
);
276 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
280 LIST_FOREACH(device_allow
, a
, c
->device_allow
) {
293 r
= sd_bus_message_append(reply
, "(ss)", a
->path
, rwm
);
298 return sd_bus_message_close_container(reply
);
301 static int property_get_ip_address_access(
304 const char *interface
,
305 const char *property
,
306 sd_bus_message
*reply
,
308 sd_bus_error
*error
) {
310 Set
**prefixes
= ASSERT_PTR(userdata
);
311 struct in_addr_prefix
*i
;
314 r
= sd_bus_message_open_container(reply
, 'a', "(iayu)");
318 SET_FOREACH(i
, *prefixes
) {
320 r
= sd_bus_message_open_container(reply
, 'r', "iayu");
324 r
= sd_bus_message_append(reply
, "i", i
->family
);
328 r
= sd_bus_message_append_array(reply
, 'y', &i
->address
, FAMILY_ADDRESS_SIZE(i
->family
));
332 r
= sd_bus_message_append(reply
, "u", (uint32_t) i
->prefixlen
);
336 r
= sd_bus_message_close_container(reply
);
341 return sd_bus_message_close_container(reply
);
344 static int property_get_bpf_foreign_program(
347 const char *interface
,
348 const char *property
,
349 sd_bus_message
*reply
,
351 sd_bus_error
*error
) {
352 CGroupContext
*c
= userdata
;
355 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
359 LIST_FOREACH(programs
, p
, c
->bpf_foreign_programs
) {
360 const char *attach_type
= bpf_cgroup_attach_type_to_string(p
->attach_type
);
362 r
= sd_bus_message_append(reply
, "(ss)", attach_type
, p
->bpffs_path
);
367 return sd_bus_message_close_container(reply
);
370 static int property_get_socket_bind(
373 const char *interface
,
374 const char *property
,
375 sd_bus_message
*reply
,
377 sd_bus_error
*error
) {
379 CGroupSocketBindItem
**items
= ASSERT_PTR(userdata
);
382 r
= sd_bus_message_open_container(reply
, 'a', "(iiqq)");
386 LIST_FOREACH(socket_bind_items
, i
, *items
) {
387 r
= sd_bus_message_append(reply
, "(iiqq)", i
->address_family
, i
->ip_protocol
, i
->nr_ports
, i
->port_min
);
392 return sd_bus_message_close_container(reply
);
395 static int property_get_restrict_network_interfaces(
398 const char *interface
,
399 const char *property
,
400 sd_bus_message
*reply
,
402 sd_bus_error
*error
) {
404 CGroupContext
*c
= ASSERT_PTR(userdata
);
410 r
= sd_bus_message_open_container(reply
, 'r', "bas");
414 r
= sd_bus_message_append(reply
, "b", c
->restrict_network_interfaces_is_allow_list
);
418 r
= sd_bus_message_open_container(reply
, 'a', "s");
422 SET_FOREACH(iface
, c
->restrict_network_interfaces
) {
423 r
= sd_bus_message_append(reply
, "s", iface
);
428 r
= sd_bus_message_close_container(reply
);
432 return sd_bus_message_close_container(reply
);
435 const sd_bus_vtable bus_cgroup_vtable
[] = {
436 SD_BUS_VTABLE_START(0),
437 SD_BUS_PROPERTY("Delegate", "b", bus_property_get_bool
, offsetof(CGroupContext
, delegate
), 0),
438 SD_BUS_PROPERTY("DelegateControllers", "as", property_get_delegate_controllers
, 0, 0),
439 SD_BUS_PROPERTY("DelegateSubgroup", "s", NULL
, offsetof(CGroupContext
, delegate_subgroup
), 0),
440 SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, cpu_accounting
), 0),
441 SD_BUS_PROPERTY("CPUWeight", "t", NULL
, offsetof(CGroupContext
, cpu_weight
), 0),
442 SD_BUS_PROPERTY("StartupCPUWeight", "t", NULL
, offsetof(CGroupContext
, startup_cpu_weight
), 0),
443 SD_BUS_PROPERTY("CPUShares", "t", NULL
, offsetof(CGroupContext
, cpu_shares
), 0),
444 SD_BUS_PROPERTY("StartupCPUShares", "t", NULL
, offsetof(CGroupContext
, startup_cpu_shares
), 0),
445 SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_per_sec_usec
), 0),
446 SD_BUS_PROPERTY("CPUQuotaPeriodUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_period_usec
), 0),
447 SD_BUS_PROPERTY("AllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_cpus
), 0),
448 SD_BUS_PROPERTY("StartupAllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_cpus
), 0),
449 SD_BUS_PROPERTY("AllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_mems
), 0),
450 SD_BUS_PROPERTY("StartupAllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_mems
), 0),
451 SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, io_accounting
), 0),
452 SD_BUS_PROPERTY("IOWeight", "t", NULL
, offsetof(CGroupContext
, io_weight
), 0),
453 SD_BUS_PROPERTY("StartupIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_io_weight
), 0),
454 SD_BUS_PROPERTY("IODeviceWeight", "a(st)", property_get_io_device_weight
, 0, 0),
455 SD_BUS_PROPERTY("IOReadBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
456 SD_BUS_PROPERTY("IOWriteBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
457 SD_BUS_PROPERTY("IOReadIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
458 SD_BUS_PROPERTY("IOWriteIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
459 SD_BUS_PROPERTY("IODeviceLatencyTargetUSec", "a(st)", property_get_io_device_latency
, 0, 0),
460 SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, blockio_accounting
), 0),
461 SD_BUS_PROPERTY("BlockIOWeight", "t", NULL
, offsetof(CGroupContext
, blockio_weight
), 0),
462 SD_BUS_PROPERTY("StartupBlockIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_blockio_weight
), 0),
463 SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight
, 0, 0),
464 SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
465 SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
466 SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, memory_accounting
), 0),
467 SD_BUS_PROPERTY("DefaultMemoryLow", "t", NULL
, offsetof(CGroupContext
, default_memory_low
), 0),
468 SD_BUS_PROPERTY("DefaultStartupMemoryLow", "t", NULL
, offsetof(CGroupContext
, default_startup_memory_low
), 0),
469 SD_BUS_PROPERTY("DefaultMemoryMin", "t", NULL
, offsetof(CGroupContext
, default_memory_min
), 0),
470 SD_BUS_PROPERTY("MemoryMin", "t", NULL
, offsetof(CGroupContext
, memory_min
), 0),
471 SD_BUS_PROPERTY("MemoryLow", "t", NULL
, offsetof(CGroupContext
, memory_low
), 0),
472 SD_BUS_PROPERTY("StartupMemoryLow", "t", NULL
, offsetof(CGroupContext
, startup_memory_low
), 0),
473 SD_BUS_PROPERTY("MemoryHigh", "t", NULL
, offsetof(CGroupContext
, memory_high
), 0),
474 SD_BUS_PROPERTY("StartupMemoryHigh", "t", NULL
, offsetof(CGroupContext
, startup_memory_high
), 0),
475 SD_BUS_PROPERTY("MemoryMax", "t", NULL
, offsetof(CGroupContext
, memory_max
), 0),
476 SD_BUS_PROPERTY("StartupMemoryMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_max
), 0),
477 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL
, offsetof(CGroupContext
, memory_swap_max
), 0),
478 SD_BUS_PROPERTY("StartupMemorySwapMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_swap_max
), 0),
479 SD_BUS_PROPERTY("MemoryZSwapMax", "t", NULL
, offsetof(CGroupContext
, memory_zswap_max
), 0),
480 SD_BUS_PROPERTY("StartupMemoryZSwapMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_zswap_max
), 0),
481 SD_BUS_PROPERTY("MemoryLimit", "t", NULL
, offsetof(CGroupContext
, memory_limit
), 0),
482 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy
, offsetof(CGroupContext
, device_policy
), 0),
483 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow
, 0, 0),
484 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, tasks_accounting
), 0),
485 SD_BUS_PROPERTY("TasksMax", "t", bus_property_get_tasks_max
, offsetof(CGroupContext
, tasks_max
), 0),
486 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, ip_accounting
), 0),
487 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_allow
), 0),
488 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_deny
), 0),
489 SD_BUS_PROPERTY("IPIngressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_ingress
), 0),
490 SD_BUS_PROPERTY("IPEgressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_egress
), 0),
491 SD_BUS_PROPERTY("DisableControllers", "as", property_get_cgroup_mask
, offsetof(CGroupContext
, disable_controllers
), 0),
492 SD_BUS_PROPERTY("ManagedOOMSwap", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_swap
), 0),
493 SD_BUS_PROPERTY("ManagedOOMMemoryPressure", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_mem_pressure
), 0),
494 SD_BUS_PROPERTY("ManagedOOMMemoryPressureLimit", "u", NULL
, offsetof(CGroupContext
, moom_mem_pressure_limit
), 0),
495 SD_BUS_PROPERTY("ManagedOOMPreference", "s", property_get_managed_oom_preference
, offsetof(CGroupContext
, moom_preference
), 0),
496 SD_BUS_PROPERTY("BPFProgram", "a(ss)", property_get_bpf_foreign_program
, 0, 0),
497 SD_BUS_PROPERTY("SocketBindAllow", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_allow
), 0),
498 SD_BUS_PROPERTY("SocketBindDeny", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_deny
), 0),
499 SD_BUS_PROPERTY("RestrictNetworkInterfaces", "(bas)", property_get_restrict_network_interfaces
, 0, 0),
500 SD_BUS_PROPERTY("MemoryPressureWatch", "s", bus_property_get_cgroup_pressure_watch
, offsetof(CGroupContext
, memory_pressure_watch
), 0),
501 SD_BUS_PROPERTY("MemoryPressureThresholdUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, memory_pressure_threshold_usec
), 0),
505 static int bus_cgroup_set_transient_property(
509 sd_bus_message
*message
,
510 UnitWriteFlags flags
,
511 sd_bus_error
*error
) {
520 flags
|= UNIT_PRIVATE
;
522 if (streq(name
, "Delegate")) {
525 if (!UNIT_VTABLE(u
)->can_delegate
)
526 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
528 r
= sd_bus_message_read(message
, "b", &b
);
532 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
534 c
->delegate_controllers
= b
? _CGROUP_MASK_ALL
: 0;
536 unit_write_settingf(u
, flags
, name
, "Delegate=%s", yes_no(b
));
541 } else if (streq(name
, "DelegateSubgroup")) {
544 if (!UNIT_VTABLE(u
)->can_delegate
)
545 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
547 r
= sd_bus_message_read(message
, "s", &s
);
551 if (!isempty(s
) && cg_needs_escape(s
))
552 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid control group name: %s", s
);
554 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
556 c
->delegate_subgroup
= mfree(c
->delegate_subgroup
);
558 r
= free_and_strdup_warn(&c
->delegate_subgroup
, s
);
563 unit_write_settingf(u
, flags
, name
, "DelegateSubgroup=%s", s
);
568 } else if (STR_IN_SET(name
, "DelegateControllers", "DisableControllers")) {
571 if (streq(name
, "DelegateControllers") && !UNIT_VTABLE(u
)->can_delegate
)
572 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
574 r
= sd_bus_message_enter_container(message
, 'a', "s");
582 r
= sd_bus_message_read(message
, "s", &t
);
588 cc
= cgroup_controller_from_string(t
);
590 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown cgroup controller '%s'", t
);
592 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
595 r
= sd_bus_message_exit_container(message
);
599 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
600 _cleanup_free_
char *t
= NULL
;
602 r
= cg_mask_to_string(mask
, &t
);
606 if (streq(name
, "DelegateControllers")) {
610 c
->delegate_controllers
= 0;
612 c
->delegate_controllers
|= mask
;
614 unit_write_settingf(u
, flags
, name
, "Delegate=%s", strempty(t
));
616 } else if (streq(name
, "DisableControllers")) {
619 c
->disable_controllers
= 0;
621 c
->disable_controllers
|= mask
;
623 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, strempty(t
));
628 } else if (STR_IN_SET(name
, "IPIngressFilterPath", "IPEgressFilterPath")) {
632 filters
= streq(name
, "IPIngressFilterPath") ? &c
->ip_filters_ingress
: &c
->ip_filters_egress
;
633 r
= sd_bus_message_enter_container(message
, 'a', "s");
640 r
= sd_bus_message_read(message
, "s", &path
);
646 if (!path_is_normalized(path
) || !path_is_absolute(path
))
647 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects a normalized absolute path.", name
);
649 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && !strv_contains(*filters
, path
)) {
650 r
= strv_extend(filters
, path
);
656 r
= sd_bus_message_exit_container(message
);
660 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
661 _cleanup_(memstream_done
) MemStream m
= {};
662 _cleanup_free_
char *buf
= NULL
;
666 *filters
= strv_free(*filters
);
668 unit_invalidate_cgroup_bpf(u
);
670 f
= memstream_init(&m
);
677 STRV_FOREACH(entry
, *filters
)
678 fprintf(f
, "%s=%s\n", name
, *entry
);
680 r
= memstream_finalize(&m
, &buf
, NULL
);
684 unit_write_setting(u
, flags
, name
, buf
);
687 r
= bpf_firewall_supported();
690 if (r
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
) {
691 static bool warned
= false;
693 log_full(warned
? LOG_DEBUG
: LOG_WARNING
,
694 "Transient unit %s configures an IP firewall with BPF, but the local system does not support BPF/cgroup firewalling with multiple filters.\n"
695 "Starting this unit will fail! (This warning is only shown for the first started transient unit using IP firewalling.)", u
->id
);
702 } else if (streq(name
, "BPFProgram")) {
706 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
710 while ((r
= sd_bus_message_read(message
, "(ss)", &a
, &p
)) > 0) {
711 int attach_type
= bpf_cgroup_attach_type_from_string(a
);
713 return sd_bus_error_setf(
715 SD_BUS_ERROR_INVALID_ARGS
,
716 "%s expects a valid BPF attach type, got '%s'.",
719 if (!path_is_normalized(p
) || !path_is_absolute(p
))
720 return sd_bus_error_setf(
722 SD_BUS_ERROR_INVALID_ARGS
,
723 "%s= expects a normalized absolute path.",
726 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
727 r
= cgroup_add_bpf_foreign_program(c
, attach_type
, p
);
736 r
= sd_bus_message_exit_container(message
);
740 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
741 _cleanup_(memstream_done
) MemStream m
= {};
742 _cleanup_free_
char *buf
= NULL
;
746 while (c
->bpf_foreign_programs
)
747 cgroup_context_remove_bpf_foreign_program(c
, c
->bpf_foreign_programs
);
749 f
= memstream_init(&m
);
756 LIST_FOREACH(programs
, fp
, c
->bpf_foreign_programs
)
757 fprintf(f
, "%s=%s:%s\n", name
,
758 bpf_cgroup_attach_type_to_string(fp
->attach_type
),
761 r
= memstream_finalize(&m
, &buf
, NULL
);
765 unit_write_setting(u
, flags
, name
, buf
);
767 if (c
->bpf_foreign_programs
) {
768 r
= bpf_foreign_supported();
773 "Transient unit %s configures a BPF program pinned to BPF "
774 "filesystem, but the local system does not support that.\n"
775 "Starting this unit will fail!", u
->id
);
781 } else if (streq(name
, "MemoryPressureWatch")) {
782 CGroupPressureWatch p
;
785 r
= sd_bus_message_read(message
, "s", &t
);
790 p
= _CGROUP_PRESSURE_WATCH_INVALID
;
792 p
= cgroup_pressure_watch_from_string(t
);
797 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
798 c
->memory_pressure_watch
= p
;
799 unit_write_settingf(u
, flags
, name
, "MemoryPressureWatch=%s", strempty(cgroup_pressure_watch_to_string(p
)));
804 } else if (streq(name
, "MemoryPressureThresholdUSec")) {
807 r
= sd_bus_message_read(message
, "t", &t
);
811 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
812 c
->memory_pressure_threshold_usec
= t
;
815 unit_write_setting(u
, flags
, name
, "MemoryPressureThresholdUSec=");
817 unit_write_settingf(u
, flags
, name
, "MemoryPressureThresholdUSec=%" PRIu64
, t
);
826 static int bus_cgroup_set_boolean(
831 sd_bus_message
*message
,
832 UnitWriteFlags flags
,
833 sd_bus_error
*error
) {
839 r
= sd_bus_message_read(message
, "b", &b
);
843 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
845 unit_invalidate_cgroup(u
, mask
);
846 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, yes_no(b
));
852 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
853 static int bus_cgroup_set_##function( \
857 sd_bus_message *message, \
858 UnitWriteFlags flags, \
859 sd_bus_error *error) { \
866 r = sd_bus_message_read(message, "t", &v); \
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); \
879 unit_write_settingf(u, flags, name, \
882 unit_write_settingf(u, flags, name, \
883 "%s=%" PRIu64, name, v); \
889 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
890 static int bus_cgroup_set_##function( \
894 sd_bus_message *message, \
895 UnitWriteFlags flags, \
896 sd_bus_error *error) { \
903 r = sd_bus_message_read(message, "t", &v); \
908 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
909 "Value specified in %s is out of range", name); \
911 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
913 unit_invalidate_cgroup(u, mask); \
915 if (v == CGROUP_LIMIT_MAX) \
916 unit_write_settingf(u, flags, name, \
917 "%s=infinity", name); \
919 unit_write_settingf(u, flags, name, \
920 "%s=%" PRIu64, name, v); \
925 static int bus_cgroup_set_##function##_scale( \
929 sd_bus_message *message, \
930 UnitWriteFlags flags, \
931 sd_bus_error *error) { \
939 r = sd_bus_message_read(message, "u", &raw); \
943 v = scale(raw, UINT32_MAX); \
944 if (v < minimum || v >= UINT64_MAX) \
945 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
946 "Value specified in %s is out of range", name); \
948 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
950 unit_invalidate_cgroup(u, mask); \
952 /* Prepare to chop off suffix */ \
953 assert_se(endswith(name, "Scale")); \
955 int scaled = UINT32_SCALE_TO_PERMYRIAD(raw); \
956 unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR, \
957 (int)(strlen(name) - strlen("Scale")), name, \
958 PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled)); \
964 DISABLE_WARNING_TYPE_LIMITS
;
965 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares
, CGROUP_MASK_CPU
, CGROUP_CPU_SHARES_IS_OK
, CGROUP_CPU_SHARES_INVALID
);
966 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight
, CGROUP_MASK_IO
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
967 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight
, CGROUP_MASK_BLKIO
, CGROUP_BLKIO_WEIGHT_IS_OK
, CGROUP_BLKIO_WEIGHT_INVALID
);
968 BUS_DEFINE_SET_CGROUP_LIMIT(memory
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 1);
969 BUS_DEFINE_SET_CGROUP_LIMIT(memory_protection
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
970 BUS_DEFINE_SET_CGROUP_LIMIT(swap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
971 BUS_DEFINE_SET_CGROUP_LIMIT(zswap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
974 static int bus_cgroup_set_cpu_weight(
978 sd_bus_message
*message
,
979 UnitWriteFlags flags
,
980 sd_bus_error
*error
) {
984 r
= sd_bus_message_read(message
, "t", &v
);
987 if (!CGROUP_WEIGHT_IS_OK(v
) && v
!= CGROUP_WEIGHT_IDLE
)
988 return sd_bus_error_setf(
989 error
, SD_BUS_ERROR_INVALID_ARGS
, "Value specified in %s is out of range", name
);
990 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
992 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
993 if (v
== CGROUP_WEIGHT_INVALID
)
994 unit_write_settingf(u
, flags
, name
, "%s=", name
);
995 else if (v
== CGROUP_WEIGHT_IDLE
)
996 unit_write_settingf(u
, flags
, name
, "%s=idle", name
);
998 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu64
, name
, v
);
1003 static int bus_cgroup_set_tasks_max(
1007 sd_bus_message
*message
,
1008 UnitWriteFlags flags
,
1009 sd_bus_error
*error
) {
1016 r
= sd_bus_message_read(message
, "t", &v
);
1021 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1022 "Value specified in %s is out of range", name
);
1024 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1025 *p
= (TasksMax
) { .value
= v
, .scale
= 0 }; /* When .scale==0, .value is the absolute value */
1026 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
1028 if (v
== CGROUP_LIMIT_MAX
)
1029 unit_write_settingf(u
, flags
, name
,
1030 "%s=infinity", name
);
1032 unit_write_settingf(u
, flags
, name
,
1033 "%s=%" PRIu64
, name
, v
);
1039 static int bus_cgroup_set_tasks_max_scale(
1043 sd_bus_message
*message
,
1044 UnitWriteFlags flags
,
1045 sd_bus_error
*error
) {
1052 r
= sd_bus_message_read(message
, "u", &v
);
1056 if (v
< 1 || v
>= UINT32_MAX
)
1057 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1058 "Value specified in %s is out of range", name
);
1060 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1061 *p
= (TasksMax
) { v
, UINT32_MAX
}; /* .scale is not 0, so this is interpreted as v/UINT32_MAX. */
1062 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
1064 uint32_t scaled
= DIV_ROUND_UP((uint64_t) v
* 100U, (uint64_t) UINT32_MAX
);
1065 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu32
".%" PRIu32
"%%", "TasksMax",
1066 scaled
/ 10, scaled
% 10);
1072 int bus_cgroup_set_property(
1076 sd_bus_message
*message
,
1077 UnitWriteFlags flags
,
1078 sd_bus_error
*error
) {
1080 CGroupIOLimitType iol_type
;
1088 flags
|= UNIT_PRIVATE
;
1090 if (streq(name
, "CPUAccounting"))
1091 return bus_cgroup_set_boolean(u
, name
, &c
->cpu_accounting
, get_cpu_accounting_mask(), message
, flags
, error
);
1093 if (streq(name
, "CPUWeight"))
1094 return bus_cgroup_set_cpu_weight(u
, name
, &c
->cpu_weight
, message
, flags
, error
);
1096 if (streq(name
, "StartupCPUWeight"))
1097 return bus_cgroup_set_cpu_weight(u
, name
, &c
->startup_cpu_weight
, message
, flags
, error
);
1099 if (streq(name
, "CPUShares"))
1100 return bus_cgroup_set_cpu_shares(u
, name
, &c
->cpu_shares
, message
, flags
, error
);
1102 if (streq(name
, "StartupCPUShares"))
1103 return bus_cgroup_set_cpu_shares(u
, name
, &c
->startup_cpu_shares
, message
, flags
, error
);
1105 if (streq(name
, "IOAccounting"))
1106 return bus_cgroup_set_boolean(u
, name
, &c
->io_accounting
, CGROUP_MASK_IO
, message
, flags
, error
);
1108 if (streq(name
, "IOWeight"))
1109 return bus_cgroup_set_io_weight(u
, name
, &c
->io_weight
, message
, flags
, error
);
1111 if (streq(name
, "StartupIOWeight"))
1112 return bus_cgroup_set_io_weight(u
, name
, &c
->startup_io_weight
, message
, flags
, error
);
1114 if (streq(name
, "BlockIOAccounting"))
1115 return bus_cgroup_set_boolean(u
, name
, &c
->blockio_accounting
, CGROUP_MASK_BLKIO
, message
, flags
, error
);
1117 if (streq(name
, "BlockIOWeight"))
1118 return bus_cgroup_set_blockio_weight(u
, name
, &c
->blockio_weight
, message
, flags
, error
);
1120 if (streq(name
, "StartupBlockIOWeight"))
1121 return bus_cgroup_set_blockio_weight(u
, name
, &c
->startup_blockio_weight
, message
, flags
, error
);
1123 if (streq(name
, "MemoryAccounting"))
1124 return bus_cgroup_set_boolean(u
, name
, &c
->memory_accounting
, CGROUP_MASK_MEMORY
, message
, flags
, error
);
1126 if (streq(name
, "MemoryMin")) {
1127 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_min
, message
, flags
, error
);
1129 c
->memory_min_set
= true;
1133 if (streq(name
, "MemoryLow")) {
1134 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_low
, message
, flags
, error
);
1136 c
->memory_low_set
= true;
1140 if (streq(name
, "StartupMemoryLow")) {
1141 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->startup_memory_low
, message
, flags
, error
);
1143 c
->startup_memory_low_set
= true;
1147 if (streq(name
, "DefaultMemoryMin")) {
1148 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1150 c
->default_memory_min_set
= true;
1154 if (streq(name
, "DefaultMemoryLow")) {
1155 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1157 c
->default_memory_low_set
= true;
1161 if (streq(name
, "DefaultStartupMemoryLow")) {
1162 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_startup_memory_low
, message
, flags
, error
);
1164 c
->default_startup_memory_low_set
= true;
1168 if (streq(name
, "MemoryHigh"))
1169 return bus_cgroup_set_memory(u
, name
, &c
->memory_high
, message
, flags
, error
);
1171 if (streq(name
, "StartupMemoryHigh")) {
1172 r
= bus_cgroup_set_memory(u
, name
, &c
->startup_memory_high
, message
, flags
, error
);
1174 c
->startup_memory_high_set
= true;
1178 if (streq(name
, "MemorySwapMax"))
1179 return bus_cgroup_set_swap(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1181 if (streq(name
, "StartupMemorySwapMax")) {
1182 r
= bus_cgroup_set_swap(u
, name
, &c
->startup_memory_swap_max
, message
, flags
, error
);
1184 c
->startup_memory_swap_max_set
= true;
1188 if (streq(name
, "MemoryZSwapMax"))
1189 return bus_cgroup_set_zswap(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1191 if (streq(name
, "StartupMemoryZSwapMax")) {
1192 r
= bus_cgroup_set_zswap(u
, name
, &c
->startup_memory_zswap_max
, message
, flags
, error
);
1194 c
->startup_memory_zswap_max_set
= true;
1198 if (streq(name
, "MemoryMax"))
1199 return bus_cgroup_set_memory(u
, name
, &c
->memory_max
, message
, flags
, error
);
1201 if (streq(name
, "StartupMemoryMax")) {
1202 r
= bus_cgroup_set_memory(u
, name
, &c
->startup_memory_max
, message
, flags
, error
);
1204 c
->startup_memory_max_set
= true;
1208 if (streq(name
, "MemoryLimit"))
1209 return bus_cgroup_set_memory(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1211 if (streq(name
, "MemoryMinScale")) {
1212 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_min
, message
, flags
, error
);
1214 c
->memory_min_set
= true;
1218 if (streq(name
, "MemoryLowScale")) {
1219 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_low
, message
, flags
, error
);
1221 c
->memory_low_set
= true;
1225 if (streq(name
, "DefaultMemoryMinScale")) {
1226 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1228 c
->default_memory_min_set
= true;
1232 if (streq(name
, "DefaultMemoryLowScale")) {
1233 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1235 c
->default_memory_low_set
= true;
1239 if (streq(name
, "MemoryHighScale"))
1240 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_high
, message
, flags
, error
);
1242 if (streq(name
, "MemorySwapMaxScale"))
1243 return bus_cgroup_set_swap_scale(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1245 if (streq(name
, "MemoryZSwapMaxScale"))
1246 return bus_cgroup_set_zswap_scale(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1248 if (streq(name
, "MemoryMaxScale"))
1249 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_max
, message
, flags
, error
);
1251 if (streq(name
, "MemoryLimitScale"))
1252 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1254 if (streq(name
, "TasksAccounting"))
1255 return bus_cgroup_set_boolean(u
, name
, &c
->tasks_accounting
, CGROUP_MASK_PIDS
, message
, flags
, error
);
1257 if (streq(name
, "TasksMax"))
1258 return bus_cgroup_set_tasks_max(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1260 if (streq(name
, "TasksMaxScale"))
1261 return bus_cgroup_set_tasks_max_scale(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1263 if (streq(name
, "CPUQuotaPerSecUSec")) {
1266 r
= sd_bus_message_read(message
, "t", &u64
);
1271 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "CPUQuotaPerSecUSec= value out of range");
1273 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1274 c
->cpu_quota_per_sec_usec
= u64
;
1275 u
->warned_clamping_cpu_quota_period
= false;
1276 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1278 if (c
->cpu_quota_per_sec_usec
== USEC_INFINITY
)
1279 unit_write_setting(u
, flags
, "CPUQuota", "CPUQuota=");
1281 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
1283 unit_write_settingf(u
, flags
, "CPUQuota",
1285 (double) (c
->cpu_quota_per_sec_usec
/ 10000));
1290 } else if (streq(name
, "CPUQuotaPeriodUSec")) {
1293 r
= sd_bus_message_read(message
, "t", &u64
);
1297 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1298 c
->cpu_quota_period_usec
= u64
;
1299 u
->warned_clamping_cpu_quota_period
= false;
1300 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1301 if (c
->cpu_quota_period_usec
== USEC_INFINITY
)
1302 unit_write_setting(u
, flags
, "CPUQuotaPeriodSec", "CPUQuotaPeriodSec=");
1304 unit_write_settingf(u
, flags
, "CPUQuotaPeriodSec",
1305 "CPUQuotaPeriodSec=%s",
1306 FORMAT_TIMESPAN(c
->cpu_quota_period_usec
, 1));
1311 } else if (STR_IN_SET(name
, "AllowedCPUs", "StartupAllowedCPUs", "AllowedMemoryNodes", "StartupAllowedMemoryNodes")) {
1314 _cleanup_(cpu_set_reset
) CPUSet new_set
= {};
1316 r
= sd_bus_message_read_array(message
, 'y', &a
, &n
);
1320 r
= cpu_set_from_dbus(a
, n
, &new_set
);
1324 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1325 _cleanup_free_
char *setstr
= NULL
;
1328 setstr
= cpu_set_to_range_string(&new_set
);
1332 if (streq(name
, "AllowedCPUs"))
1333 set
= &c
->cpuset_cpus
;
1334 else if (streq(name
, "StartupAllowedCPUs"))
1335 set
= &c
->startup_cpuset_cpus
;
1336 else if (streq(name
, "AllowedMemoryNodes"))
1337 set
= &c
->cpuset_mems
;
1338 else if (streq(name
, "StartupAllowedMemoryNodes"))
1339 set
= &c
->startup_cpuset_mems
;
1345 new_set
= (CPUSet
) {};
1347 unit_invalidate_cgroup(u
, CGROUP_MASK_CPUSET
);
1348 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, setstr
);
1353 } else if ((iol_type
= cgroup_io_limit_type_from_string(name
)) >= 0) {
1358 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1362 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1364 if (!path_is_normalized(path
))
1365 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1367 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1368 CGroupIODeviceLimit
*a
= NULL
;
1370 LIST_FOREACH(device_limits
, b
, c
->io_device_limits
)
1371 if (path_equal(path
, b
->path
)) {
1377 CGroupIOLimitType type
;
1379 a
= new0(CGroupIODeviceLimit
, 1);
1383 a
->path
= strdup(path
);
1389 for (type
= 0; type
< _CGROUP_IO_LIMIT_TYPE_MAX
; type
++)
1390 a
->limits
[type
] = cgroup_io_limit_defaults
[type
];
1392 LIST_PREPEND(device_limits
, c
->io_device_limits
, a
);
1395 a
->limits
[iol_type
] = u64
;
1403 r
= sd_bus_message_exit_container(message
);
1407 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1408 _cleanup_(memstream_done
) MemStream m
= {};
1409 _cleanup_free_
char *buf
= NULL
;
1413 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1414 a
->limits
[iol_type
] = cgroup_io_limit_defaults
[iol_type
];
1416 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1418 f
= memstream_init(&m
);
1422 fprintf(f
, "%s=\n", name
);
1423 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1424 if (a
->limits
[iol_type
] != cgroup_io_limit_defaults
[iol_type
])
1425 fprintf(f
, "%s=%s %" PRIu64
"\n", name
, a
->path
, a
->limits
[iol_type
]);
1427 r
= memstream_finalize(&m
, &buf
, NULL
);
1431 unit_write_setting(u
, flags
, name
, buf
);
1436 } else if (streq(name
, "IODeviceWeight")) {
1441 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1445 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1447 if (!path_is_normalized(path
))
1448 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1450 if (!CGROUP_WEIGHT_IS_OK(weight
) || weight
== CGROUP_WEIGHT_INVALID
)
1451 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "IODeviceWeight= value out of range");
1453 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1454 CGroupIODeviceWeight
*a
= NULL
;
1456 LIST_FOREACH(device_weights
, b
, c
->io_device_weights
)
1457 if (path_equal(b
->path
, path
)) {
1463 a
= new0(CGroupIODeviceWeight
, 1);
1467 a
->path
= strdup(path
);
1472 LIST_PREPEND(device_weights
, c
->io_device_weights
, a
);
1481 r
= sd_bus_message_exit_container(message
);
1485 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1486 _cleanup_(memstream_done
) MemStream m
= {};
1487 _cleanup_free_
char *buf
= NULL
;
1491 while (c
->io_device_weights
)
1492 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
1494 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1496 f
= memstream_init(&m
);
1500 fputs("IODeviceWeight=\n", f
);
1501 LIST_FOREACH(device_weights
, a
, c
->io_device_weights
)
1502 fprintf(f
, "IODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1504 r
= memstream_finalize(&m
, &buf
, NULL
);
1508 unit_write_setting(u
, flags
, name
, buf
);
1513 } else if (streq(name
, "IODeviceLatencyTargetUSec")) {
1518 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1522 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &target
)) > 0) {
1524 if (!path_is_normalized(path
))
1525 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1527 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1528 CGroupIODeviceLatency
*a
= NULL
;
1530 LIST_FOREACH(device_latencies
, b
, c
->io_device_latencies
)
1531 if (path_equal(b
->path
, path
)) {
1537 a
= new0(CGroupIODeviceLatency
, 1);
1541 a
->path
= strdup(path
);
1546 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, a
);
1549 a
->target_usec
= target
;
1555 r
= sd_bus_message_exit_container(message
);
1559 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1560 _cleanup_(memstream_done
) MemStream m
= {};
1561 _cleanup_free_
char *buf
= NULL
;
1565 while (c
->io_device_latencies
)
1566 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
1568 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1570 f
= memstream_init(&m
);
1574 fputs("IODeviceLatencyTargetSec=\n", f
);
1575 LIST_FOREACH(device_latencies
, a
, c
->io_device_latencies
)
1576 fprintf(f
, "IODeviceLatencyTargetSec=%s %s\n",
1577 a
->path
, FORMAT_TIMESPAN(a
->target_usec
, 1));
1579 r
= memstream_finalize(&m
, &buf
, NULL
);
1583 unit_write_setting(u
, flags
, name
, buf
);
1588 } else if (STR_IN_SET(name
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
1594 read
= streq(name
, "BlockIOReadBandwidth");
1596 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1600 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1602 if (!path_is_normalized(path
))
1603 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1605 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1606 CGroupBlockIODeviceBandwidth
*a
= NULL
;
1608 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
)
1609 if (path_equal(path
, b
->path
)) {
1615 a
= new0(CGroupBlockIODeviceBandwidth
, 1);
1619 a
->rbps
= CGROUP_LIMIT_MAX
;
1620 a
->wbps
= CGROUP_LIMIT_MAX
;
1621 a
->path
= strdup(path
);
1627 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, a
);
1641 r
= sd_bus_message_exit_container(message
);
1645 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1646 _cleanup_(memstream_done
) MemStream m
= {};
1647 _cleanup_free_
char *buf
= NULL
;
1651 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
) {
1653 a
->rbps
= CGROUP_LIMIT_MAX
;
1655 a
->wbps
= CGROUP_LIMIT_MAX
;
1658 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1660 f
= memstream_init(&m
);
1665 fputs("BlockIOReadBandwidth=\n", f
);
1666 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1667 if (a
->rbps
!= CGROUP_LIMIT_MAX
)
1668 fprintf(f
, "BlockIOReadBandwidth=%s %" PRIu64
"\n", a
->path
, a
->rbps
);
1670 fputs("BlockIOWriteBandwidth=\n", f
);
1671 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1672 if (a
->wbps
!= CGROUP_LIMIT_MAX
)
1673 fprintf(f
, "BlockIOWriteBandwidth=%s %" PRIu64
"\n", a
->path
, a
->wbps
);
1676 r
= memstream_finalize(&m
, &buf
, NULL
);
1680 unit_write_setting(u
, flags
, name
, buf
);
1685 } else if (streq(name
, "BlockIODeviceWeight")) {
1690 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1694 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1696 if (!path_is_normalized(path
))
1697 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1699 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight
) || weight
== CGROUP_BLKIO_WEIGHT_INVALID
)
1700 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "BlockIODeviceWeight= out of range");
1702 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1703 CGroupBlockIODeviceWeight
*a
= NULL
;
1705 LIST_FOREACH(device_weights
, b
, c
->blockio_device_weights
)
1706 if (path_equal(b
->path
, path
)) {
1712 a
= new0(CGroupBlockIODeviceWeight
, 1);
1716 a
->path
= strdup(path
);
1721 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, a
);
1730 r
= sd_bus_message_exit_container(message
);
1734 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1735 _cleanup_(memstream_done
) MemStream m
= {};
1736 _cleanup_free_
char *buf
= NULL
;
1740 while (c
->blockio_device_weights
)
1741 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
1743 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1745 f
= memstream_init(&m
);
1749 fputs("BlockIODeviceWeight=\n", f
);
1750 LIST_FOREACH(device_weights
, a
, c
->blockio_device_weights
)
1751 fprintf(f
, "BlockIODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1753 r
= memstream_finalize(&m
, &buf
, NULL
);
1757 unit_write_setting(u
, flags
, name
, buf
);
1762 } else if (streq(name
, "DevicePolicy")) {
1764 CGroupDevicePolicy p
;
1766 r
= sd_bus_message_read(message
, "s", &policy
);
1770 p
= cgroup_device_policy_from_string(policy
);
1774 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1775 c
->device_policy
= p
;
1776 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1777 unit_write_settingf(u
, flags
, name
, "DevicePolicy=%s", policy
);
1782 } else if (streq(name
, "DeviceAllow")) {
1783 const char *path
, *rwm
;
1786 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
1790 while ((r
= sd_bus_message_read(message
, "(ss)", &path
, &rwm
)) > 0) {
1792 if (!valid_device_allow_pattern(path
) || strpbrk(path
, WHITESPACE
))
1793 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires device node or pattern");
1797 else if (!in_charset(rwm
, "rwm"))
1798 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires combination of rwm flags");
1800 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1801 CGroupDeviceAllow
*a
= NULL
;
1803 LIST_FOREACH(device_allow
, b
, c
->device_allow
)
1804 if (path_equal(b
->path
, path
)) {
1810 a
= new0(CGroupDeviceAllow
, 1);
1814 a
->path
= strdup(path
);
1820 LIST_PREPEND(device_allow
, c
->device_allow
, a
);
1823 a
->r
= strchr(rwm
, 'r');
1824 a
->w
= strchr(rwm
, 'w');
1825 a
->m
= strchr(rwm
, 'm');
1833 r
= sd_bus_message_exit_container(message
);
1837 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1838 _cleanup_(memstream_done
) MemStream m
= {};
1839 _cleanup_free_
char *buf
= NULL
;
1843 while (c
->device_allow
)
1844 cgroup_context_free_device_allow(c
, c
->device_allow
);
1846 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1848 f
= memstream_init(&m
);
1852 fputs("DeviceAllow=\n", f
);
1853 LIST_FOREACH(device_allow
, a
, c
->device_allow
)
1854 fprintf(f
, "DeviceAllow=%s %s%s%s\n", a
->path
, a
->r
? "r" : "", a
->w
? "w" : "", a
->m
? "m" : "");
1856 r
= memstream_finalize(&m
, &buf
, NULL
);
1860 unit_write_setting(u
, flags
, name
, buf
);
1865 } else if (streq(name
, "IPAccounting")) {
1868 r
= sd_bus_message_read(message
, "b", &b
);
1872 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1873 c
->ip_accounting
= b
;
1875 unit_invalidate_cgroup_bpf(u
);
1876 unit_write_settingf(u
, flags
, name
, "IPAccounting=%s", yes_no(b
));
1881 } else if (STR_IN_SET(name
, "IPAddressAllow", "IPAddressDeny")) {
1882 _cleanup_set_free_ Set
*new_prefixes
= NULL
;
1885 r
= sd_bus_message_enter_container(message
, 'a', "(iayu)");
1895 r
= sd_bus_message_enter_container(message
, 'r', "iayu");
1901 r
= sd_bus_message_read(message
, "i", &family
);
1905 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1906 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects IPv4 or IPv6 addresses only.", name
);
1908 r
= sd_bus_message_read_array(message
, 'y', &ap
, &an
);
1912 if (an
!= FAMILY_ADDRESS_SIZE(family
))
1913 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1914 af_to_name(family
), FAMILY_ADDRESS_SIZE(family
), an
);
1916 r
= sd_bus_message_read(message
, "u", &prefixlen
);
1920 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
)*8)
1921 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
));
1923 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1924 struct in_addr_prefix prefix
= {
1926 .prefixlen
= prefixlen
,
1929 memcpy(&prefix
.address
, ap
, an
);
1931 r
= in_addr_prefix_add(&new_prefixes
, &prefix
);
1936 r
= sd_bus_message_exit_container(message
);
1943 r
= sd_bus_message_exit_container(message
);
1947 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1948 _cleanup_(memstream_done
) MemStream m
= {};
1949 _cleanup_free_
char *buf
= NULL
;
1954 unit_invalidate_cgroup_bpf(u
);
1956 f
= memstream_init(&m
);
1960 prefixes
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow
: &c
->ip_address_deny
;
1961 reduced
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow_reduced
: &c
->ip_address_deny_reduced
;
1965 *prefixes
= set_free(*prefixes
);
1971 r
= in_addr_prefixes_merge(prefixes
, new_prefixes
);
1975 const struct in_addr_prefix
*p
;
1976 SET_FOREACH(p
, new_prefixes
)
1977 fprintf(f
, "%s=%s\n", name
,
1978 IN_ADDR_PREFIX_TO_STRING(p
->family
, &p
->address
, p
->prefixlen
));
1981 r
= memstream_finalize(&m
, &buf
, NULL
);
1985 unit_write_setting(u
, flags
, name
, buf
);
1991 if (STR_IN_SET(name
, "ManagedOOMSwap", "ManagedOOMMemoryPressure")) {
1992 ManagedOOMMode
*cgroup_mode
= streq(name
, "ManagedOOMSwap") ? &c
->moom_swap
: &c
->moom_mem_pressure
;
1996 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
1997 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
1999 r
= sd_bus_message_read(message
, "s", &mode
);
2003 m
= managed_oom_mode_from_string(mode
);
2007 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2009 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, mode
);
2012 (void) manager_varlink_send_managed_oom_update(u
);
2016 if (streq(name
, "ManagedOOMMemoryPressureLimit")) {
2019 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
2020 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
2022 r
= sd_bus_message_read(message
, "u", &v
);
2026 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2027 c
->moom_mem_pressure_limit
= v
;
2028 unit_write_settingf(u
, flags
, name
,
2029 "ManagedOOMMemoryPressureLimit=" PERMYRIAD_AS_PERCENT_FORMAT_STR
,
2030 PERMYRIAD_AS_PERCENT_FORMAT_VAL(UINT32_SCALE_TO_PERMYRIAD(v
)));
2033 if (c
->moom_mem_pressure
== MANAGED_OOM_KILL
)
2034 (void) manager_varlink_send_managed_oom_update(u
);
2039 if (streq(name
, "ManagedOOMPreference")) {
2040 ManagedOOMPreference p
;
2043 r
= sd_bus_message_read(message
, "s", &pref
);
2047 p
= managed_oom_preference_from_string(pref
);
2051 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2052 c
->moom_preference
= p
;
2053 unit_write_settingf(u
, flags
, name
, "ManagedOOMPreference=%s", pref
);
2058 if (STR_IN_SET(name
, "SocketBindAllow", "SocketBindDeny")) {
2059 CGroupSocketBindItem
**list
;
2060 uint16_t nr_ports
, port_min
;
2062 int32_t family
, ip_protocol
;
2064 list
= streq(name
, "SocketBindAllow") ? &c
->socket_bind_allow
: &c
->socket_bind_deny
;
2066 r
= sd_bus_message_enter_container(message
, 'a', "(iiqq)");
2070 while ((r
= sd_bus_message_read(message
, "(iiqq)", &family
, &ip_protocol
, &nr_ports
, &port_min
)) > 0) {
2072 if (!IN_SET(family
, AF_UNSPEC
, AF_INET
, AF_INET6
))
2073 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects INET or INET6 family, if specified.", name
);
2075 if (!IN_SET(ip_protocol
, 0, IPPROTO_TCP
, IPPROTO_UDP
))
2076 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects TCP or UDP protocol, if specified.", name
);
2078 if (port_min
+ (uint32_t) nr_ports
> (1 << 16))
2079 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects maximum port value lesser than 65536.", name
);
2081 if (port_min
== 0 && nr_ports
!= 0)
2082 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects port range starting with positive value.", name
);
2084 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2085 _cleanup_free_ CGroupSocketBindItem
*item
= NULL
;
2087 item
= new(CGroupSocketBindItem
, 1);
2091 *item
= (CGroupSocketBindItem
) {
2092 .address_family
= family
,
2093 .ip_protocol
= ip_protocol
,
2094 .nr_ports
= nr_ports
,
2095 .port_min
= port_min
2098 LIST_PREPEND(socket_bind_items
, *list
, TAKE_PTR(item
));
2105 r
= sd_bus_message_exit_container(message
);
2109 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2110 _cleanup_(memstream_done
) MemStream m
= {};
2111 _cleanup_free_
char *buf
= NULL
;
2115 cgroup_context_remove_socket_bind(list
);
2117 if ((u
->manager
->cgroup_supported
& CGROUP_MASK_BPF_SOCKET_BIND
) == 0)
2119 "Unit %s configures source compiled BPF programs "
2120 "but the local system does not support that.\n"
2121 "Starting this unit will fail!", u
->id
);
2124 f
= memstream_init(&m
);
2129 fprintf(f
, "%s=\n", name
);
2131 LIST_FOREACH(socket_bind_items
, item
, *list
) {
2132 fprintf(f
, "%s=", name
);
2133 cgroup_context_dump_socket_bind_item(item
, f
);
2137 r
= memstream_finalize(&m
, &buf
, NULL
);
2141 unit_write_setting(u
, flags
, name
, buf
);
2146 if (streq(name
, "RestrictNetworkInterfaces")) {
2148 _cleanup_strv_free_
char **l
= NULL
;
2150 r
= sd_bus_message_enter_container(message
, 'r', "bas");
2154 r
= sd_bus_message_read(message
, "b", &is_allow_list
);
2158 r
= sd_bus_message_read_strv(message
, &l
);
2162 r
= sd_bus_message_exit_container(message
);
2166 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2167 _cleanup_free_
char *joined
= NULL
;
2169 if (strv_isempty(l
)) {
2170 c
->restrict_network_interfaces_is_allow_list
= false;
2171 c
->restrict_network_interfaces
= set_free(c
->restrict_network_interfaces
);
2173 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2177 if (set_isempty(c
->restrict_network_interfaces
))
2178 c
->restrict_network_interfaces_is_allow_list
= is_allow_list
;
2180 STRV_FOREACH(s
, l
) {
2181 if (!ifname_valid(*s
)) {
2182 log_full(LOG_WARNING
, "Invalid interface name, ignoring: %s", *s
);
2185 if (c
->restrict_network_interfaces_is_allow_list
!= (bool) is_allow_list
)
2186 free(set_remove(c
->restrict_network_interfaces
, *s
));
2188 r
= set_put_strdup(&c
->restrict_network_interfaces
, *s
);
2194 joined
= strv_join(l
, " ");
2198 unit_write_settingf(u
, flags
, name
, "%s=%s%s", name
, is_allow_list
? "" : "~", joined
);
2204 if (streq(name
, "DisableControllers") || (u
->transient
&& u
->load_state
== UNIT_STUB
))
2205 return bus_cgroup_set_transient_property(u
, c
, name
, message
, flags
, error
);