1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "bpf-firewall.h"
8 #include "bpf-foreign.h"
9 #include "bus-get-properties.h"
10 #include "cgroup-util.h"
12 #include "core-varlink.h"
13 #include "dbus-cgroup.h"
14 #include "dbus-util.h"
15 #include "errno-util.h"
18 #include "in-addr-prefix-util.h"
19 #include "ip-protocol-list.h"
20 #include "limits-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "percent-util.h"
24 #include "socket-util.h"
26 BUS_DEFINE_PROPERTY_GET(bus_property_get_tasks_max
, "t", TasksMax
, tasks_max_resolve
);
27 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_cgroup_pressure_watch
, cgroup_pressure_watch
, CGroupPressureWatch
);
29 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_cgroup_device_policy
, cgroup_device_policy
, CGroupDevicePolicy
);
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_mode
, managed_oom_mode
, ManagedOOMMode
);
31 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_preference
, managed_oom_preference
, ManagedOOMPreference
);
33 static int property_get_cgroup_mask(
36 const char *interface
,
38 sd_bus_message
*reply
,
40 sd_bus_error
*error
) {
42 CGroupMask
*mask
= userdata
;
48 r
= sd_bus_message_open_container(reply
, 'a', "s");
52 for (CGroupController ctrl
= 0; ctrl
< _CGROUP_CONTROLLER_MAX
; ctrl
++) {
53 if ((*mask
& CGROUP_CONTROLLER_TO_MASK(ctrl
)) == 0)
56 r
= sd_bus_message_append(reply
, "s", cgroup_controller_to_string(ctrl
));
61 return sd_bus_message_close_container(reply
);
64 static int property_get_delegate_controllers(
67 const char *interface
,
69 sd_bus_message
*reply
,
71 sd_bus_error
*error
) {
73 CGroupContext
*c
= ASSERT_PTR(userdata
);
79 return sd_bus_message_append(reply
, "as", 0);
81 return property_get_cgroup_mask(bus
, path
, interface
, property
, reply
, &c
->delegate_controllers
, error
);
84 static int property_get_cpuset(
87 const char *interface
,
89 sd_bus_message
*reply
,
91 sd_bus_error
*error
) {
93 CPUSet
*cpus
= ASSERT_PTR(userdata
);
94 _cleanup_free_
uint8_t *array
= NULL
;
100 (void) cpu_set_to_dbus(cpus
, &array
, &allocated
);
101 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
104 static int property_get_io_device_weight(
107 const char *interface
,
108 const char *property
,
109 sd_bus_message
*reply
,
111 sd_bus_error
*error
) {
113 CGroupContext
*c
= ASSERT_PTR(userdata
);
119 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
123 LIST_FOREACH(device_weights
, w
, c
->io_device_weights
) {
124 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
129 return sd_bus_message_close_container(reply
);
132 static int property_get_io_device_limits(
135 const char *interface
,
136 const char *property
,
137 sd_bus_message
*reply
,
139 sd_bus_error
*error
) {
141 CGroupContext
*c
= ASSERT_PTR(userdata
);
147 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
151 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
) {
152 CGroupIOLimitType type
;
154 type
= cgroup_io_limit_type_from_string(property
);
155 if (type
< 0 || l
->limits
[type
] == cgroup_io_limit_defaults
[type
])
158 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->limits
[type
]);
163 return sd_bus_message_close_container(reply
);
166 static int property_get_io_device_latency(
169 const char *interface
,
170 const char *property
,
171 sd_bus_message
*reply
,
173 sd_bus_error
*error
) {
175 CGroupContext
*c
= ASSERT_PTR(userdata
);
181 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
185 LIST_FOREACH(device_latencies
, l
, c
->io_device_latencies
) {
186 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->target_usec
);
191 return sd_bus_message_close_container(reply
);
194 static int property_get_blockio_device_weight(
197 const char *interface
,
198 const char *property
,
199 sd_bus_message
*reply
,
201 sd_bus_error
*error
) {
203 CGroupContext
*c
= ASSERT_PTR(userdata
);
209 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
213 LIST_FOREACH(device_weights
, w
, c
->blockio_device_weights
) {
214 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
219 return sd_bus_message_close_container(reply
);
222 static int property_get_blockio_device_bandwidths(
225 const char *interface
,
226 const char *property
,
227 sd_bus_message
*reply
,
229 sd_bus_error
*error
) {
231 CGroupContext
*c
= ASSERT_PTR(userdata
);
237 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
241 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
244 if (streq(property
, "BlockIOReadBandwidth"))
249 if (v
== CGROUP_LIMIT_MAX
)
252 r
= sd_bus_message_append(reply
, "(st)", b
->path
, v
);
257 return sd_bus_message_close_container(reply
);
260 static int property_get_device_allow(
263 const char *interface
,
264 const char *property
,
265 sd_bus_message
*reply
,
267 sd_bus_error
*error
) {
269 CGroupContext
*c
= ASSERT_PTR(userdata
);
275 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
279 LIST_FOREACH(device_allow
, a
, c
->device_allow
) {
292 r
= sd_bus_message_append(reply
, "(ss)", a
->path
, rwm
);
297 return sd_bus_message_close_container(reply
);
300 static int property_get_ip_address_access(
303 const char *interface
,
304 const char *property
,
305 sd_bus_message
*reply
,
307 sd_bus_error
*error
) {
309 Set
**prefixes
= ASSERT_PTR(userdata
);
310 struct in_addr_prefix
*i
;
313 r
= sd_bus_message_open_container(reply
, 'a', "(iayu)");
317 SET_FOREACH(i
, *prefixes
) {
319 r
= sd_bus_message_open_container(reply
, 'r', "iayu");
323 r
= sd_bus_message_append(reply
, "i", i
->family
);
327 r
= sd_bus_message_append_array(reply
, 'y', &i
->address
, FAMILY_ADDRESS_SIZE(i
->family
));
331 r
= sd_bus_message_append(reply
, "u", (uint32_t) i
->prefixlen
);
335 r
= sd_bus_message_close_container(reply
);
340 return sd_bus_message_close_container(reply
);
343 static int property_get_bpf_foreign_program(
346 const char *interface
,
347 const char *property
,
348 sd_bus_message
*reply
,
350 sd_bus_error
*error
) {
351 CGroupContext
*c
= userdata
;
354 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
358 LIST_FOREACH(programs
, p
, c
->bpf_foreign_programs
) {
359 const char *attach_type
= bpf_cgroup_attach_type_to_string(p
->attach_type
);
361 r
= sd_bus_message_append(reply
, "(ss)", attach_type
, p
->bpffs_path
);
366 return sd_bus_message_close_container(reply
);
369 static int property_get_socket_bind(
372 const char *interface
,
373 const char *property
,
374 sd_bus_message
*reply
,
376 sd_bus_error
*error
) {
378 CGroupSocketBindItem
**items
= ASSERT_PTR(userdata
);
381 r
= sd_bus_message_open_container(reply
, 'a', "(iiqq)");
385 LIST_FOREACH(socket_bind_items
, i
, *items
) {
386 r
= sd_bus_message_append(reply
, "(iiqq)", i
->address_family
, i
->ip_protocol
, i
->nr_ports
, i
->port_min
);
391 return sd_bus_message_close_container(reply
);
394 static int property_get_restrict_network_interfaces(
397 const char *interface
,
398 const char *property
,
399 sd_bus_message
*reply
,
401 sd_bus_error
*error
) {
403 CGroupContext
*c
= ASSERT_PTR(userdata
);
409 r
= sd_bus_message_open_container(reply
, 'r', "bas");
413 r
= sd_bus_message_append(reply
, "b", c
->restrict_network_interfaces_is_allow_list
);
417 r
= sd_bus_message_open_container(reply
, 'a', "s");
421 SET_FOREACH(iface
, c
->restrict_network_interfaces
) {
422 r
= sd_bus_message_append(reply
, "s", iface
);
427 r
= sd_bus_message_close_container(reply
);
431 return sd_bus_message_close_container(reply
);
434 const sd_bus_vtable bus_cgroup_vtable
[] = {
435 SD_BUS_VTABLE_START(0),
436 SD_BUS_PROPERTY("Delegate", "b", bus_property_get_bool
, offsetof(CGroupContext
, delegate
), 0),
437 SD_BUS_PROPERTY("DelegateControllers", "as", property_get_delegate_controllers
, 0, 0),
438 SD_BUS_PROPERTY("DelegateSubgroup", "s", NULL
, offsetof(CGroupContext
, delegate_subgroup
), 0),
439 SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, cpu_accounting
), 0),
440 SD_BUS_PROPERTY("CPUWeight", "t", NULL
, offsetof(CGroupContext
, cpu_weight
), 0),
441 SD_BUS_PROPERTY("StartupCPUWeight", "t", NULL
, offsetof(CGroupContext
, startup_cpu_weight
), 0),
442 SD_BUS_PROPERTY("CPUShares", "t", NULL
, offsetof(CGroupContext
, cpu_shares
), 0),
443 SD_BUS_PROPERTY("StartupCPUShares", "t", NULL
, offsetof(CGroupContext
, startup_cpu_shares
), 0),
444 SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_per_sec_usec
), 0),
445 SD_BUS_PROPERTY("CPUQuotaPeriodUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_period_usec
), 0),
446 SD_BUS_PROPERTY("AllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_cpus
), 0),
447 SD_BUS_PROPERTY("StartupAllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_cpus
), 0),
448 SD_BUS_PROPERTY("AllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_mems
), 0),
449 SD_BUS_PROPERTY("StartupAllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_mems
), 0),
450 SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, io_accounting
), 0),
451 SD_BUS_PROPERTY("IOWeight", "t", NULL
, offsetof(CGroupContext
, io_weight
), 0),
452 SD_BUS_PROPERTY("StartupIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_io_weight
), 0),
453 SD_BUS_PROPERTY("IODeviceWeight", "a(st)", property_get_io_device_weight
, 0, 0),
454 SD_BUS_PROPERTY("IOReadBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
455 SD_BUS_PROPERTY("IOWriteBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
456 SD_BUS_PROPERTY("IOReadIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
457 SD_BUS_PROPERTY("IOWriteIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
458 SD_BUS_PROPERTY("IODeviceLatencyTargetUSec", "a(st)", property_get_io_device_latency
, 0, 0),
459 SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, blockio_accounting
), 0),
460 SD_BUS_PROPERTY("BlockIOWeight", "t", NULL
, offsetof(CGroupContext
, blockio_weight
), 0),
461 SD_BUS_PROPERTY("StartupBlockIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_blockio_weight
), 0),
462 SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight
, 0, 0),
463 SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
464 SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
465 SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, memory_accounting
), 0),
466 SD_BUS_PROPERTY("DefaultMemoryLow", "t", NULL
, offsetof(CGroupContext
, default_memory_low
), 0),
467 SD_BUS_PROPERTY("DefaultStartupMemoryLow", "t", NULL
, offsetof(CGroupContext
, default_startup_memory_low
), 0),
468 SD_BUS_PROPERTY("DefaultMemoryMin", "t", NULL
, offsetof(CGroupContext
, default_memory_min
), 0),
469 SD_BUS_PROPERTY("MemoryMin", "t", NULL
, offsetof(CGroupContext
, memory_min
), 0),
470 SD_BUS_PROPERTY("MemoryLow", "t", NULL
, offsetof(CGroupContext
, memory_low
), 0),
471 SD_BUS_PROPERTY("StartupMemoryLow", "t", NULL
, offsetof(CGroupContext
, startup_memory_low
), 0),
472 SD_BUS_PROPERTY("MemoryHigh", "t", NULL
, offsetof(CGroupContext
, memory_high
), 0),
473 SD_BUS_PROPERTY("StartupMemoryHigh", "t", NULL
, offsetof(CGroupContext
, startup_memory_high
), 0),
474 SD_BUS_PROPERTY("MemoryMax", "t", NULL
, offsetof(CGroupContext
, memory_max
), 0),
475 SD_BUS_PROPERTY("StartupMemoryMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_max
), 0),
476 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL
, offsetof(CGroupContext
, memory_swap_max
), 0),
477 SD_BUS_PROPERTY("StartupMemorySwapMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_swap_max
), 0),
478 SD_BUS_PROPERTY("MemoryZSwapMax", "t", NULL
, offsetof(CGroupContext
, memory_zswap_max
), 0),
479 SD_BUS_PROPERTY("StartupMemoryZSwapMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_zswap_max
), 0),
480 SD_BUS_PROPERTY("MemoryLimit", "t", NULL
, offsetof(CGroupContext
, memory_limit
), 0),
481 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy
, offsetof(CGroupContext
, device_policy
), 0),
482 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow
, 0, 0),
483 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, tasks_accounting
), 0),
484 SD_BUS_PROPERTY("TasksMax", "t", bus_property_get_tasks_max
, offsetof(CGroupContext
, tasks_max
), 0),
485 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, ip_accounting
), 0),
486 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_allow
), 0),
487 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_deny
), 0),
488 SD_BUS_PROPERTY("IPIngressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_ingress
), 0),
489 SD_BUS_PROPERTY("IPEgressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_egress
), 0),
490 SD_BUS_PROPERTY("DisableControllers", "as", property_get_cgroup_mask
, offsetof(CGroupContext
, disable_controllers
), 0),
491 SD_BUS_PROPERTY("ManagedOOMSwap", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_swap
), 0),
492 SD_BUS_PROPERTY("ManagedOOMMemoryPressure", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_mem_pressure
), 0),
493 SD_BUS_PROPERTY("ManagedOOMMemoryPressureLimit", "u", NULL
, offsetof(CGroupContext
, moom_mem_pressure_limit
), 0),
494 SD_BUS_PROPERTY("ManagedOOMPreference", "s", property_get_managed_oom_preference
, offsetof(CGroupContext
, moom_preference
), 0),
495 SD_BUS_PROPERTY("BPFProgram", "a(ss)", property_get_bpf_foreign_program
, 0, 0),
496 SD_BUS_PROPERTY("SocketBindAllow", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_allow
), 0),
497 SD_BUS_PROPERTY("SocketBindDeny", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_deny
), 0),
498 SD_BUS_PROPERTY("RestrictNetworkInterfaces", "(bas)", property_get_restrict_network_interfaces
, 0, 0),
499 SD_BUS_PROPERTY("MemoryPressureWatch", "s", bus_property_get_cgroup_pressure_watch
, offsetof(CGroupContext
, memory_pressure_watch
), 0),
500 SD_BUS_PROPERTY("MemoryPressureThresholdUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, memory_pressure_threshold_usec
), 0),
504 static int bus_cgroup_set_transient_property(
508 sd_bus_message
*message
,
509 UnitWriteFlags flags
,
510 sd_bus_error
*error
) {
519 flags
|= UNIT_PRIVATE
;
521 if (streq(name
, "Delegate")) {
524 if (!UNIT_VTABLE(u
)->can_delegate
)
525 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
527 r
= sd_bus_message_read(message
, "b", &b
);
531 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
533 c
->delegate_controllers
= b
? _CGROUP_MASK_ALL
: 0;
535 unit_write_settingf(u
, flags
, name
, "Delegate=%s", yes_no(b
));
540 } else if (streq(name
, "DelegateSubgroup")) {
543 if (!UNIT_VTABLE(u
)->can_delegate
)
544 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
546 r
= sd_bus_message_read(message
, "s", &s
);
550 if (!isempty(s
) && cg_needs_escape(s
))
551 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid control group name: %s", s
);
553 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
555 c
->delegate_subgroup
= mfree(c
->delegate_subgroup
);
557 r
= free_and_strdup_warn(&c
->delegate_subgroup
, s
);
562 unit_write_settingf(u
, flags
, name
, "DelegateSubgroup=%s", s
);
567 } else if (STR_IN_SET(name
, "DelegateControllers", "DisableControllers")) {
570 if (streq(name
, "DelegateControllers") && !UNIT_VTABLE(u
)->can_delegate
)
571 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
573 r
= sd_bus_message_enter_container(message
, 'a', "s");
581 r
= sd_bus_message_read(message
, "s", &t
);
587 cc
= cgroup_controller_from_string(t
);
589 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown cgroup controller '%s'", t
);
591 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
594 r
= sd_bus_message_exit_container(message
);
598 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
599 _cleanup_free_
char *t
= NULL
;
601 r
= cg_mask_to_string(mask
, &t
);
605 if (streq(name
, "DelegateControllers")) {
609 c
->delegate_controllers
= 0;
611 c
->delegate_controllers
|= mask
;
613 unit_write_settingf(u
, flags
, name
, "Delegate=%s", strempty(t
));
615 } else if (streq(name
, "DisableControllers")) {
618 c
->disable_controllers
= 0;
620 c
->disable_controllers
|= mask
;
622 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, strempty(t
));
627 } else if (STR_IN_SET(name
, "IPIngressFilterPath", "IPEgressFilterPath")) {
631 filters
= streq(name
, "IPIngressFilterPath") ? &c
->ip_filters_ingress
: &c
->ip_filters_egress
;
632 r
= sd_bus_message_enter_container(message
, 'a', "s");
639 r
= sd_bus_message_read(message
, "s", &path
);
645 if (!path_is_normalized(path
) || !path_is_absolute(path
))
646 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects a normalized absolute path.", name
);
648 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && !strv_contains(*filters
, path
)) {
649 r
= strv_extend(filters
, path
);
655 r
= sd_bus_message_exit_container(message
);
659 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
660 _cleanup_free_
char *buf
= NULL
;
661 _cleanup_fclose_
FILE *f
= NULL
;
665 *filters
= strv_free(*filters
);
667 unit_invalidate_cgroup_bpf(u
);
668 f
= open_memstream_unlocked(&buf
, &size
);
675 STRV_FOREACH(entry
, *filters
)
676 fprintf(f
, "%s=%s\n", name
, *entry
);
678 r
= fflush_and_check(f
);
682 unit_write_setting(u
, flags
, name
, buf
);
685 r
= bpf_firewall_supported();
688 if (r
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
) {
689 static bool warned
= false;
691 log_full(warned
? LOG_DEBUG
: LOG_WARNING
,
692 "Transient unit %s configures an IP firewall with BPF, but the local system does not support BPF/cgroup firewalling with multiple filters.\n"
693 "Starting this unit will fail! (This warning is only shown for the first started transient unit using IP firewalling.)", u
->id
);
700 } else if (streq(name
, "BPFProgram")) {
704 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
708 while ((r
= sd_bus_message_read(message
, "(ss)", &a
, &p
)) > 0) {
709 int attach_type
= bpf_cgroup_attach_type_from_string(a
);
711 return sd_bus_error_setf(
713 SD_BUS_ERROR_INVALID_ARGS
,
714 "%s expects a valid BPF attach type, got '%s'.",
717 if (!path_is_normalized(p
) || !path_is_absolute(p
))
718 return sd_bus_error_setf(
720 SD_BUS_ERROR_INVALID_ARGS
,
721 "%s= expects a normalized absolute path.",
724 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
725 r
= cgroup_add_bpf_foreign_program(c
, attach_type
, p
);
734 r
= sd_bus_message_exit_container(message
);
738 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
739 _cleanup_free_
char *buf
= NULL
;
740 _cleanup_fclose_
FILE *f
= NULL
;
744 while (c
->bpf_foreign_programs
)
745 cgroup_context_remove_bpf_foreign_program(c
, c
->bpf_foreign_programs
);
747 f
= open_memstream_unlocked(&buf
, &size
);
754 LIST_FOREACH(programs
, fp
, c
->bpf_foreign_programs
)
755 fprintf(f
, "%s=%s:%s\n", name
,
756 bpf_cgroup_attach_type_to_string(fp
->attach_type
),
759 r
= fflush_and_check(f
);
763 unit_write_setting(u
, flags
, name
, buf
);
765 if (c
->bpf_foreign_programs
) {
766 r
= bpf_foreign_supported();
771 "Transient unit %s configures a BPF program pinned to BPF "
772 "filesystem, but the local system does not support that.\n"
773 "Starting this unit will fail!", u
->id
);
779 } else if (streq(name
, "MemoryPressureWatch")) {
780 CGroupPressureWatch p
;
783 r
= sd_bus_message_read(message
, "s", &t
);
788 p
= _CGROUP_PRESSURE_WATCH_INVALID
;
790 p
= cgroup_pressure_watch_from_string(t
);
795 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
796 c
->memory_pressure_watch
= p
;
797 unit_write_settingf(u
, flags
, name
, "MemoryPressureWatch=%s", strempty(cgroup_pressure_watch_to_string(p
)));
802 } else if (streq(name
, "MemoryPressureThresholdUSec")) {
805 r
= sd_bus_message_read(message
, "t", &t
);
809 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
810 c
->memory_pressure_threshold_usec
= t
;
813 unit_write_setting(u
, flags
, name
, "MemoryPressureThresholdUSec=");
815 unit_write_settingf(u
, flags
, name
, "MemoryPressureThresholdUSec=%" PRIu64
, t
);
824 static int bus_cgroup_set_boolean(
829 sd_bus_message
*message
,
830 UnitWriteFlags flags
,
831 sd_bus_error
*error
) {
837 r
= sd_bus_message_read(message
, "b", &b
);
841 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
843 unit_invalidate_cgroup(u
, mask
);
844 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, yes_no(b
));
850 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
851 static int bus_cgroup_set_##function( \
855 sd_bus_message *message, \
856 UnitWriteFlags flags, \
857 sd_bus_error *error) { \
864 r = sd_bus_message_read(message, "t", &v); \
869 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
870 "Value specified in %s is out of range", name); \
872 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
874 unit_invalidate_cgroup(u, mask); \
877 unit_write_settingf(u, flags, name, \
880 unit_write_settingf(u, flags, name, \
881 "%s=%" PRIu64, name, v); \
887 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
888 static int bus_cgroup_set_##function( \
892 sd_bus_message *message, \
893 UnitWriteFlags flags, \
894 sd_bus_error *error) { \
901 r = sd_bus_message_read(message, "t", &v); \
906 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
907 "Value specified in %s is out of range", name); \
909 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
911 unit_invalidate_cgroup(u, mask); \
913 if (v == CGROUP_LIMIT_MAX) \
914 unit_write_settingf(u, flags, name, \
915 "%s=infinity", name); \
917 unit_write_settingf(u, flags, name, \
918 "%s=%" PRIu64, name, v); \
923 static int bus_cgroup_set_##function##_scale( \
927 sd_bus_message *message, \
928 UnitWriteFlags flags, \
929 sd_bus_error *error) { \
937 r = sd_bus_message_read(message, "u", &raw); \
941 v = scale(raw, UINT32_MAX); \
942 if (v < minimum || v >= UINT64_MAX) \
943 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
944 "Value specified in %s is out of range", name); \
946 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
948 unit_invalidate_cgroup(u, mask); \
950 /* Prepare to chop off suffix */ \
951 assert_se(endswith(name, "Scale")); \
953 int scaled = UINT32_SCALE_TO_PERMYRIAD(raw); \
954 unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR, \
955 (int)(strlen(name) - strlen("Scale")), name, \
956 PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled)); \
962 DISABLE_WARNING_TYPE_LIMITS
;
963 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares
, CGROUP_MASK_CPU
, CGROUP_CPU_SHARES_IS_OK
, CGROUP_CPU_SHARES_INVALID
);
964 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight
, CGROUP_MASK_IO
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
965 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight
, CGROUP_MASK_BLKIO
, CGROUP_BLKIO_WEIGHT_IS_OK
, CGROUP_BLKIO_WEIGHT_INVALID
);
966 BUS_DEFINE_SET_CGROUP_LIMIT(memory
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 1);
967 BUS_DEFINE_SET_CGROUP_LIMIT(memory_protection
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
968 BUS_DEFINE_SET_CGROUP_LIMIT(swap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
969 BUS_DEFINE_SET_CGROUP_LIMIT(zswap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
972 static int bus_cgroup_set_cpu_weight(
976 sd_bus_message
*message
,
977 UnitWriteFlags flags
,
978 sd_bus_error
*error
) {
982 r
= sd_bus_message_read(message
, "t", &v
);
985 if (!CGROUP_WEIGHT_IS_OK(v
) && v
!= CGROUP_WEIGHT_IDLE
)
986 return sd_bus_error_setf(
987 error
, SD_BUS_ERROR_INVALID_ARGS
, "Value specified in %s is out of range", name
);
988 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
990 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
991 if (v
== CGROUP_WEIGHT_INVALID
)
992 unit_write_settingf(u
, flags
, name
, "%s=", name
);
993 else if (v
== CGROUP_WEIGHT_IDLE
)
994 unit_write_settingf(u
, flags
, name
, "%s=idle", name
);
996 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu64
, name
, v
);
1001 static int bus_cgroup_set_tasks_max(
1005 sd_bus_message
*message
,
1006 UnitWriteFlags flags
,
1007 sd_bus_error
*error
) {
1014 r
= sd_bus_message_read(message
, "t", &v
);
1019 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1020 "Value specified in %s is out of range", name
);
1022 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1023 *p
= (TasksMax
) { .value
= v
, .scale
= 0 }; /* When .scale==0, .value is the absolute value */
1024 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
1026 if (v
== CGROUP_LIMIT_MAX
)
1027 unit_write_settingf(u
, flags
, name
,
1028 "%s=infinity", name
);
1030 unit_write_settingf(u
, flags
, name
,
1031 "%s=%" PRIu64
, name
, v
);
1037 static int bus_cgroup_set_tasks_max_scale(
1041 sd_bus_message
*message
,
1042 UnitWriteFlags flags
,
1043 sd_bus_error
*error
) {
1050 r
= sd_bus_message_read(message
, "u", &v
);
1054 if (v
< 1 || v
>= UINT32_MAX
)
1055 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1056 "Value specified in %s is out of range", name
);
1058 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1059 *p
= (TasksMax
) { v
, UINT32_MAX
}; /* .scale is not 0, so this is interpreted as v/UINT32_MAX. */
1060 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
1062 uint32_t scaled
= DIV_ROUND_UP((uint64_t) v
* 100U, (uint64_t) UINT32_MAX
);
1063 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu32
".%" PRIu32
"%%", "TasksMax",
1064 scaled
/ 10, scaled
% 10);
1070 int bus_cgroup_set_property(
1074 sd_bus_message
*message
,
1075 UnitWriteFlags flags
,
1076 sd_bus_error
*error
) {
1078 CGroupIOLimitType iol_type
;
1086 flags
|= UNIT_PRIVATE
;
1088 if (streq(name
, "CPUAccounting"))
1089 return bus_cgroup_set_boolean(u
, name
, &c
->cpu_accounting
, get_cpu_accounting_mask(), message
, flags
, error
);
1091 if (streq(name
, "CPUWeight"))
1092 return bus_cgroup_set_cpu_weight(u
, name
, &c
->cpu_weight
, message
, flags
, error
);
1094 if (streq(name
, "StartupCPUWeight"))
1095 return bus_cgroup_set_cpu_weight(u
, name
, &c
->startup_cpu_weight
, message
, flags
, error
);
1097 if (streq(name
, "CPUShares"))
1098 return bus_cgroup_set_cpu_shares(u
, name
, &c
->cpu_shares
, message
, flags
, error
);
1100 if (streq(name
, "StartupCPUShares"))
1101 return bus_cgroup_set_cpu_shares(u
, name
, &c
->startup_cpu_shares
, message
, flags
, error
);
1103 if (streq(name
, "IOAccounting"))
1104 return bus_cgroup_set_boolean(u
, name
, &c
->io_accounting
, CGROUP_MASK_IO
, message
, flags
, error
);
1106 if (streq(name
, "IOWeight"))
1107 return bus_cgroup_set_io_weight(u
, name
, &c
->io_weight
, message
, flags
, error
);
1109 if (streq(name
, "StartupIOWeight"))
1110 return bus_cgroup_set_io_weight(u
, name
, &c
->startup_io_weight
, message
, flags
, error
);
1112 if (streq(name
, "BlockIOAccounting"))
1113 return bus_cgroup_set_boolean(u
, name
, &c
->blockio_accounting
, CGROUP_MASK_BLKIO
, message
, flags
, error
);
1115 if (streq(name
, "BlockIOWeight"))
1116 return bus_cgroup_set_blockio_weight(u
, name
, &c
->blockio_weight
, message
, flags
, error
);
1118 if (streq(name
, "StartupBlockIOWeight"))
1119 return bus_cgroup_set_blockio_weight(u
, name
, &c
->startup_blockio_weight
, message
, flags
, error
);
1121 if (streq(name
, "MemoryAccounting"))
1122 return bus_cgroup_set_boolean(u
, name
, &c
->memory_accounting
, CGROUP_MASK_MEMORY
, message
, flags
, error
);
1124 if (streq(name
, "MemoryMin")) {
1125 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_min
, message
, flags
, error
);
1127 c
->memory_min_set
= true;
1131 if (streq(name
, "MemoryLow")) {
1132 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_low
, message
, flags
, error
);
1134 c
->memory_low_set
= true;
1138 if (streq(name
, "StartupMemoryLow")) {
1139 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->startup_memory_low
, message
, flags
, error
);
1141 c
->startup_memory_low_set
= true;
1145 if (streq(name
, "DefaultMemoryMin")) {
1146 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1148 c
->default_memory_min_set
= true;
1152 if (streq(name
, "DefaultMemoryLow")) {
1153 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1155 c
->default_memory_low_set
= true;
1159 if (streq(name
, "DefaultStartupMemoryLow")) {
1160 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_startup_memory_low
, message
, flags
, error
);
1162 c
->default_startup_memory_low_set
= true;
1166 if (streq(name
, "MemoryHigh"))
1167 return bus_cgroup_set_memory(u
, name
, &c
->memory_high
, message
, flags
, error
);
1169 if (streq(name
, "StartupMemoryHigh")) {
1170 r
= bus_cgroup_set_memory(u
, name
, &c
->startup_memory_high
, message
, flags
, error
);
1172 c
->startup_memory_high_set
= true;
1176 if (streq(name
, "MemorySwapMax"))
1177 return bus_cgroup_set_swap(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1179 if (streq(name
, "StartupMemorySwapMax")) {
1180 r
= bus_cgroup_set_swap(u
, name
, &c
->startup_memory_swap_max
, message
, flags
, error
);
1182 c
->startup_memory_swap_max_set
= true;
1186 if (streq(name
, "MemoryZSwapMax"))
1187 return bus_cgroup_set_zswap(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1189 if (streq(name
, "StartupMemoryZSwapMax")) {
1190 r
= bus_cgroup_set_zswap(u
, name
, &c
->startup_memory_zswap_max
, message
, flags
, error
);
1192 c
->startup_memory_zswap_max_set
= true;
1196 if (streq(name
, "MemoryMax"))
1197 return bus_cgroup_set_memory(u
, name
, &c
->memory_max
, message
, flags
, error
);
1199 if (streq(name
, "StartupMemoryMax")) {
1200 r
= bus_cgroup_set_memory(u
, name
, &c
->startup_memory_max
, message
, flags
, error
);
1202 c
->startup_memory_max_set
= true;
1206 if (streq(name
, "MemoryLimit"))
1207 return bus_cgroup_set_memory(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1209 if (streq(name
, "MemoryMinScale")) {
1210 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_min
, message
, flags
, error
);
1212 c
->memory_min_set
= true;
1216 if (streq(name
, "MemoryLowScale")) {
1217 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_low
, message
, flags
, error
);
1219 c
->memory_low_set
= true;
1223 if (streq(name
, "DefaultMemoryMinScale")) {
1224 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1226 c
->default_memory_min_set
= true;
1230 if (streq(name
, "DefaultMemoryLowScale")) {
1231 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1233 c
->default_memory_low_set
= true;
1237 if (streq(name
, "MemoryHighScale"))
1238 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_high
, message
, flags
, error
);
1240 if (streq(name
, "MemorySwapMaxScale"))
1241 return bus_cgroup_set_swap_scale(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1243 if (streq(name
, "MemoryZSwapMaxScale"))
1244 return bus_cgroup_set_zswap_scale(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1246 if (streq(name
, "MemoryMaxScale"))
1247 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_max
, message
, flags
, error
);
1249 if (streq(name
, "MemoryLimitScale"))
1250 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1252 if (streq(name
, "TasksAccounting"))
1253 return bus_cgroup_set_boolean(u
, name
, &c
->tasks_accounting
, CGROUP_MASK_PIDS
, message
, flags
, error
);
1255 if (streq(name
, "TasksMax"))
1256 return bus_cgroup_set_tasks_max(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1258 if (streq(name
, "TasksMaxScale"))
1259 return bus_cgroup_set_tasks_max_scale(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1261 if (streq(name
, "CPUQuotaPerSecUSec")) {
1264 r
= sd_bus_message_read(message
, "t", &u64
);
1269 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "CPUQuotaPerSecUSec= value out of range");
1271 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1272 c
->cpu_quota_per_sec_usec
= u64
;
1273 u
->warned_clamping_cpu_quota_period
= false;
1274 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1276 if (c
->cpu_quota_per_sec_usec
== USEC_INFINITY
)
1277 unit_write_setting(u
, flags
, "CPUQuota", "CPUQuota=");
1279 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
1281 unit_write_settingf(u
, flags
, "CPUQuota",
1283 (double) (c
->cpu_quota_per_sec_usec
/ 10000));
1288 } else if (streq(name
, "CPUQuotaPeriodUSec")) {
1291 r
= sd_bus_message_read(message
, "t", &u64
);
1295 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1296 c
->cpu_quota_period_usec
= u64
;
1297 u
->warned_clamping_cpu_quota_period
= false;
1298 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1299 if (c
->cpu_quota_period_usec
== USEC_INFINITY
)
1300 unit_write_setting(u
, flags
, "CPUQuotaPeriodSec", "CPUQuotaPeriodSec=");
1302 unit_write_settingf(u
, flags
, "CPUQuotaPeriodSec",
1303 "CPUQuotaPeriodSec=%s",
1304 FORMAT_TIMESPAN(c
->cpu_quota_period_usec
, 1));
1309 } else if (STR_IN_SET(name
, "AllowedCPUs", "StartupAllowedCPUs", "AllowedMemoryNodes", "StartupAllowedMemoryNodes")) {
1312 _cleanup_(cpu_set_reset
) CPUSet new_set
= {};
1314 r
= sd_bus_message_read_array(message
, 'y', &a
, &n
);
1318 r
= cpu_set_from_dbus(a
, n
, &new_set
);
1322 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1323 _cleanup_free_
char *setstr
= NULL
;
1326 setstr
= cpu_set_to_range_string(&new_set
);
1330 if (streq(name
, "AllowedCPUs"))
1331 set
= &c
->cpuset_cpus
;
1332 else if (streq(name
, "StartupAllowedCPUs"))
1333 set
= &c
->startup_cpuset_cpus
;
1334 else if (streq(name
, "AllowedMemoryNodes"))
1335 set
= &c
->cpuset_mems
;
1336 else if (streq(name
, "StartupAllowedMemoryNodes"))
1337 set
= &c
->startup_cpuset_mems
;
1343 new_set
= (CPUSet
) {};
1345 unit_invalidate_cgroup(u
, CGROUP_MASK_CPUSET
);
1346 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, setstr
);
1351 } else if ((iol_type
= cgroup_io_limit_type_from_string(name
)) >= 0) {
1356 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1360 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1362 if (!path_is_normalized(path
))
1363 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1365 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1366 CGroupIODeviceLimit
*a
= NULL
;
1368 LIST_FOREACH(device_limits
, b
, c
->io_device_limits
)
1369 if (path_equal(path
, b
->path
)) {
1375 CGroupIOLimitType type
;
1377 a
= new0(CGroupIODeviceLimit
, 1);
1381 a
->path
= strdup(path
);
1387 for (type
= 0; type
< _CGROUP_IO_LIMIT_TYPE_MAX
; type
++)
1388 a
->limits
[type
] = cgroup_io_limit_defaults
[type
];
1390 LIST_PREPEND(device_limits
, c
->io_device_limits
, a
);
1393 a
->limits
[iol_type
] = u64
;
1401 r
= sd_bus_message_exit_container(message
);
1405 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1406 _cleanup_free_
char *buf
= NULL
;
1407 _cleanup_fclose_
FILE *f
= NULL
;
1411 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1412 a
->limits
[iol_type
] = cgroup_io_limit_defaults
[iol_type
];
1414 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1416 f
= open_memstream_unlocked(&buf
, &size
);
1420 fprintf(f
, "%s=\n", name
);
1421 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1422 if (a
->limits
[iol_type
] != cgroup_io_limit_defaults
[iol_type
])
1423 fprintf(f
, "%s=%s %" PRIu64
"\n", name
, a
->path
, a
->limits
[iol_type
]);
1425 r
= fflush_and_check(f
);
1428 unit_write_setting(u
, flags
, name
, buf
);
1433 } else if (streq(name
, "IODeviceWeight")) {
1438 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1442 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1444 if (!path_is_normalized(path
))
1445 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1447 if (!CGROUP_WEIGHT_IS_OK(weight
) || weight
== CGROUP_WEIGHT_INVALID
)
1448 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "IODeviceWeight= value out of range");
1450 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1451 CGroupIODeviceWeight
*a
= NULL
;
1453 LIST_FOREACH(device_weights
, b
, c
->io_device_weights
)
1454 if (path_equal(b
->path
, path
)) {
1460 a
= new0(CGroupIODeviceWeight
, 1);
1464 a
->path
= strdup(path
);
1469 LIST_PREPEND(device_weights
, c
->io_device_weights
, a
);
1478 r
= sd_bus_message_exit_container(message
);
1482 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1483 _cleanup_free_
char *buf
= NULL
;
1484 _cleanup_fclose_
FILE *f
= NULL
;
1488 while (c
->io_device_weights
)
1489 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
1491 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1493 f
= open_memstream_unlocked(&buf
, &size
);
1497 fputs("IODeviceWeight=\n", f
);
1498 LIST_FOREACH(device_weights
, a
, c
->io_device_weights
)
1499 fprintf(f
, "IODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1501 r
= fflush_and_check(f
);
1504 unit_write_setting(u
, flags
, name
, buf
);
1509 } else if (streq(name
, "IODeviceLatencyTargetUSec")) {
1514 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1518 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &target
)) > 0) {
1520 if (!path_is_normalized(path
))
1521 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1523 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1524 CGroupIODeviceLatency
*a
= NULL
;
1526 LIST_FOREACH(device_latencies
, b
, c
->io_device_latencies
)
1527 if (path_equal(b
->path
, path
)) {
1533 a
= new0(CGroupIODeviceLatency
, 1);
1537 a
->path
= strdup(path
);
1542 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, a
);
1545 a
->target_usec
= target
;
1551 r
= sd_bus_message_exit_container(message
);
1555 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1556 _cleanup_free_
char *buf
= NULL
;
1557 _cleanup_fclose_
FILE *f
= NULL
;
1561 while (c
->io_device_latencies
)
1562 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
1564 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1566 f
= open_memstream_unlocked(&buf
, &size
);
1570 fputs("IODeviceLatencyTargetSec=\n", f
);
1571 LIST_FOREACH(device_latencies
, a
, c
->io_device_latencies
)
1572 fprintf(f
, "IODeviceLatencyTargetSec=%s %s\n",
1573 a
->path
, FORMAT_TIMESPAN(a
->target_usec
, 1));
1575 r
= fflush_and_check(f
);
1578 unit_write_setting(u
, flags
, name
, buf
);
1583 } else if (STR_IN_SET(name
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
1589 read
= streq(name
, "BlockIOReadBandwidth");
1591 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1595 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1597 if (!path_is_normalized(path
))
1598 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1600 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1601 CGroupBlockIODeviceBandwidth
*a
= NULL
;
1603 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
)
1604 if (path_equal(path
, b
->path
)) {
1610 a
= new0(CGroupBlockIODeviceBandwidth
, 1);
1614 a
->rbps
= CGROUP_LIMIT_MAX
;
1615 a
->wbps
= CGROUP_LIMIT_MAX
;
1616 a
->path
= strdup(path
);
1622 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, a
);
1636 r
= sd_bus_message_exit_container(message
);
1640 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1641 _cleanup_free_
char *buf
= NULL
;
1642 _cleanup_fclose_
FILE *f
= NULL
;
1646 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
) {
1648 a
->rbps
= CGROUP_LIMIT_MAX
;
1650 a
->wbps
= CGROUP_LIMIT_MAX
;
1653 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1655 f
= open_memstream_unlocked(&buf
, &size
);
1660 fputs("BlockIOReadBandwidth=\n", f
);
1661 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1662 if (a
->rbps
!= CGROUP_LIMIT_MAX
)
1663 fprintf(f
, "BlockIOReadBandwidth=%s %" PRIu64
"\n", a
->path
, a
->rbps
);
1665 fputs("BlockIOWriteBandwidth=\n", f
);
1666 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1667 if (a
->wbps
!= CGROUP_LIMIT_MAX
)
1668 fprintf(f
, "BlockIOWriteBandwidth=%s %" PRIu64
"\n", a
->path
, a
->wbps
);
1671 r
= fflush_and_check(f
);
1675 unit_write_setting(u
, flags
, name
, buf
);
1680 } else if (streq(name
, "BlockIODeviceWeight")) {
1685 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1689 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1691 if (!path_is_normalized(path
))
1692 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1694 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight
) || weight
== CGROUP_BLKIO_WEIGHT_INVALID
)
1695 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "BlockIODeviceWeight= out of range");
1697 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1698 CGroupBlockIODeviceWeight
*a
= NULL
;
1700 LIST_FOREACH(device_weights
, b
, c
->blockio_device_weights
)
1701 if (path_equal(b
->path
, path
)) {
1707 a
= new0(CGroupBlockIODeviceWeight
, 1);
1711 a
->path
= strdup(path
);
1716 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, a
);
1725 r
= sd_bus_message_exit_container(message
);
1729 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1730 _cleanup_free_
char *buf
= NULL
;
1731 _cleanup_fclose_
FILE *f
= NULL
;
1735 while (c
->blockio_device_weights
)
1736 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
1738 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1740 f
= open_memstream_unlocked(&buf
, &size
);
1744 fputs("BlockIODeviceWeight=\n", f
);
1745 LIST_FOREACH(device_weights
, a
, c
->blockio_device_weights
)
1746 fprintf(f
, "BlockIODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1748 r
= fflush_and_check(f
);
1752 unit_write_setting(u
, flags
, name
, buf
);
1757 } else if (streq(name
, "DevicePolicy")) {
1759 CGroupDevicePolicy p
;
1761 r
= sd_bus_message_read(message
, "s", &policy
);
1765 p
= cgroup_device_policy_from_string(policy
);
1769 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1770 c
->device_policy
= p
;
1771 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1772 unit_write_settingf(u
, flags
, name
, "DevicePolicy=%s", policy
);
1777 } else if (streq(name
, "DeviceAllow")) {
1778 const char *path
, *rwm
;
1781 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
1785 while ((r
= sd_bus_message_read(message
, "(ss)", &path
, &rwm
)) > 0) {
1787 if (!valid_device_allow_pattern(path
) || strpbrk(path
, WHITESPACE
))
1788 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires device node or pattern");
1792 else if (!in_charset(rwm
, "rwm"))
1793 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires combination of rwm flags");
1795 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1796 CGroupDeviceAllow
*a
= NULL
;
1798 LIST_FOREACH(device_allow
, b
, c
->device_allow
)
1799 if (path_equal(b
->path
, path
)) {
1805 a
= new0(CGroupDeviceAllow
, 1);
1809 a
->path
= strdup(path
);
1815 LIST_PREPEND(device_allow
, c
->device_allow
, a
);
1818 a
->r
= strchr(rwm
, 'r');
1819 a
->w
= strchr(rwm
, 'w');
1820 a
->m
= strchr(rwm
, 'm');
1828 r
= sd_bus_message_exit_container(message
);
1832 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1833 _cleanup_free_
char *buf
= NULL
;
1834 _cleanup_fclose_
FILE *f
= NULL
;
1838 while (c
->device_allow
)
1839 cgroup_context_free_device_allow(c
, c
->device_allow
);
1841 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1843 f
= open_memstream_unlocked(&buf
, &size
);
1847 fputs("DeviceAllow=\n", f
);
1848 LIST_FOREACH(device_allow
, a
, c
->device_allow
)
1849 fprintf(f
, "DeviceAllow=%s %s%s%s\n", a
->path
, a
->r
? "r" : "", a
->w
? "w" : "", a
->m
? "m" : "");
1851 r
= fflush_and_check(f
);
1854 unit_write_setting(u
, flags
, name
, buf
);
1859 } else if (streq(name
, "IPAccounting")) {
1862 r
= sd_bus_message_read(message
, "b", &b
);
1866 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1867 c
->ip_accounting
= b
;
1869 unit_invalidate_cgroup_bpf(u
);
1870 unit_write_settingf(u
, flags
, name
, "IPAccounting=%s", yes_no(b
));
1875 } else if (STR_IN_SET(name
, "IPAddressAllow", "IPAddressDeny")) {
1876 _cleanup_set_free_ Set
*new_prefixes
= NULL
;
1879 r
= sd_bus_message_enter_container(message
, 'a', "(iayu)");
1889 r
= sd_bus_message_enter_container(message
, 'r', "iayu");
1895 r
= sd_bus_message_read(message
, "i", &family
);
1899 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1900 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects IPv4 or IPv6 addresses only.", name
);
1902 r
= sd_bus_message_read_array(message
, 'y', &ap
, &an
);
1906 if (an
!= FAMILY_ADDRESS_SIZE(family
))
1907 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1908 af_to_name(family
), FAMILY_ADDRESS_SIZE(family
), an
);
1910 r
= sd_bus_message_read(message
, "u", &prefixlen
);
1914 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
)*8)
1915 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
));
1917 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1918 struct in_addr_prefix prefix
= {
1920 .prefixlen
= prefixlen
,
1923 memcpy(&prefix
.address
, ap
, an
);
1925 r
= in_addr_prefix_add(&new_prefixes
, &prefix
);
1930 r
= sd_bus_message_exit_container(message
);
1937 r
= sd_bus_message_exit_container(message
);
1941 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1942 _cleanup_free_
char *buf
= NULL
;
1943 _cleanup_fclose_
FILE *f
= NULL
;
1948 unit_invalidate_cgroup_bpf(u
);
1949 f
= open_memstream_unlocked(&buf
, &size
);
1953 prefixes
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow
: &c
->ip_address_deny
;
1954 reduced
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow_reduced
: &c
->ip_address_deny_reduced
;
1958 *prefixes
= set_free(*prefixes
);
1964 r
= in_addr_prefixes_merge(prefixes
, new_prefixes
);
1968 const struct in_addr_prefix
*p
;
1969 SET_FOREACH(p
, new_prefixes
)
1970 fprintf(f
, "%s=%s\n", name
,
1971 IN_ADDR_PREFIX_TO_STRING(p
->family
, &p
->address
, p
->prefixlen
));
1974 r
= fflush_and_check(f
);
1978 unit_write_setting(u
, flags
, name
, buf
);
1984 if (STR_IN_SET(name
, "ManagedOOMSwap", "ManagedOOMMemoryPressure")) {
1985 ManagedOOMMode
*cgroup_mode
= streq(name
, "ManagedOOMSwap") ? &c
->moom_swap
: &c
->moom_mem_pressure
;
1989 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
1990 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
1992 r
= sd_bus_message_read(message
, "s", &mode
);
1996 m
= managed_oom_mode_from_string(mode
);
2000 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2002 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, mode
);
2005 (void) manager_varlink_send_managed_oom_update(u
);
2009 if (streq(name
, "ManagedOOMMemoryPressureLimit")) {
2012 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
2013 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
2015 r
= sd_bus_message_read(message
, "u", &v
);
2019 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2020 c
->moom_mem_pressure_limit
= v
;
2021 unit_write_settingf(u
, flags
, name
,
2022 "ManagedOOMMemoryPressureLimit=" PERMYRIAD_AS_PERCENT_FORMAT_STR
,
2023 PERMYRIAD_AS_PERCENT_FORMAT_VAL(UINT32_SCALE_TO_PERMYRIAD(v
)));
2026 if (c
->moom_mem_pressure
== MANAGED_OOM_KILL
)
2027 (void) manager_varlink_send_managed_oom_update(u
);
2032 if (streq(name
, "ManagedOOMPreference")) {
2033 ManagedOOMPreference p
;
2036 r
= sd_bus_message_read(message
, "s", &pref
);
2040 p
= managed_oom_preference_from_string(pref
);
2044 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2045 c
->moom_preference
= p
;
2046 unit_write_settingf(u
, flags
, name
, "ManagedOOMPreference=%s", pref
);
2051 if (STR_IN_SET(name
, "SocketBindAllow", "SocketBindDeny")) {
2052 CGroupSocketBindItem
**list
;
2053 uint16_t nr_ports
, port_min
;
2055 int32_t family
, ip_protocol
;
2057 list
= streq(name
, "SocketBindAllow") ? &c
->socket_bind_allow
: &c
->socket_bind_deny
;
2059 r
= sd_bus_message_enter_container(message
, 'a', "(iiqq)");
2063 while ((r
= sd_bus_message_read(message
, "(iiqq)", &family
, &ip_protocol
, &nr_ports
, &port_min
)) > 0) {
2065 if (!IN_SET(family
, AF_UNSPEC
, AF_INET
, AF_INET6
))
2066 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects INET or INET6 family, if specified.", name
);
2068 if (!IN_SET(ip_protocol
, 0, IPPROTO_TCP
, IPPROTO_UDP
))
2069 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects TCP or UDP protocol, if specified.", name
);
2071 if (port_min
+ (uint32_t) nr_ports
> (1 << 16))
2072 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects maximum port value lesser than 65536.", name
);
2074 if (port_min
== 0 && nr_ports
!= 0)
2075 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects port range starting with positive value.", name
);
2077 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2078 _cleanup_free_ CGroupSocketBindItem
*item
= NULL
;
2080 item
= new(CGroupSocketBindItem
, 1);
2084 *item
= (CGroupSocketBindItem
) {
2085 .address_family
= family
,
2086 .ip_protocol
= ip_protocol
,
2087 .nr_ports
= nr_ports
,
2088 .port_min
= port_min
2091 LIST_PREPEND(socket_bind_items
, *list
, TAKE_PTR(item
));
2098 r
= sd_bus_message_exit_container(message
);
2102 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2103 _cleanup_free_
char *buf
= NULL
;
2104 _cleanup_fclose_
FILE *f
= NULL
;
2108 cgroup_context_remove_socket_bind(list
);
2110 if ((u
->manager
->cgroup_supported
& CGROUP_MASK_BPF_SOCKET_BIND
) == 0)
2112 "Unit %s configures source compiled BPF programs "
2113 "but the local system does not support that.\n"
2114 "Starting this unit will fail!", u
->id
);
2117 f
= open_memstream_unlocked(&buf
, &size
);
2121 fprintf(f
, "%s:", name
);
2123 LIST_FOREACH(socket_bind_items
, item
, *list
)
2124 cgroup_context_dump_socket_bind_item(item
, f
);
2128 r
= fflush_and_check(f
);
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
);