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"
11 #include "cgroup-util.h"
13 #include "core-varlink.h"
14 #include "dbus-cgroup.h"
15 #include "dbus-util.h"
16 #include "errno-util.h"
19 #include "in-addr-prefix-util.h"
20 #include "ip-protocol-list.h"
21 #include "limits-util.h"
22 #include "memstream-util.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "percent-util.h"
26 #include "socket-util.h"
28 BUS_DEFINE_PROPERTY_GET(bus_property_get_tasks_max
, "t", TasksMax
, tasks_max_resolve
);
29 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_cgroup_pressure_watch
, cgroup_pressure_watch
, CGroupPressureWatch
);
31 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_cgroup_device_policy
, cgroup_device_policy
, CGroupDevicePolicy
);
32 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_mode
, managed_oom_mode
, ManagedOOMMode
);
33 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_preference
, managed_oom_preference
, ManagedOOMPreference
);
35 static int property_get_cgroup_mask(
38 const char *interface
,
40 sd_bus_message
*reply
,
42 sd_bus_error
*error
) {
44 CGroupMask
*mask
= userdata
;
50 r
= sd_bus_message_open_container(reply
, 'a', "s");
54 for (CGroupController ctrl
= 0; ctrl
< _CGROUP_CONTROLLER_MAX
; ctrl
++) {
55 if ((*mask
& CGROUP_CONTROLLER_TO_MASK(ctrl
)) == 0)
58 r
= sd_bus_message_append(reply
, "s", cgroup_controller_to_string(ctrl
));
63 return sd_bus_message_close_container(reply
);
66 static int property_get_delegate_controllers(
69 const char *interface
,
71 sd_bus_message
*reply
,
73 sd_bus_error
*error
) {
75 CGroupContext
*c
= ASSERT_PTR(userdata
);
81 return sd_bus_message_append(reply
, "as", 0);
83 return property_get_cgroup_mask(bus
, path
, interface
, property
, reply
, &c
->delegate_controllers
, error
);
86 static int property_get_cpuset(
89 const char *interface
,
91 sd_bus_message
*reply
,
93 sd_bus_error
*error
) {
95 CPUSet
*cpus
= ASSERT_PTR(userdata
);
96 _cleanup_free_
uint8_t *array
= NULL
;
102 (void) cpu_set_to_dbus(cpus
, &array
, &allocated
);
103 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
106 static int property_get_io_device_weight(
109 const char *interface
,
110 const char *property
,
111 sd_bus_message
*reply
,
113 sd_bus_error
*error
) {
115 CGroupContext
*c
= ASSERT_PTR(userdata
);
121 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
125 LIST_FOREACH(device_weights
, w
, c
->io_device_weights
) {
126 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
131 return sd_bus_message_close_container(reply
);
134 static int property_get_io_device_limits(
137 const char *interface
,
138 const char *property
,
139 sd_bus_message
*reply
,
141 sd_bus_error
*error
) {
143 CGroupContext
*c
= ASSERT_PTR(userdata
);
149 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
153 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
) {
154 CGroupIOLimitType type
;
156 type
= cgroup_io_limit_type_from_string(property
);
157 if (type
< 0 || l
->limits
[type
] == cgroup_io_limit_defaults
[type
])
160 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->limits
[type
]);
165 return sd_bus_message_close_container(reply
);
168 static int property_get_io_device_latency(
171 const char *interface
,
172 const char *property
,
173 sd_bus_message
*reply
,
175 sd_bus_error
*error
) {
177 CGroupContext
*c
= ASSERT_PTR(userdata
);
183 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
187 LIST_FOREACH(device_latencies
, l
, c
->io_device_latencies
) {
188 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->target_usec
);
193 return sd_bus_message_close_container(reply
);
196 static int property_get_blockio_device_weight(
199 const char *interface
,
200 const char *property
,
201 sd_bus_message
*reply
,
203 sd_bus_error
*error
) {
205 CGroupContext
*c
= ASSERT_PTR(userdata
);
211 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
215 LIST_FOREACH(device_weights
, w
, c
->blockio_device_weights
) {
216 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
221 return sd_bus_message_close_container(reply
);
224 static int property_get_blockio_device_bandwidths(
227 const char *interface
,
228 const char *property
,
229 sd_bus_message
*reply
,
231 sd_bus_error
*error
) {
233 CGroupContext
*c
= ASSERT_PTR(userdata
);
239 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
243 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
246 if (streq(property
, "BlockIOReadBandwidth"))
251 if (v
== CGROUP_LIMIT_MAX
)
254 r
= sd_bus_message_append(reply
, "(st)", b
->path
, v
);
259 return sd_bus_message_close_container(reply
);
262 static int property_get_device_allow(
265 const char *interface
,
266 const char *property
,
267 sd_bus_message
*reply
,
269 sd_bus_error
*error
) {
271 CGroupContext
*c
= ASSERT_PTR(userdata
);
277 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
281 LIST_FOREACH(device_allow
, a
, c
->device_allow
) {
294 r
= sd_bus_message_append(reply
, "(ss)", a
->path
, rwm
);
299 return sd_bus_message_close_container(reply
);
302 static int property_get_ip_address_access(
305 const char *interface
,
306 const char *property
,
307 sd_bus_message
*reply
,
309 sd_bus_error
*error
) {
311 Set
**prefixes
= ASSERT_PTR(userdata
);
312 struct in_addr_prefix
*i
;
315 r
= sd_bus_message_open_container(reply
, 'a', "(iayu)");
319 SET_FOREACH(i
, *prefixes
) {
321 r
= sd_bus_message_open_container(reply
, 'r', "iayu");
325 r
= sd_bus_message_append(reply
, "i", i
->family
);
329 r
= sd_bus_message_append_array(reply
, 'y', &i
->address
, FAMILY_ADDRESS_SIZE(i
->family
));
333 r
= sd_bus_message_append(reply
, "u", (uint32_t) i
->prefixlen
);
337 r
= sd_bus_message_close_container(reply
);
342 return sd_bus_message_close_container(reply
);
345 static int property_get_bpf_foreign_program(
348 const char *interface
,
349 const char *property
,
350 sd_bus_message
*reply
,
352 sd_bus_error
*error
) {
353 CGroupContext
*c
= userdata
;
356 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
360 LIST_FOREACH(programs
, p
, c
->bpf_foreign_programs
) {
361 const char *attach_type
= bpf_cgroup_attach_type_to_string(p
->attach_type
);
363 r
= sd_bus_message_append(reply
, "(ss)", attach_type
, p
->bpffs_path
);
368 return sd_bus_message_close_container(reply
);
371 static int property_get_socket_bind(
374 const char *interface
,
375 const char *property
,
376 sd_bus_message
*reply
,
378 sd_bus_error
*error
) {
380 CGroupSocketBindItem
**items
= ASSERT_PTR(userdata
);
383 r
= sd_bus_message_open_container(reply
, 'a', "(iiqq)");
387 LIST_FOREACH(socket_bind_items
, i
, *items
) {
388 r
= sd_bus_message_append(reply
, "(iiqq)", i
->address_family
, i
->ip_protocol
, i
->nr_ports
, i
->port_min
);
393 return sd_bus_message_close_container(reply
);
396 static int property_get_restrict_network_interfaces(
399 const char *interface
,
400 const char *property
,
401 sd_bus_message
*reply
,
403 sd_bus_error
*error
) {
405 CGroupContext
*c
= ASSERT_PTR(userdata
);
411 r
= sd_bus_message_open_container(reply
, 'r', "bas");
415 r
= sd_bus_message_append(reply
, "b", c
->restrict_network_interfaces_is_allow_list
);
419 r
= bus_message_append_string_set(reply
, c
->restrict_network_interfaces
);
423 return sd_bus_message_close_container(reply
);
426 const sd_bus_vtable bus_cgroup_vtable
[] = {
427 SD_BUS_VTABLE_START(0),
428 SD_BUS_PROPERTY("Delegate", "b", bus_property_get_bool
, offsetof(CGroupContext
, delegate
), 0),
429 SD_BUS_PROPERTY("DelegateControllers", "as", property_get_delegate_controllers
, 0, 0),
430 SD_BUS_PROPERTY("DelegateSubgroup", "s", NULL
, offsetof(CGroupContext
, delegate_subgroup
), 0),
431 SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, cpu_accounting
), 0),
432 SD_BUS_PROPERTY("CPUWeight", "t", NULL
, offsetof(CGroupContext
, cpu_weight
), 0),
433 SD_BUS_PROPERTY("StartupCPUWeight", "t", NULL
, offsetof(CGroupContext
, startup_cpu_weight
), 0),
434 SD_BUS_PROPERTY("CPUShares", "t", NULL
, offsetof(CGroupContext
, cpu_shares
), 0),
435 SD_BUS_PROPERTY("StartupCPUShares", "t", NULL
, offsetof(CGroupContext
, startup_cpu_shares
), 0),
436 SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_per_sec_usec
), 0),
437 SD_BUS_PROPERTY("CPUQuotaPeriodUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_period_usec
), 0),
438 SD_BUS_PROPERTY("AllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_cpus
), 0),
439 SD_BUS_PROPERTY("StartupAllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_cpus
), 0),
440 SD_BUS_PROPERTY("AllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_mems
), 0),
441 SD_BUS_PROPERTY("StartupAllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_mems
), 0),
442 SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, io_accounting
), 0),
443 SD_BUS_PROPERTY("IOWeight", "t", NULL
, offsetof(CGroupContext
, io_weight
), 0),
444 SD_BUS_PROPERTY("StartupIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_io_weight
), 0),
445 SD_BUS_PROPERTY("IODeviceWeight", "a(st)", property_get_io_device_weight
, 0, 0),
446 SD_BUS_PROPERTY("IOReadBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
447 SD_BUS_PROPERTY("IOWriteBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
448 SD_BUS_PROPERTY("IOReadIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
449 SD_BUS_PROPERTY("IOWriteIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
450 SD_BUS_PROPERTY("IODeviceLatencyTargetUSec", "a(st)", property_get_io_device_latency
, 0, 0),
451 SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, blockio_accounting
), 0),
452 SD_BUS_PROPERTY("BlockIOWeight", "t", NULL
, offsetof(CGroupContext
, blockio_weight
), 0),
453 SD_BUS_PROPERTY("StartupBlockIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_blockio_weight
), 0),
454 SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight
, 0, 0),
455 SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
456 SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
457 SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, memory_accounting
), 0),
458 SD_BUS_PROPERTY("DefaultMemoryLow", "t", NULL
, offsetof(CGroupContext
, default_memory_low
), 0),
459 SD_BUS_PROPERTY("DefaultStartupMemoryLow", "t", NULL
, offsetof(CGroupContext
, default_startup_memory_low
), 0),
460 SD_BUS_PROPERTY("DefaultMemoryMin", "t", NULL
, offsetof(CGroupContext
, default_memory_min
), 0),
461 SD_BUS_PROPERTY("MemoryMin", "t", NULL
, offsetof(CGroupContext
, memory_min
), 0),
462 SD_BUS_PROPERTY("MemoryLow", "t", NULL
, offsetof(CGroupContext
, memory_low
), 0),
463 SD_BUS_PROPERTY("StartupMemoryLow", "t", NULL
, offsetof(CGroupContext
, startup_memory_low
), 0),
464 SD_BUS_PROPERTY("MemoryHigh", "t", NULL
, offsetof(CGroupContext
, memory_high
), 0),
465 SD_BUS_PROPERTY("StartupMemoryHigh", "t", NULL
, offsetof(CGroupContext
, startup_memory_high
), 0),
466 SD_BUS_PROPERTY("MemoryMax", "t", NULL
, offsetof(CGroupContext
, memory_max
), 0),
467 SD_BUS_PROPERTY("StartupMemoryMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_max
), 0),
468 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL
, offsetof(CGroupContext
, memory_swap_max
), 0),
469 SD_BUS_PROPERTY("StartupMemorySwapMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_swap_max
), 0),
470 SD_BUS_PROPERTY("MemoryZSwapMax", "t", NULL
, offsetof(CGroupContext
, memory_zswap_max
), 0),
471 SD_BUS_PROPERTY("StartupMemoryZSwapMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_zswap_max
), 0),
472 SD_BUS_PROPERTY("MemoryLimit", "t", NULL
, offsetof(CGroupContext
, memory_limit
), 0),
473 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy
, offsetof(CGroupContext
, device_policy
), 0),
474 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow
, 0, 0),
475 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, tasks_accounting
), 0),
476 SD_BUS_PROPERTY("TasksMax", "t", bus_property_get_tasks_max
, offsetof(CGroupContext
, tasks_max
), 0),
477 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, ip_accounting
), 0),
478 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_allow
), 0),
479 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_deny
), 0),
480 SD_BUS_PROPERTY("IPIngressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_ingress
), 0),
481 SD_BUS_PROPERTY("IPEgressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_egress
), 0),
482 SD_BUS_PROPERTY("DisableControllers", "as", property_get_cgroup_mask
, offsetof(CGroupContext
, disable_controllers
), 0),
483 SD_BUS_PROPERTY("ManagedOOMSwap", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_swap
), 0),
484 SD_BUS_PROPERTY("ManagedOOMMemoryPressure", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_mem_pressure
), 0),
485 SD_BUS_PROPERTY("ManagedOOMMemoryPressureLimit", "u", NULL
, offsetof(CGroupContext
, moom_mem_pressure_limit
), 0),
486 SD_BUS_PROPERTY("ManagedOOMPreference", "s", property_get_managed_oom_preference
, offsetof(CGroupContext
, moom_preference
), 0),
487 SD_BUS_PROPERTY("BPFProgram", "a(ss)", property_get_bpf_foreign_program
, 0, 0),
488 SD_BUS_PROPERTY("SocketBindAllow", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_allow
), 0),
489 SD_BUS_PROPERTY("SocketBindDeny", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_deny
), 0),
490 SD_BUS_PROPERTY("RestrictNetworkInterfaces", "(bas)", property_get_restrict_network_interfaces
, 0, 0),
491 SD_BUS_PROPERTY("MemoryPressureWatch", "s", bus_property_get_cgroup_pressure_watch
, offsetof(CGroupContext
, memory_pressure_watch
), 0),
492 SD_BUS_PROPERTY("MemoryPressureThresholdUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, memory_pressure_threshold_usec
), 0),
496 static int bus_cgroup_set_transient_property(
500 sd_bus_message
*message
,
501 UnitWriteFlags flags
,
502 sd_bus_error
*error
) {
511 flags
|= UNIT_PRIVATE
;
513 if (streq(name
, "Delegate")) {
516 if (!UNIT_VTABLE(u
)->can_delegate
)
517 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
519 r
= sd_bus_message_read(message
, "b", &b
);
523 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
525 c
->delegate_controllers
= b
? _CGROUP_MASK_ALL
: 0;
527 unit_write_settingf(u
, flags
, name
, "Delegate=%s", yes_no(b
));
532 } else if (streq(name
, "DelegateSubgroup")) {
535 if (!UNIT_VTABLE(u
)->can_delegate
)
536 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
538 r
= sd_bus_message_read(message
, "s", &s
);
542 if (!isempty(s
) && cg_needs_escape(s
))
543 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid control group name: %s", s
);
545 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
547 c
->delegate_subgroup
= mfree(c
->delegate_subgroup
);
549 r
= free_and_strdup_warn(&c
->delegate_subgroup
, s
);
554 unit_write_settingf(u
, flags
, name
, "DelegateSubgroup=%s", s
);
559 } else if (STR_IN_SET(name
, "DelegateControllers", "DisableControllers")) {
562 if (streq(name
, "DelegateControllers") && !UNIT_VTABLE(u
)->can_delegate
)
563 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
565 r
= sd_bus_message_enter_container(message
, 'a', "s");
573 r
= sd_bus_message_read(message
, "s", &t
);
579 cc
= cgroup_controller_from_string(t
);
581 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown cgroup controller '%s'", t
);
583 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
586 r
= sd_bus_message_exit_container(message
);
590 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
591 _cleanup_free_
char *t
= NULL
;
593 r
= cg_mask_to_string(mask
, &t
);
597 if (streq(name
, "DelegateControllers")) {
601 c
->delegate_controllers
= 0;
603 c
->delegate_controllers
|= mask
;
605 unit_write_settingf(u
, flags
, name
, "Delegate=%s", strempty(t
));
607 } else if (streq(name
, "DisableControllers")) {
610 c
->disable_controllers
= 0;
612 c
->disable_controllers
|= mask
;
614 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, strempty(t
));
619 } else if (STR_IN_SET(name
, "IPIngressFilterPath", "IPEgressFilterPath")) {
623 filters
= streq(name
, "IPIngressFilterPath") ? &c
->ip_filters_ingress
: &c
->ip_filters_egress
;
624 r
= sd_bus_message_enter_container(message
, 'a', "s");
631 r
= sd_bus_message_read(message
, "s", &path
);
637 if (!path_is_normalized(path
) || !path_is_absolute(path
))
638 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects a normalized absolute path.", name
);
640 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && !strv_contains(*filters
, path
)) {
641 r
= strv_extend(filters
, path
);
647 r
= sd_bus_message_exit_container(message
);
651 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
652 _cleanup_(memstream_done
) MemStream m
= {};
653 _cleanup_free_
char *buf
= NULL
;
657 *filters
= strv_free(*filters
);
659 unit_invalidate_cgroup_bpf(u
);
661 f
= memstream_init(&m
);
668 STRV_FOREACH(entry
, *filters
)
669 fprintf(f
, "%s=%s\n", name
, *entry
);
671 r
= memstream_finalize(&m
, &buf
, NULL
);
675 unit_write_setting(u
, flags
, name
, buf
);
678 r
= bpf_firewall_supported();
681 if (r
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
) {
682 static bool warned
= false;
684 log_full(warned
? LOG_DEBUG
: LOG_WARNING
,
685 "Transient unit %s configures an IP firewall with BPF, but the local system does not support BPF/cgroup firewalling with multiple filters.\n"
686 "Starting this unit will fail! (This warning is only shown for the first started transient unit using IP firewalling.)", u
->id
);
693 } else if (streq(name
, "BPFProgram")) {
697 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
701 while ((r
= sd_bus_message_read(message
, "(ss)", &a
, &p
)) > 0) {
702 int attach_type
= bpf_cgroup_attach_type_from_string(a
);
704 return sd_bus_error_setf(
706 SD_BUS_ERROR_INVALID_ARGS
,
707 "%s expects a valid BPF attach type, got '%s'.",
710 if (!path_is_normalized(p
) || !path_is_absolute(p
))
711 return sd_bus_error_setf(
713 SD_BUS_ERROR_INVALID_ARGS
,
714 "%s= expects a normalized absolute path.",
717 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
718 r
= cgroup_add_bpf_foreign_program(c
, attach_type
, p
);
727 r
= sd_bus_message_exit_container(message
);
731 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
732 _cleanup_(memstream_done
) MemStream m
= {};
733 _cleanup_free_
char *buf
= NULL
;
737 while (c
->bpf_foreign_programs
)
738 cgroup_context_remove_bpf_foreign_program(c
, c
->bpf_foreign_programs
);
740 f
= memstream_init(&m
);
747 LIST_FOREACH(programs
, fp
, c
->bpf_foreign_programs
)
748 fprintf(f
, "%s=%s:%s\n", name
,
749 bpf_cgroup_attach_type_to_string(fp
->attach_type
),
752 r
= memstream_finalize(&m
, &buf
, NULL
);
756 unit_write_setting(u
, flags
, name
, buf
);
758 if (c
->bpf_foreign_programs
) {
759 r
= bpf_foreign_supported();
764 "Transient unit %s configures a BPF program pinned to BPF "
765 "filesystem, but the local system does not support that.\n"
766 "Starting this unit will fail!", u
->id
);
772 } else if (streq(name
, "MemoryPressureWatch")) {
773 CGroupPressureWatch p
;
776 r
= sd_bus_message_read(message
, "s", &t
);
781 p
= _CGROUP_PRESSURE_WATCH_INVALID
;
783 p
= cgroup_pressure_watch_from_string(t
);
788 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
789 c
->memory_pressure_watch
= p
;
790 unit_write_settingf(u
, flags
, name
, "MemoryPressureWatch=%s", strempty(cgroup_pressure_watch_to_string(p
)));
795 } else if (streq(name
, "MemoryPressureThresholdUSec")) {
798 r
= sd_bus_message_read(message
, "t", &t
);
802 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
803 c
->memory_pressure_threshold_usec
= t
;
806 unit_write_setting(u
, flags
, name
, "MemoryPressureThresholdUSec=");
808 unit_write_settingf(u
, flags
, name
, "MemoryPressureThresholdUSec=%" PRIu64
, t
);
817 static int bus_cgroup_set_boolean(
822 sd_bus_message
*message
,
823 UnitWriteFlags flags
,
824 sd_bus_error
*error
) {
830 r
= sd_bus_message_read(message
, "b", &b
);
834 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
836 unit_invalidate_cgroup(u
, mask
);
837 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, yes_no(b
));
843 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
844 static int bus_cgroup_set_##function( \
848 sd_bus_message *message, \
849 UnitWriteFlags flags, \
850 sd_bus_error *error) { \
857 r = sd_bus_message_read(message, "t", &v); \
862 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
863 "Value specified in %s is out of range", name); \
865 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
867 unit_invalidate_cgroup(u, mask); \
870 unit_write_settingf(u, flags, name, \
873 unit_write_settingf(u, flags, name, \
874 "%s=%" PRIu64, name, v); \
880 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
881 static int bus_cgroup_set_##function( \
885 sd_bus_message *message, \
886 UnitWriteFlags flags, \
887 sd_bus_error *error) { \
894 r = sd_bus_message_read(message, "t", &v); \
899 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
900 "Value specified in %s is out of range", name); \
902 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
904 unit_invalidate_cgroup(u, mask); \
906 if (v == CGROUP_LIMIT_MAX) \
907 unit_write_settingf(u, flags, name, \
908 "%s=infinity", name); \
910 unit_write_settingf(u, flags, name, \
911 "%s=%" PRIu64, name, v); \
916 static int bus_cgroup_set_##function##_scale( \
920 sd_bus_message *message, \
921 UnitWriteFlags flags, \
922 sd_bus_error *error) { \
930 r = sd_bus_message_read(message, "u", &raw); \
934 v = scale(raw, UINT32_MAX); \
935 if (v < minimum || v >= UINT64_MAX) \
936 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
937 "Value specified in %s is out of range", name); \
939 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
941 unit_invalidate_cgroup(u, mask); \
943 /* Prepare to chop off suffix */ \
944 assert_se(endswith(name, "Scale")); \
946 int scaled = UINT32_SCALE_TO_PERMYRIAD(raw); \
947 unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR, \
948 (int)(strlen(name) - strlen("Scale")), name, \
949 PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled)); \
955 DISABLE_WARNING_TYPE_LIMITS
;
956 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares
, CGROUP_MASK_CPU
, CGROUP_CPU_SHARES_IS_OK
, CGROUP_CPU_SHARES_INVALID
);
957 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight
, CGROUP_MASK_IO
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
958 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight
, CGROUP_MASK_BLKIO
, CGROUP_BLKIO_WEIGHT_IS_OK
, CGROUP_BLKIO_WEIGHT_INVALID
);
959 BUS_DEFINE_SET_CGROUP_LIMIT(memory
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 1);
960 BUS_DEFINE_SET_CGROUP_LIMIT(memory_protection
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
961 BUS_DEFINE_SET_CGROUP_LIMIT(swap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
962 BUS_DEFINE_SET_CGROUP_LIMIT(zswap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
965 static int bus_cgroup_set_cpu_weight(
969 sd_bus_message
*message
,
970 UnitWriteFlags flags
,
971 sd_bus_error
*error
) {
975 r
= sd_bus_message_read(message
, "t", &v
);
978 if (!CGROUP_WEIGHT_IS_OK(v
) && v
!= CGROUP_WEIGHT_IDLE
)
979 return sd_bus_error_setf(
980 error
, SD_BUS_ERROR_INVALID_ARGS
, "Value specified in %s is out of range", name
);
981 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
983 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
984 if (v
== CGROUP_WEIGHT_INVALID
)
985 unit_write_settingf(u
, flags
, name
, "%s=", name
);
986 else if (v
== CGROUP_WEIGHT_IDLE
)
987 unit_write_settingf(u
, flags
, name
, "%s=idle", name
);
989 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu64
, name
, v
);
994 static int bus_cgroup_set_tasks_max(
998 sd_bus_message
*message
,
999 UnitWriteFlags flags
,
1000 sd_bus_error
*error
) {
1007 r
= sd_bus_message_read(message
, "t", &v
);
1012 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1013 "Value specified in %s is out of range", name
);
1015 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1016 *p
= (TasksMax
) { .value
= v
, .scale
= 0 }; /* When .scale==0, .value is the absolute value */
1017 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
1019 if (v
== CGROUP_LIMIT_MAX
)
1020 unit_write_settingf(u
, flags
, name
,
1021 "%s=infinity", name
);
1023 unit_write_settingf(u
, flags
, name
,
1024 "%s=%" PRIu64
, name
, v
);
1030 static int bus_cgroup_set_tasks_max_scale(
1034 sd_bus_message
*message
,
1035 UnitWriteFlags flags
,
1036 sd_bus_error
*error
) {
1043 r
= sd_bus_message_read(message
, "u", &v
);
1047 if (v
< 1 || v
>= UINT32_MAX
)
1048 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1049 "Value specified in %s is out of range", name
);
1051 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1052 *p
= (TasksMax
) { v
, UINT32_MAX
}; /* .scale is not 0, so this is interpreted as v/UINT32_MAX. */
1053 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
1055 uint32_t scaled
= DIV_ROUND_UP((uint64_t) v
* 100U, (uint64_t) UINT32_MAX
);
1056 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu32
".%" PRIu32
"%%", "TasksMax",
1057 scaled
/ 10, scaled
% 10);
1063 int bus_cgroup_set_property(
1067 sd_bus_message
*message
,
1068 UnitWriteFlags flags
,
1069 sd_bus_error
*error
) {
1071 CGroupIOLimitType iol_type
;
1079 flags
|= UNIT_PRIVATE
;
1081 if (streq(name
, "CPUAccounting"))
1082 return bus_cgroup_set_boolean(u
, name
, &c
->cpu_accounting
, get_cpu_accounting_mask(), message
, flags
, error
);
1084 if (streq(name
, "CPUWeight"))
1085 return bus_cgroup_set_cpu_weight(u
, name
, &c
->cpu_weight
, message
, flags
, error
);
1087 if (streq(name
, "StartupCPUWeight"))
1088 return bus_cgroup_set_cpu_weight(u
, name
, &c
->startup_cpu_weight
, message
, flags
, error
);
1090 if (streq(name
, "CPUShares"))
1091 return bus_cgroup_set_cpu_shares(u
, name
, &c
->cpu_shares
, message
, flags
, error
);
1093 if (streq(name
, "StartupCPUShares"))
1094 return bus_cgroup_set_cpu_shares(u
, name
, &c
->startup_cpu_shares
, message
, flags
, error
);
1096 if (streq(name
, "IOAccounting"))
1097 return bus_cgroup_set_boolean(u
, name
, &c
->io_accounting
, CGROUP_MASK_IO
, message
, flags
, error
);
1099 if (streq(name
, "IOWeight"))
1100 return bus_cgroup_set_io_weight(u
, name
, &c
->io_weight
, message
, flags
, error
);
1102 if (streq(name
, "StartupIOWeight"))
1103 return bus_cgroup_set_io_weight(u
, name
, &c
->startup_io_weight
, message
, flags
, error
);
1105 if (streq(name
, "BlockIOAccounting"))
1106 return bus_cgroup_set_boolean(u
, name
, &c
->blockio_accounting
, CGROUP_MASK_BLKIO
, message
, flags
, error
);
1108 if (streq(name
, "BlockIOWeight"))
1109 return bus_cgroup_set_blockio_weight(u
, name
, &c
->blockio_weight
, message
, flags
, error
);
1111 if (streq(name
, "StartupBlockIOWeight"))
1112 return bus_cgroup_set_blockio_weight(u
, name
, &c
->startup_blockio_weight
, message
, flags
, error
);
1114 if (streq(name
, "MemoryAccounting"))
1115 return bus_cgroup_set_boolean(u
, name
, &c
->memory_accounting
, CGROUP_MASK_MEMORY
, message
, flags
, error
);
1117 if (streq(name
, "MemoryMin")) {
1118 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_min
, message
, flags
, error
);
1120 c
->memory_min_set
= true;
1124 if (streq(name
, "MemoryLow")) {
1125 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_low
, message
, flags
, error
);
1127 c
->memory_low_set
= true;
1131 if (streq(name
, "StartupMemoryLow")) {
1132 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->startup_memory_low
, message
, flags
, error
);
1134 c
->startup_memory_low_set
= true;
1138 if (streq(name
, "DefaultMemoryMin")) {
1139 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1141 c
->default_memory_min_set
= true;
1145 if (streq(name
, "DefaultMemoryLow")) {
1146 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1148 c
->default_memory_low_set
= true;
1152 if (streq(name
, "DefaultStartupMemoryLow")) {
1153 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_startup_memory_low
, message
, flags
, error
);
1155 c
->default_startup_memory_low_set
= true;
1159 if (streq(name
, "MemoryHigh"))
1160 return bus_cgroup_set_memory(u
, name
, &c
->memory_high
, message
, flags
, error
);
1162 if (streq(name
, "StartupMemoryHigh")) {
1163 r
= bus_cgroup_set_memory(u
, name
, &c
->startup_memory_high
, message
, flags
, error
);
1165 c
->startup_memory_high_set
= true;
1169 if (streq(name
, "MemorySwapMax"))
1170 return bus_cgroup_set_swap(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1172 if (streq(name
, "StartupMemorySwapMax")) {
1173 r
= bus_cgroup_set_swap(u
, name
, &c
->startup_memory_swap_max
, message
, flags
, error
);
1175 c
->startup_memory_swap_max_set
= true;
1179 if (streq(name
, "MemoryZSwapMax"))
1180 return bus_cgroup_set_zswap(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1182 if (streq(name
, "StartupMemoryZSwapMax")) {
1183 r
= bus_cgroup_set_zswap(u
, name
, &c
->startup_memory_zswap_max
, message
, flags
, error
);
1185 c
->startup_memory_zswap_max_set
= true;
1189 if (streq(name
, "MemoryMax"))
1190 return bus_cgroup_set_memory(u
, name
, &c
->memory_max
, message
, flags
, error
);
1192 if (streq(name
, "StartupMemoryMax")) {
1193 r
= bus_cgroup_set_memory(u
, name
, &c
->startup_memory_max
, message
, flags
, error
);
1195 c
->startup_memory_max_set
= true;
1199 if (streq(name
, "MemoryLimit"))
1200 return bus_cgroup_set_memory(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1202 if (streq(name
, "MemoryMinScale")) {
1203 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_min
, message
, flags
, error
);
1205 c
->memory_min_set
= true;
1209 if (streq(name
, "MemoryLowScale")) {
1210 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_low
, message
, flags
, error
);
1212 c
->memory_low_set
= true;
1216 if (streq(name
, "DefaultMemoryMinScale")) {
1217 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1219 c
->default_memory_min_set
= true;
1223 if (streq(name
, "DefaultMemoryLowScale")) {
1224 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1226 c
->default_memory_low_set
= true;
1230 if (streq(name
, "MemoryHighScale"))
1231 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_high
, message
, flags
, error
);
1233 if (streq(name
, "MemorySwapMaxScale"))
1234 return bus_cgroup_set_swap_scale(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1236 if (streq(name
, "MemoryZSwapMaxScale"))
1237 return bus_cgroup_set_zswap_scale(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1239 if (streq(name
, "MemoryMaxScale"))
1240 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_max
, message
, flags
, error
);
1242 if (streq(name
, "MemoryLimitScale"))
1243 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1245 if (streq(name
, "TasksAccounting"))
1246 return bus_cgroup_set_boolean(u
, name
, &c
->tasks_accounting
, CGROUP_MASK_PIDS
, message
, flags
, error
);
1248 if (streq(name
, "TasksMax"))
1249 return bus_cgroup_set_tasks_max(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1251 if (streq(name
, "TasksMaxScale"))
1252 return bus_cgroup_set_tasks_max_scale(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1254 if (streq(name
, "CPUQuotaPerSecUSec")) {
1257 r
= sd_bus_message_read(message
, "t", &u64
);
1262 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "CPUQuotaPerSecUSec= value out of range");
1264 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1265 c
->cpu_quota_per_sec_usec
= u64
;
1266 u
->warned_clamping_cpu_quota_period
= false;
1267 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1269 if (c
->cpu_quota_per_sec_usec
== USEC_INFINITY
)
1270 unit_write_setting(u
, flags
, "CPUQuota", "CPUQuota=");
1272 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
1274 unit_write_settingf(u
, flags
, "CPUQuota",
1276 (double) (c
->cpu_quota_per_sec_usec
/ 10000));
1281 } else if (streq(name
, "CPUQuotaPeriodUSec")) {
1284 r
= sd_bus_message_read(message
, "t", &u64
);
1288 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1289 c
->cpu_quota_period_usec
= u64
;
1290 u
->warned_clamping_cpu_quota_period
= false;
1291 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1292 if (c
->cpu_quota_period_usec
== USEC_INFINITY
)
1293 unit_write_setting(u
, flags
, "CPUQuotaPeriodSec", "CPUQuotaPeriodSec=");
1295 unit_write_settingf(u
, flags
, "CPUQuotaPeriodSec",
1296 "CPUQuotaPeriodSec=%s",
1297 FORMAT_TIMESPAN(c
->cpu_quota_period_usec
, 1));
1302 } else if (STR_IN_SET(name
, "AllowedCPUs", "StartupAllowedCPUs", "AllowedMemoryNodes", "StartupAllowedMemoryNodes")) {
1305 _cleanup_(cpu_set_reset
) CPUSet new_set
= {};
1307 r
= sd_bus_message_read_array(message
, 'y', &a
, &n
);
1311 r
= cpu_set_from_dbus(a
, n
, &new_set
);
1315 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1316 _cleanup_free_
char *setstr
= NULL
;
1319 setstr
= cpu_set_to_range_string(&new_set
);
1323 if (streq(name
, "AllowedCPUs"))
1324 set
= &c
->cpuset_cpus
;
1325 else if (streq(name
, "StartupAllowedCPUs"))
1326 set
= &c
->startup_cpuset_cpus
;
1327 else if (streq(name
, "AllowedMemoryNodes"))
1328 set
= &c
->cpuset_mems
;
1329 else if (streq(name
, "StartupAllowedMemoryNodes"))
1330 set
= &c
->startup_cpuset_mems
;
1336 new_set
= (CPUSet
) {};
1338 unit_invalidate_cgroup(u
, CGROUP_MASK_CPUSET
);
1339 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, setstr
);
1344 } else if ((iol_type
= cgroup_io_limit_type_from_string(name
)) >= 0) {
1349 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1353 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1355 if (!path_is_normalized(path
))
1356 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1358 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1359 CGroupIODeviceLimit
*a
= NULL
;
1361 LIST_FOREACH(device_limits
, b
, c
->io_device_limits
)
1362 if (path_equal(path
, b
->path
)) {
1368 CGroupIOLimitType type
;
1370 a
= new0(CGroupIODeviceLimit
, 1);
1374 a
->path
= strdup(path
);
1380 for (type
= 0; type
< _CGROUP_IO_LIMIT_TYPE_MAX
; type
++)
1381 a
->limits
[type
] = cgroup_io_limit_defaults
[type
];
1383 LIST_PREPEND(device_limits
, c
->io_device_limits
, a
);
1386 a
->limits
[iol_type
] = u64
;
1394 r
= sd_bus_message_exit_container(message
);
1398 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1399 _cleanup_(memstream_done
) MemStream m
= {};
1400 _cleanup_free_
char *buf
= NULL
;
1404 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1405 a
->limits
[iol_type
] = cgroup_io_limit_defaults
[iol_type
];
1407 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1409 f
= memstream_init(&m
);
1413 fprintf(f
, "%s=\n", name
);
1414 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1415 if (a
->limits
[iol_type
] != cgroup_io_limit_defaults
[iol_type
])
1416 fprintf(f
, "%s=%s %" PRIu64
"\n", name
, a
->path
, a
->limits
[iol_type
]);
1418 r
= memstream_finalize(&m
, &buf
, NULL
);
1422 unit_write_setting(u
, flags
, name
, buf
);
1427 } else if (streq(name
, "IODeviceWeight")) {
1432 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1436 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1438 if (!path_is_normalized(path
))
1439 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1441 if (!CGROUP_WEIGHT_IS_OK(weight
) || weight
== CGROUP_WEIGHT_INVALID
)
1442 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "IODeviceWeight= value out of range");
1444 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1445 CGroupIODeviceWeight
*a
= NULL
;
1447 LIST_FOREACH(device_weights
, b
, c
->io_device_weights
)
1448 if (path_equal(b
->path
, path
)) {
1454 a
= new0(CGroupIODeviceWeight
, 1);
1458 a
->path
= strdup(path
);
1463 LIST_PREPEND(device_weights
, c
->io_device_weights
, a
);
1472 r
= sd_bus_message_exit_container(message
);
1476 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1477 _cleanup_(memstream_done
) MemStream m
= {};
1478 _cleanup_free_
char *buf
= NULL
;
1482 while (c
->io_device_weights
)
1483 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
1485 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1487 f
= memstream_init(&m
);
1491 fputs("IODeviceWeight=\n", f
);
1492 LIST_FOREACH(device_weights
, a
, c
->io_device_weights
)
1493 fprintf(f
, "IODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1495 r
= memstream_finalize(&m
, &buf
, NULL
);
1499 unit_write_setting(u
, flags
, name
, buf
);
1504 } else if (streq(name
, "IODeviceLatencyTargetUSec")) {
1509 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1513 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &target
)) > 0) {
1515 if (!path_is_normalized(path
))
1516 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1518 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1519 CGroupIODeviceLatency
*a
= NULL
;
1521 LIST_FOREACH(device_latencies
, b
, c
->io_device_latencies
)
1522 if (path_equal(b
->path
, path
)) {
1528 a
= new0(CGroupIODeviceLatency
, 1);
1532 a
->path
= strdup(path
);
1537 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, a
);
1540 a
->target_usec
= target
;
1546 r
= sd_bus_message_exit_container(message
);
1550 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1551 _cleanup_(memstream_done
) MemStream m
= {};
1552 _cleanup_free_
char *buf
= NULL
;
1556 while (c
->io_device_latencies
)
1557 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
1559 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1561 f
= memstream_init(&m
);
1565 fputs("IODeviceLatencyTargetSec=\n", f
);
1566 LIST_FOREACH(device_latencies
, a
, c
->io_device_latencies
)
1567 fprintf(f
, "IODeviceLatencyTargetSec=%s %s\n",
1568 a
->path
, FORMAT_TIMESPAN(a
->target_usec
, 1));
1570 r
= memstream_finalize(&m
, &buf
, NULL
);
1574 unit_write_setting(u
, flags
, name
, buf
);
1579 } else if (STR_IN_SET(name
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
1585 read
= streq(name
, "BlockIOReadBandwidth");
1587 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1591 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1593 if (!path_is_normalized(path
))
1594 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1596 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1597 CGroupBlockIODeviceBandwidth
*a
= NULL
;
1599 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
)
1600 if (path_equal(path
, b
->path
)) {
1606 a
= new0(CGroupBlockIODeviceBandwidth
, 1);
1610 a
->rbps
= CGROUP_LIMIT_MAX
;
1611 a
->wbps
= CGROUP_LIMIT_MAX
;
1612 a
->path
= strdup(path
);
1618 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, a
);
1632 r
= sd_bus_message_exit_container(message
);
1636 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1637 _cleanup_(memstream_done
) MemStream m
= {};
1638 _cleanup_free_
char *buf
= NULL
;
1642 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
) {
1644 a
->rbps
= CGROUP_LIMIT_MAX
;
1646 a
->wbps
= CGROUP_LIMIT_MAX
;
1649 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1651 f
= memstream_init(&m
);
1656 fputs("BlockIOReadBandwidth=\n", f
);
1657 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1658 if (a
->rbps
!= CGROUP_LIMIT_MAX
)
1659 fprintf(f
, "BlockIOReadBandwidth=%s %" PRIu64
"\n", a
->path
, a
->rbps
);
1661 fputs("BlockIOWriteBandwidth=\n", f
);
1662 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1663 if (a
->wbps
!= CGROUP_LIMIT_MAX
)
1664 fprintf(f
, "BlockIOWriteBandwidth=%s %" PRIu64
"\n", a
->path
, a
->wbps
);
1667 r
= memstream_finalize(&m
, &buf
, NULL
);
1671 unit_write_setting(u
, flags
, name
, buf
);
1676 } else if (streq(name
, "BlockIODeviceWeight")) {
1681 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1685 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1687 if (!path_is_normalized(path
))
1688 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1690 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight
) || weight
== CGROUP_BLKIO_WEIGHT_INVALID
)
1691 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "BlockIODeviceWeight= out of range");
1693 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1694 CGroupBlockIODeviceWeight
*a
= NULL
;
1696 LIST_FOREACH(device_weights
, b
, c
->blockio_device_weights
)
1697 if (path_equal(b
->path
, path
)) {
1703 a
= new0(CGroupBlockIODeviceWeight
, 1);
1707 a
->path
= strdup(path
);
1712 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, a
);
1721 r
= sd_bus_message_exit_container(message
);
1725 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1726 _cleanup_(memstream_done
) MemStream m
= {};
1727 _cleanup_free_
char *buf
= NULL
;
1731 while (c
->blockio_device_weights
)
1732 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
1734 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1736 f
= memstream_init(&m
);
1740 fputs("BlockIODeviceWeight=\n", f
);
1741 LIST_FOREACH(device_weights
, a
, c
->blockio_device_weights
)
1742 fprintf(f
, "BlockIODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1744 r
= memstream_finalize(&m
, &buf
, NULL
);
1748 unit_write_setting(u
, flags
, name
, buf
);
1753 } else if (streq(name
, "DevicePolicy")) {
1755 CGroupDevicePolicy p
;
1757 r
= sd_bus_message_read(message
, "s", &policy
);
1761 p
= cgroup_device_policy_from_string(policy
);
1765 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1766 c
->device_policy
= p
;
1767 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1768 unit_write_settingf(u
, flags
, name
, "DevicePolicy=%s", policy
);
1773 } else if (streq(name
, "DeviceAllow")) {
1774 const char *path
, *rwm
;
1777 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
1781 while ((r
= sd_bus_message_read(message
, "(ss)", &path
, &rwm
)) > 0) {
1783 if (!valid_device_allow_pattern(path
) || strpbrk(path
, WHITESPACE
))
1784 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires device node or pattern");
1788 else if (!in_charset(rwm
, "rwm"))
1789 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires combination of rwm flags");
1791 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1792 CGroupDeviceAllow
*a
= NULL
;
1794 LIST_FOREACH(device_allow
, b
, c
->device_allow
)
1795 if (path_equal(b
->path
, path
)) {
1801 a
= new0(CGroupDeviceAllow
, 1);
1805 a
->path
= strdup(path
);
1811 LIST_PREPEND(device_allow
, c
->device_allow
, a
);
1814 a
->r
= strchr(rwm
, 'r');
1815 a
->w
= strchr(rwm
, 'w');
1816 a
->m
= strchr(rwm
, 'm');
1824 r
= sd_bus_message_exit_container(message
);
1828 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1829 _cleanup_(memstream_done
) MemStream m
= {};
1830 _cleanup_free_
char *buf
= NULL
;
1834 while (c
->device_allow
)
1835 cgroup_context_free_device_allow(c
, c
->device_allow
);
1837 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1839 f
= memstream_init(&m
);
1843 fputs("DeviceAllow=\n", f
);
1844 LIST_FOREACH(device_allow
, a
, c
->device_allow
)
1845 fprintf(f
, "DeviceAllow=%s %s%s%s\n", a
->path
, a
->r
? "r" : "", a
->w
? "w" : "", a
->m
? "m" : "");
1847 r
= memstream_finalize(&m
, &buf
, NULL
);
1851 unit_write_setting(u
, flags
, name
, buf
);
1856 } else if (streq(name
, "IPAccounting")) {
1859 r
= sd_bus_message_read(message
, "b", &b
);
1863 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1864 c
->ip_accounting
= b
;
1866 unit_invalidate_cgroup_bpf(u
);
1867 unit_write_settingf(u
, flags
, name
, "IPAccounting=%s", yes_no(b
));
1872 } else if (STR_IN_SET(name
, "IPAddressAllow", "IPAddressDeny")) {
1873 _cleanup_set_free_ Set
*new_prefixes
= NULL
;
1876 r
= sd_bus_message_enter_container(message
, 'a', "(iayu)");
1886 r
= sd_bus_message_enter_container(message
, 'r', "iayu");
1892 r
= sd_bus_message_read(message
, "i", &family
);
1896 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1897 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects IPv4 or IPv6 addresses only.", name
);
1899 r
= sd_bus_message_read_array(message
, 'y', &ap
, &an
);
1903 if (an
!= FAMILY_ADDRESS_SIZE(family
))
1904 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1905 af_to_name(family
), FAMILY_ADDRESS_SIZE(family
), an
);
1907 r
= sd_bus_message_read(message
, "u", &prefixlen
);
1911 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
)*8)
1912 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
));
1914 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1915 struct in_addr_prefix prefix
= {
1917 .prefixlen
= prefixlen
,
1920 memcpy(&prefix
.address
, ap
, an
);
1922 r
= in_addr_prefix_add(&new_prefixes
, &prefix
);
1927 r
= sd_bus_message_exit_container(message
);
1934 r
= sd_bus_message_exit_container(message
);
1938 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1939 _cleanup_(memstream_done
) MemStream m
= {};
1940 _cleanup_free_
char *buf
= NULL
;
1945 unit_invalidate_cgroup_bpf(u
);
1947 f
= memstream_init(&m
);
1951 prefixes
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow
: &c
->ip_address_deny
;
1952 reduced
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow_reduced
: &c
->ip_address_deny_reduced
;
1956 *prefixes
= set_free(*prefixes
);
1962 r
= in_addr_prefixes_merge(prefixes
, new_prefixes
);
1966 const struct in_addr_prefix
*p
;
1967 SET_FOREACH(p
, new_prefixes
)
1968 fprintf(f
, "%s=%s\n", name
,
1969 IN_ADDR_PREFIX_TO_STRING(p
->family
, &p
->address
, p
->prefixlen
));
1972 r
= memstream_finalize(&m
, &buf
, NULL
);
1976 unit_write_setting(u
, flags
, name
, buf
);
1982 if (STR_IN_SET(name
, "ManagedOOMSwap", "ManagedOOMMemoryPressure")) {
1983 ManagedOOMMode
*cgroup_mode
= streq(name
, "ManagedOOMSwap") ? &c
->moom_swap
: &c
->moom_mem_pressure
;
1987 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
1988 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
1990 r
= sd_bus_message_read(message
, "s", &mode
);
1994 m
= managed_oom_mode_from_string(mode
);
1998 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2000 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, mode
);
2003 (void) manager_varlink_send_managed_oom_update(u
);
2007 if (streq(name
, "ManagedOOMMemoryPressureLimit")) {
2010 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
2011 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
2013 r
= sd_bus_message_read(message
, "u", &v
);
2017 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2018 c
->moom_mem_pressure_limit
= v
;
2019 unit_write_settingf(u
, flags
, name
,
2020 "ManagedOOMMemoryPressureLimit=" PERMYRIAD_AS_PERCENT_FORMAT_STR
,
2021 PERMYRIAD_AS_PERCENT_FORMAT_VAL(UINT32_SCALE_TO_PERMYRIAD(v
)));
2024 if (c
->moom_mem_pressure
== MANAGED_OOM_KILL
)
2025 (void) manager_varlink_send_managed_oom_update(u
);
2030 if (streq(name
, "ManagedOOMPreference")) {
2031 ManagedOOMPreference p
;
2034 r
= sd_bus_message_read(message
, "s", &pref
);
2038 p
= managed_oom_preference_from_string(pref
);
2042 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2043 c
->moom_preference
= p
;
2044 unit_write_settingf(u
, flags
, name
, "ManagedOOMPreference=%s", pref
);
2049 if (STR_IN_SET(name
, "SocketBindAllow", "SocketBindDeny")) {
2050 CGroupSocketBindItem
**list
;
2051 uint16_t nr_ports
, port_min
;
2053 int32_t family
, ip_protocol
;
2055 list
= streq(name
, "SocketBindAllow") ? &c
->socket_bind_allow
: &c
->socket_bind_deny
;
2057 r
= sd_bus_message_enter_container(message
, 'a', "(iiqq)");
2061 while ((r
= sd_bus_message_read(message
, "(iiqq)", &family
, &ip_protocol
, &nr_ports
, &port_min
)) > 0) {
2063 if (!IN_SET(family
, AF_UNSPEC
, AF_INET
, AF_INET6
))
2064 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects INET or INET6 family, if specified.", name
);
2066 if (!IN_SET(ip_protocol
, 0, IPPROTO_TCP
, IPPROTO_UDP
))
2067 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects TCP or UDP protocol, if specified.", name
);
2069 if (port_min
+ (uint32_t) nr_ports
> (1 << 16))
2070 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects maximum port value lesser than 65536.", name
);
2072 if (port_min
== 0 && nr_ports
!= 0)
2073 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects port range starting with positive value.", name
);
2075 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2076 _cleanup_free_ CGroupSocketBindItem
*item
= NULL
;
2078 item
= new(CGroupSocketBindItem
, 1);
2082 *item
= (CGroupSocketBindItem
) {
2083 .address_family
= family
,
2084 .ip_protocol
= ip_protocol
,
2085 .nr_ports
= nr_ports
,
2086 .port_min
= port_min
2089 LIST_PREPEND(socket_bind_items
, *list
, TAKE_PTR(item
));
2096 r
= sd_bus_message_exit_container(message
);
2100 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2101 _cleanup_(memstream_done
) MemStream m
= {};
2102 _cleanup_free_
char *buf
= NULL
;
2106 cgroup_context_remove_socket_bind(list
);
2108 if ((u
->manager
->cgroup_supported
& CGROUP_MASK_BPF_SOCKET_BIND
) == 0)
2110 "Unit %s configures source compiled BPF programs "
2111 "but the local system does not support that.\n"
2112 "Starting this unit will fail!", u
->id
);
2115 f
= memstream_init(&m
);
2120 fprintf(f
, "%s=\n", name
);
2122 LIST_FOREACH(socket_bind_items
, item
, *list
) {
2123 fprintf(f
, "%s=", name
);
2124 cgroup_context_dump_socket_bind_item(item
, f
);
2128 r
= memstream_finalize(&m
, &buf
, NULL
);
2132 unit_write_setting(u
, flags
, name
, buf
);
2137 if (streq(name
, "RestrictNetworkInterfaces")) {
2139 _cleanup_strv_free_
char **l
= NULL
;
2141 r
= sd_bus_message_enter_container(message
, 'r', "bas");
2145 r
= sd_bus_message_read(message
, "b", &is_allow_list
);
2149 r
= sd_bus_message_read_strv(message
, &l
);
2153 r
= sd_bus_message_exit_container(message
);
2157 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2158 _cleanup_free_
char *joined
= NULL
;
2160 if (strv_isempty(l
)) {
2161 c
->restrict_network_interfaces_is_allow_list
= false;
2162 c
->restrict_network_interfaces
= set_free(c
->restrict_network_interfaces
);
2164 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2168 if (set_isempty(c
->restrict_network_interfaces
))
2169 c
->restrict_network_interfaces_is_allow_list
= is_allow_list
;
2171 STRV_FOREACH(s
, l
) {
2172 if (!ifname_valid(*s
)) {
2173 log_full(LOG_WARNING
, "Invalid interface name, ignoring: %s", *s
);
2176 if (c
->restrict_network_interfaces_is_allow_list
!= (bool) is_allow_list
)
2177 free(set_remove(c
->restrict_network_interfaces
, *s
));
2179 r
= set_put_strdup(&c
->restrict_network_interfaces
, *s
);
2185 joined
= strv_join(l
, " ");
2189 unit_write_settingf(u
, flags
, name
, "%s=%s%s", name
, is_allow_list
? "" : "~", joined
);
2195 if (streq(name
, "DisableControllers") || (u
->transient
&& u
->load_state
== UNIT_STUB
))
2196 return bus_cgroup_set_transient_property(u
, c
, name
, message
, flags
, error
);