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("CPUAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, cpu_accounting
), 0),
439 SD_BUS_PROPERTY("CPUWeight", "t", NULL
, offsetof(CGroupContext
, cpu_weight
), 0),
440 SD_BUS_PROPERTY("StartupCPUWeight", "t", NULL
, offsetof(CGroupContext
, startup_cpu_weight
), 0),
441 SD_BUS_PROPERTY("CPUShares", "t", NULL
, offsetof(CGroupContext
, cpu_shares
), 0),
442 SD_BUS_PROPERTY("StartupCPUShares", "t", NULL
, offsetof(CGroupContext
, startup_cpu_shares
), 0),
443 SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_per_sec_usec
), 0),
444 SD_BUS_PROPERTY("CPUQuotaPeriodUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_period_usec
), 0),
445 SD_BUS_PROPERTY("AllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_cpus
), 0),
446 SD_BUS_PROPERTY("StartupAllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_cpus
), 0),
447 SD_BUS_PROPERTY("AllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_mems
), 0),
448 SD_BUS_PROPERTY("StartupAllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_mems
), 0),
449 SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, io_accounting
), 0),
450 SD_BUS_PROPERTY("IOWeight", "t", NULL
, offsetof(CGroupContext
, io_weight
), 0),
451 SD_BUS_PROPERTY("StartupIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_io_weight
), 0),
452 SD_BUS_PROPERTY("IODeviceWeight", "a(st)", property_get_io_device_weight
, 0, 0),
453 SD_BUS_PROPERTY("IOReadBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
454 SD_BUS_PROPERTY("IOWriteBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
455 SD_BUS_PROPERTY("IOReadIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
456 SD_BUS_PROPERTY("IOWriteIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
457 SD_BUS_PROPERTY("IODeviceLatencyTargetUSec", "a(st)", property_get_io_device_latency
, 0, 0),
458 SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, blockio_accounting
), 0),
459 SD_BUS_PROPERTY("BlockIOWeight", "t", NULL
, offsetof(CGroupContext
, blockio_weight
), 0),
460 SD_BUS_PROPERTY("StartupBlockIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_blockio_weight
), 0),
461 SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight
, 0, 0),
462 SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
463 SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
464 SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, memory_accounting
), 0),
465 SD_BUS_PROPERTY("DefaultMemoryLow", "t", NULL
, offsetof(CGroupContext
, default_memory_low
), 0),
466 SD_BUS_PROPERTY("DefaultStartupMemoryLow", "t", NULL
, offsetof(CGroupContext
, default_startup_memory_low
), 0),
467 SD_BUS_PROPERTY("DefaultMemoryMin", "t", NULL
, offsetof(CGroupContext
, default_memory_min
), 0),
468 SD_BUS_PROPERTY("MemoryMin", "t", NULL
, offsetof(CGroupContext
, memory_min
), 0),
469 SD_BUS_PROPERTY("MemoryLow", "t", NULL
, offsetof(CGroupContext
, memory_low
), 0),
470 SD_BUS_PROPERTY("StartupMemoryLow", "t", NULL
, offsetof(CGroupContext
, startup_memory_low
), 0),
471 SD_BUS_PROPERTY("MemoryHigh", "t", NULL
, offsetof(CGroupContext
, memory_high
), 0),
472 SD_BUS_PROPERTY("StartupMemoryHigh", "t", NULL
, offsetof(CGroupContext
, startup_memory_high
), 0),
473 SD_BUS_PROPERTY("MemoryMax", "t", NULL
, offsetof(CGroupContext
, memory_max
), 0),
474 SD_BUS_PROPERTY("StartupMemoryMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_max
), 0),
475 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL
, offsetof(CGroupContext
, memory_swap_max
), 0),
476 SD_BUS_PROPERTY("StartupMemorySwapMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_swap_max
), 0),
477 SD_BUS_PROPERTY("MemoryZSwapMax", "t", NULL
, offsetof(CGroupContext
, memory_zswap_max
), 0),
478 SD_BUS_PROPERTY("StartupMemoryZSwapMax", "t", NULL
, offsetof(CGroupContext
, startup_memory_zswap_max
), 0),
479 SD_BUS_PROPERTY("MemoryLimit", "t", NULL
, offsetof(CGroupContext
, memory_limit
), 0),
480 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy
, offsetof(CGroupContext
, device_policy
), 0),
481 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow
, 0, 0),
482 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, tasks_accounting
), 0),
483 SD_BUS_PROPERTY("TasksMax", "t", bus_property_get_tasks_max
, offsetof(CGroupContext
, tasks_max
), 0),
484 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, ip_accounting
), 0),
485 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_allow
), 0),
486 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_deny
), 0),
487 SD_BUS_PROPERTY("IPIngressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_ingress
), 0),
488 SD_BUS_PROPERTY("IPEgressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_egress
), 0),
489 SD_BUS_PROPERTY("DisableControllers", "as", property_get_cgroup_mask
, offsetof(CGroupContext
, disable_controllers
), 0),
490 SD_BUS_PROPERTY("ManagedOOMSwap", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_swap
), 0),
491 SD_BUS_PROPERTY("ManagedOOMMemoryPressure", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_mem_pressure
), 0),
492 SD_BUS_PROPERTY("ManagedOOMMemoryPressureLimit", "u", NULL
, offsetof(CGroupContext
, moom_mem_pressure_limit
), 0),
493 SD_BUS_PROPERTY("ManagedOOMPreference", "s", property_get_managed_oom_preference
, offsetof(CGroupContext
, moom_preference
), 0),
494 SD_BUS_PROPERTY("BPFProgram", "a(ss)", property_get_bpf_foreign_program
, 0, 0),
495 SD_BUS_PROPERTY("SocketBindAllow", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_allow
), 0),
496 SD_BUS_PROPERTY("SocketBindDeny", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_deny
), 0),
497 SD_BUS_PROPERTY("RestrictNetworkInterfaces", "(bas)", property_get_restrict_network_interfaces
, 0, 0),
498 SD_BUS_PROPERTY("MemoryPressureWatch", "s", bus_property_get_cgroup_pressure_watch
, offsetof(CGroupContext
, memory_pressure_watch
), 0),
499 SD_BUS_PROPERTY("MemoryPressureThresholdUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, memory_pressure_threshold_usec
), 0),
503 static int bus_cgroup_set_transient_property(
507 sd_bus_message
*message
,
508 UnitWriteFlags flags
,
509 sd_bus_error
*error
) {
518 flags
|= UNIT_PRIVATE
;
520 if (streq(name
, "Delegate")) {
523 if (!UNIT_VTABLE(u
)->can_delegate
)
524 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
526 r
= sd_bus_message_read(message
, "b", &b
);
530 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
532 c
->delegate_controllers
= b
? _CGROUP_MASK_ALL
: 0;
534 unit_write_settingf(u
, flags
, name
, "Delegate=%s", yes_no(b
));
539 } else if (STR_IN_SET(name
, "DelegateControllers", "DisableControllers")) {
542 if (streq(name
, "DelegateControllers") && !UNIT_VTABLE(u
)->can_delegate
)
543 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
545 r
= sd_bus_message_enter_container(message
, 'a', "s");
553 r
= sd_bus_message_read(message
, "s", &t
);
559 cc
= cgroup_controller_from_string(t
);
561 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown cgroup controller '%s'", t
);
563 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
566 r
= sd_bus_message_exit_container(message
);
570 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
571 _cleanup_free_
char *t
= NULL
;
573 r
= cg_mask_to_string(mask
, &t
);
577 if (streq(name
, "DelegateControllers")) {
581 c
->delegate_controllers
= 0;
583 c
->delegate_controllers
|= mask
;
585 unit_write_settingf(u
, flags
, name
, "Delegate=%s", strempty(t
));
587 } else if (streq(name
, "DisableControllers")) {
590 c
->disable_controllers
= 0;
592 c
->disable_controllers
|= mask
;
594 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, strempty(t
));
599 } else if (STR_IN_SET(name
, "IPIngressFilterPath", "IPEgressFilterPath")) {
603 filters
= streq(name
, "IPIngressFilterPath") ? &c
->ip_filters_ingress
: &c
->ip_filters_egress
;
604 r
= sd_bus_message_enter_container(message
, 'a', "s");
611 r
= sd_bus_message_read(message
, "s", &path
);
617 if (!path_is_normalized(path
) || !path_is_absolute(path
))
618 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects a normalized absolute path.", name
);
620 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && !strv_contains(*filters
, path
)) {
621 r
= strv_extend(filters
, path
);
627 r
= sd_bus_message_exit_container(message
);
631 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
632 _cleanup_free_
char *buf
= NULL
;
633 _cleanup_fclose_
FILE *f
= NULL
;
637 *filters
= strv_free(*filters
);
639 unit_invalidate_cgroup_bpf(u
);
640 f
= open_memstream_unlocked(&buf
, &size
);
647 STRV_FOREACH(entry
, *filters
)
648 fprintf(f
, "%s=%s\n", name
, *entry
);
650 r
= fflush_and_check(f
);
654 unit_write_setting(u
, flags
, name
, buf
);
657 r
= bpf_firewall_supported();
660 if (r
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
) {
661 static bool warned
= false;
663 log_full(warned
? LOG_DEBUG
: LOG_WARNING
,
664 "Transient unit %s configures an IP firewall with BPF, but the local system does not support BPF/cgroup firewalling with multiple filters.\n"
665 "Starting this unit will fail! (This warning is only shown for the first started transient unit using IP firewalling.)", u
->id
);
672 } else if (streq(name
, "BPFProgram")) {
676 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
680 while ((r
= sd_bus_message_read(message
, "(ss)", &a
, &p
)) > 0) {
681 int attach_type
= bpf_cgroup_attach_type_from_string(a
);
683 return sd_bus_error_setf(
685 SD_BUS_ERROR_INVALID_ARGS
,
686 "%s expects a valid BPF attach type, got '%s'.",
689 if (!path_is_normalized(p
) || !path_is_absolute(p
))
690 return sd_bus_error_setf(
692 SD_BUS_ERROR_INVALID_ARGS
,
693 "%s= expects a normalized absolute path.",
696 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
697 r
= cgroup_add_bpf_foreign_program(c
, attach_type
, p
);
706 r
= sd_bus_message_exit_container(message
);
710 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
711 _cleanup_free_
char *buf
= NULL
;
712 _cleanup_fclose_
FILE *f
= NULL
;
716 while (c
->bpf_foreign_programs
)
717 cgroup_context_remove_bpf_foreign_program(c
, c
->bpf_foreign_programs
);
719 f
= open_memstream_unlocked(&buf
, &size
);
726 LIST_FOREACH(programs
, fp
, c
->bpf_foreign_programs
)
727 fprintf(f
, "%s=%s:%s\n", name
,
728 bpf_cgroup_attach_type_to_string(fp
->attach_type
),
731 r
= fflush_and_check(f
);
735 unit_write_setting(u
, flags
, name
, buf
);
737 if (c
->bpf_foreign_programs
) {
738 r
= bpf_foreign_supported();
743 "Transient unit %s configures a BPF program pinned to BPF "
744 "filesystem, but the local system does not support that.\n"
745 "Starting this unit will fail!", u
->id
);
751 } else if (streq(name
, "MemoryPressureWatch")) {
752 CGroupPressureWatch p
;
755 r
= sd_bus_message_read(message
, "s", &t
);
760 p
= _CGROUP_PRESSURE_WATCH_INVALID
;
762 p
= cgroup_pressure_watch_from_string(t
);
767 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
768 c
->memory_pressure_watch
= p
;
769 unit_write_settingf(u
, flags
, name
, "MemoryPressureWatch=%s", strempty(cgroup_pressure_watch_to_string(p
)));
774 } else if (streq(name
, "MemoryPressureThresholdUSec")) {
777 r
= sd_bus_message_read(message
, "t", &t
);
781 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
782 c
->memory_pressure_threshold_usec
= t
;
785 unit_write_setting(u
, flags
, name
, "MemoryPressureThresholdUSec=");
787 unit_write_settingf(u
, flags
, name
, "MemoryPressureThresholdUSec=%" PRIu64
, t
);
796 static int bus_cgroup_set_boolean(
801 sd_bus_message
*message
,
802 UnitWriteFlags flags
,
803 sd_bus_error
*error
) {
809 r
= sd_bus_message_read(message
, "b", &b
);
813 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
815 unit_invalidate_cgroup(u
, mask
);
816 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, yes_no(b
));
822 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
823 static int bus_cgroup_set_##function( \
827 sd_bus_message *message, \
828 UnitWriteFlags flags, \
829 sd_bus_error *error) { \
836 r = sd_bus_message_read(message, "t", &v); \
841 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
842 "Value specified in %s is out of range", name); \
844 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
846 unit_invalidate_cgroup(u, mask); \
849 unit_write_settingf(u, flags, name, \
852 unit_write_settingf(u, flags, name, \
853 "%s=%" PRIu64, name, v); \
859 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
860 static int bus_cgroup_set_##function( \
864 sd_bus_message *message, \
865 UnitWriteFlags flags, \
866 sd_bus_error *error) { \
873 r = sd_bus_message_read(message, "t", &v); \
878 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
879 "Value specified in %s is out of range", name); \
881 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
883 unit_invalidate_cgroup(u, mask); \
885 if (v == CGROUP_LIMIT_MAX) \
886 unit_write_settingf(u, flags, name, \
887 "%s=infinity", name); \
889 unit_write_settingf(u, flags, name, \
890 "%s=%" PRIu64, name, v); \
895 static int bus_cgroup_set_##function##_scale( \
899 sd_bus_message *message, \
900 UnitWriteFlags flags, \
901 sd_bus_error *error) { \
909 r = sd_bus_message_read(message, "u", &raw); \
913 v = scale(raw, UINT32_MAX); \
914 if (v < minimum || v >= UINT64_MAX) \
915 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
916 "Value specified in %s is out of range", name); \
918 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
920 unit_invalidate_cgroup(u, mask); \
922 /* Prepare to chop off suffix */ \
923 assert_se(endswith(name, "Scale")); \
925 int scaled = UINT32_SCALE_TO_PERMYRIAD(raw); \
926 unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR, \
927 (int)(strlen(name) - strlen("Scale")), name, \
928 PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled)); \
934 DISABLE_WARNING_TYPE_LIMITS
;
935 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares
, CGROUP_MASK_CPU
, CGROUP_CPU_SHARES_IS_OK
, CGROUP_CPU_SHARES_INVALID
);
936 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight
, CGROUP_MASK_IO
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
937 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight
, CGROUP_MASK_BLKIO
, CGROUP_BLKIO_WEIGHT_IS_OK
, CGROUP_BLKIO_WEIGHT_INVALID
);
938 BUS_DEFINE_SET_CGROUP_LIMIT(memory
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 1);
939 BUS_DEFINE_SET_CGROUP_LIMIT(memory_protection
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
940 BUS_DEFINE_SET_CGROUP_LIMIT(swap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
941 BUS_DEFINE_SET_CGROUP_LIMIT(zswap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
944 static int bus_cgroup_set_cpu_weight(
948 sd_bus_message
*message
,
949 UnitWriteFlags flags
,
950 sd_bus_error
*error
) {
954 r
= sd_bus_message_read(message
, "t", &v
);
957 if (!CGROUP_WEIGHT_IS_OK(v
) && v
!= CGROUP_WEIGHT_IDLE
)
958 return sd_bus_error_setf(
959 error
, SD_BUS_ERROR_INVALID_ARGS
, "Value specified in %s is out of range", name
);
960 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
962 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
963 if (v
== CGROUP_WEIGHT_INVALID
)
964 unit_write_settingf(u
, flags
, name
, "%s=", name
);
965 else if (v
== CGROUP_WEIGHT_IDLE
)
966 unit_write_settingf(u
, flags
, name
, "%s=idle", name
);
968 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu64
, name
, v
);
973 static int bus_cgroup_set_tasks_max(
977 sd_bus_message
*message
,
978 UnitWriteFlags flags
,
979 sd_bus_error
*error
) {
986 r
= sd_bus_message_read(message
, "t", &v
);
991 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
992 "Value specified in %s is out of range", name
);
994 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
995 *p
= (TasksMax
) { .value
= v
, .scale
= 0 }; /* When .scale==0, .value is the absolute value */
996 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
998 if (v
== CGROUP_LIMIT_MAX
)
999 unit_write_settingf(u
, flags
, name
,
1000 "%s=infinity", name
);
1002 unit_write_settingf(u
, flags
, name
,
1003 "%s=%" PRIu64
, name
, v
);
1009 static int bus_cgroup_set_tasks_max_scale(
1013 sd_bus_message
*message
,
1014 UnitWriteFlags flags
,
1015 sd_bus_error
*error
) {
1022 r
= sd_bus_message_read(message
, "u", &v
);
1026 if (v
< 1 || v
>= UINT32_MAX
)
1027 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1028 "Value specified in %s is out of range", name
);
1030 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1031 *p
= (TasksMax
) { v
, UINT32_MAX
}; /* .scale is not 0, so this is interpreted as v/UINT32_MAX. */
1032 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
1034 uint32_t scaled
= DIV_ROUND_UP((uint64_t) v
* 100U, (uint64_t) UINT32_MAX
);
1035 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu32
".%" PRIu32
"%%", "TasksMax",
1036 scaled
/ 10, scaled
% 10);
1042 int bus_cgroup_set_property(
1046 sd_bus_message
*message
,
1047 UnitWriteFlags flags
,
1048 sd_bus_error
*error
) {
1050 CGroupIOLimitType iol_type
;
1058 flags
|= UNIT_PRIVATE
;
1060 if (streq(name
, "CPUAccounting"))
1061 return bus_cgroup_set_boolean(u
, name
, &c
->cpu_accounting
, get_cpu_accounting_mask(), message
, flags
, error
);
1063 if (streq(name
, "CPUWeight"))
1064 return bus_cgroup_set_cpu_weight(u
, name
, &c
->cpu_weight
, message
, flags
, error
);
1066 if (streq(name
, "StartupCPUWeight"))
1067 return bus_cgroup_set_cpu_weight(u
, name
, &c
->startup_cpu_weight
, message
, flags
, error
);
1069 if (streq(name
, "CPUShares"))
1070 return bus_cgroup_set_cpu_shares(u
, name
, &c
->cpu_shares
, message
, flags
, error
);
1072 if (streq(name
, "StartupCPUShares"))
1073 return bus_cgroup_set_cpu_shares(u
, name
, &c
->startup_cpu_shares
, message
, flags
, error
);
1075 if (streq(name
, "IOAccounting"))
1076 return bus_cgroup_set_boolean(u
, name
, &c
->io_accounting
, CGROUP_MASK_IO
, message
, flags
, error
);
1078 if (streq(name
, "IOWeight"))
1079 return bus_cgroup_set_io_weight(u
, name
, &c
->io_weight
, message
, flags
, error
);
1081 if (streq(name
, "StartupIOWeight"))
1082 return bus_cgroup_set_io_weight(u
, name
, &c
->startup_io_weight
, message
, flags
, error
);
1084 if (streq(name
, "BlockIOAccounting"))
1085 return bus_cgroup_set_boolean(u
, name
, &c
->blockio_accounting
, CGROUP_MASK_BLKIO
, message
, flags
, error
);
1087 if (streq(name
, "BlockIOWeight"))
1088 return bus_cgroup_set_blockio_weight(u
, name
, &c
->blockio_weight
, message
, flags
, error
);
1090 if (streq(name
, "StartupBlockIOWeight"))
1091 return bus_cgroup_set_blockio_weight(u
, name
, &c
->startup_blockio_weight
, message
, flags
, error
);
1093 if (streq(name
, "MemoryAccounting"))
1094 return bus_cgroup_set_boolean(u
, name
, &c
->memory_accounting
, CGROUP_MASK_MEMORY
, message
, flags
, error
);
1096 if (streq(name
, "MemoryMin")) {
1097 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_min
, message
, flags
, error
);
1099 c
->memory_min_set
= true;
1103 if (streq(name
, "MemoryLow")) {
1104 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_low
, message
, flags
, error
);
1106 c
->memory_low_set
= true;
1110 if (streq(name
, "StartupMemoryLow")) {
1111 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->startup_memory_low
, message
, flags
, error
);
1113 c
->startup_memory_low_set
= true;
1117 if (streq(name
, "DefaultMemoryMin")) {
1118 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1120 c
->default_memory_min_set
= true;
1124 if (streq(name
, "DefaultMemoryLow")) {
1125 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1127 c
->default_memory_low_set
= true;
1131 if (streq(name
, "DefaultStartupMemoryLow")) {
1132 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_startup_memory_low
, message
, flags
, error
);
1134 c
->default_startup_memory_low_set
= true;
1138 if (streq(name
, "MemoryHigh"))
1139 return bus_cgroup_set_memory(u
, name
, &c
->memory_high
, message
, flags
, error
);
1141 if (streq(name
, "StartupMemoryHigh")) {
1142 r
= bus_cgroup_set_memory(u
, name
, &c
->startup_memory_high
, message
, flags
, error
);
1144 c
->startup_memory_high_set
= true;
1148 if (streq(name
, "MemorySwapMax"))
1149 return bus_cgroup_set_swap(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1151 if (streq(name
, "StartupMemorySwapMax")) {
1152 r
= bus_cgroup_set_swap(u
, name
, &c
->startup_memory_swap_max
, message
, flags
, error
);
1154 c
->startup_memory_swap_max_set
= true;
1158 if (streq(name
, "MemoryZSwapMax"))
1159 return bus_cgroup_set_zswap(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1161 if (streq(name
, "StartupMemoryZSwapMax")) {
1162 r
= bus_cgroup_set_zswap(u
, name
, &c
->startup_memory_zswap_max
, message
, flags
, error
);
1164 c
->startup_memory_zswap_max_set
= true;
1168 if (streq(name
, "MemoryMax"))
1169 return bus_cgroup_set_memory(u
, name
, &c
->memory_max
, message
, flags
, error
);
1171 if (streq(name
, "StartupMemoryMax")) {
1172 r
= bus_cgroup_set_memory(u
, name
, &c
->startup_memory_max
, message
, flags
, error
);
1174 c
->startup_memory_max_set
= true;
1178 if (streq(name
, "MemoryLimit"))
1179 return bus_cgroup_set_memory(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1181 if (streq(name
, "MemoryMinScale")) {
1182 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_min
, message
, flags
, error
);
1184 c
->memory_min_set
= true;
1188 if (streq(name
, "MemoryLowScale")) {
1189 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_low
, message
, flags
, error
);
1191 c
->memory_low_set
= true;
1195 if (streq(name
, "DefaultMemoryMinScale")) {
1196 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1198 c
->default_memory_min_set
= true;
1202 if (streq(name
, "DefaultMemoryLowScale")) {
1203 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1205 c
->default_memory_low_set
= true;
1209 if (streq(name
, "MemoryHighScale"))
1210 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_high
, message
, flags
, error
);
1212 if (streq(name
, "MemorySwapMaxScale"))
1213 return bus_cgroup_set_swap_scale(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1215 if (streq(name
, "MemoryZSwapMaxScale"))
1216 return bus_cgroup_set_zswap_scale(u
, name
, &c
->memory_zswap_max
, message
, flags
, error
);
1218 if (streq(name
, "MemoryMaxScale"))
1219 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_max
, message
, flags
, error
);
1221 if (streq(name
, "MemoryLimitScale"))
1222 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1224 if (streq(name
, "TasksAccounting"))
1225 return bus_cgroup_set_boolean(u
, name
, &c
->tasks_accounting
, CGROUP_MASK_PIDS
, message
, flags
, error
);
1227 if (streq(name
, "TasksMax"))
1228 return bus_cgroup_set_tasks_max(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1230 if (streq(name
, "TasksMaxScale"))
1231 return bus_cgroup_set_tasks_max_scale(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1233 if (streq(name
, "CPUQuotaPerSecUSec")) {
1236 r
= sd_bus_message_read(message
, "t", &u64
);
1241 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "CPUQuotaPerSecUSec= value out of range");
1243 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1244 c
->cpu_quota_per_sec_usec
= u64
;
1245 u
->warned_clamping_cpu_quota_period
= false;
1246 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1248 if (c
->cpu_quota_per_sec_usec
== USEC_INFINITY
)
1249 unit_write_setting(u
, flags
, "CPUQuota", "CPUQuota=");
1251 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
1253 unit_write_settingf(u
, flags
, "CPUQuota",
1255 (double) (c
->cpu_quota_per_sec_usec
/ 10000));
1260 } else if (streq(name
, "CPUQuotaPeriodUSec")) {
1263 r
= sd_bus_message_read(message
, "t", &u64
);
1267 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1268 c
->cpu_quota_period_usec
= u64
;
1269 u
->warned_clamping_cpu_quota_period
= false;
1270 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1271 if (c
->cpu_quota_period_usec
== USEC_INFINITY
)
1272 unit_write_setting(u
, flags
, "CPUQuotaPeriodSec", "CPUQuotaPeriodSec=");
1274 unit_write_settingf(u
, flags
, "CPUQuotaPeriodSec",
1275 "CPUQuotaPeriodSec=%s",
1276 FORMAT_TIMESPAN(c
->cpu_quota_period_usec
, 1));
1281 } else if (STR_IN_SET(name
, "AllowedCPUs", "StartupAllowedCPUs", "AllowedMemoryNodes", "StartupAllowedMemoryNodes")) {
1284 _cleanup_(cpu_set_reset
) CPUSet new_set
= {};
1286 r
= sd_bus_message_read_array(message
, 'y', &a
, &n
);
1290 r
= cpu_set_from_dbus(a
, n
, &new_set
);
1294 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1295 _cleanup_free_
char *setstr
= NULL
;
1298 setstr
= cpu_set_to_range_string(&new_set
);
1302 if (streq(name
, "AllowedCPUs"))
1303 set
= &c
->cpuset_cpus
;
1304 else if (streq(name
, "StartupAllowedCPUs"))
1305 set
= &c
->startup_cpuset_cpus
;
1306 else if (streq(name
, "AllowedMemoryNodes"))
1307 set
= &c
->cpuset_mems
;
1308 else if (streq(name
, "StartupAllowedMemoryNodes"))
1309 set
= &c
->startup_cpuset_mems
;
1315 new_set
= (CPUSet
) {};
1317 unit_invalidate_cgroup(u
, CGROUP_MASK_CPUSET
);
1318 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, setstr
);
1323 } else if ((iol_type
= cgroup_io_limit_type_from_string(name
)) >= 0) {
1328 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1332 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1334 if (!path_is_normalized(path
))
1335 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1337 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1338 CGroupIODeviceLimit
*a
= NULL
;
1340 LIST_FOREACH(device_limits
, b
, c
->io_device_limits
)
1341 if (path_equal(path
, b
->path
)) {
1347 CGroupIOLimitType type
;
1349 a
= new0(CGroupIODeviceLimit
, 1);
1353 a
->path
= strdup(path
);
1359 for (type
= 0; type
< _CGROUP_IO_LIMIT_TYPE_MAX
; type
++)
1360 a
->limits
[type
] = cgroup_io_limit_defaults
[type
];
1362 LIST_PREPEND(device_limits
, c
->io_device_limits
, a
);
1365 a
->limits
[iol_type
] = u64
;
1373 r
= sd_bus_message_exit_container(message
);
1377 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1378 _cleanup_free_
char *buf
= NULL
;
1379 _cleanup_fclose_
FILE *f
= NULL
;
1383 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1384 a
->limits
[iol_type
] = cgroup_io_limit_defaults
[iol_type
];
1386 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1388 f
= open_memstream_unlocked(&buf
, &size
);
1392 fprintf(f
, "%s=\n", name
);
1393 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1394 if (a
->limits
[iol_type
] != cgroup_io_limit_defaults
[iol_type
])
1395 fprintf(f
, "%s=%s %" PRIu64
"\n", name
, a
->path
, a
->limits
[iol_type
]);
1397 r
= fflush_and_check(f
);
1400 unit_write_setting(u
, flags
, name
, buf
);
1405 } else if (streq(name
, "IODeviceWeight")) {
1410 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1414 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1416 if (!path_is_normalized(path
))
1417 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1419 if (!CGROUP_WEIGHT_IS_OK(weight
) || weight
== CGROUP_WEIGHT_INVALID
)
1420 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "IODeviceWeight= value out of range");
1422 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1423 CGroupIODeviceWeight
*a
= NULL
;
1425 LIST_FOREACH(device_weights
, b
, c
->io_device_weights
)
1426 if (path_equal(b
->path
, path
)) {
1432 a
= new0(CGroupIODeviceWeight
, 1);
1436 a
->path
= strdup(path
);
1441 LIST_PREPEND(device_weights
, c
->io_device_weights
, a
);
1450 r
= sd_bus_message_exit_container(message
);
1454 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1455 _cleanup_free_
char *buf
= NULL
;
1456 _cleanup_fclose_
FILE *f
= NULL
;
1460 while (c
->io_device_weights
)
1461 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
1463 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1465 f
= open_memstream_unlocked(&buf
, &size
);
1469 fputs("IODeviceWeight=\n", f
);
1470 LIST_FOREACH(device_weights
, a
, c
->io_device_weights
)
1471 fprintf(f
, "IODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1473 r
= fflush_and_check(f
);
1476 unit_write_setting(u
, flags
, name
, buf
);
1481 } else if (streq(name
, "IODeviceLatencyTargetUSec")) {
1486 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1490 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &target
)) > 0) {
1492 if (!path_is_normalized(path
))
1493 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1495 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1496 CGroupIODeviceLatency
*a
= NULL
;
1498 LIST_FOREACH(device_latencies
, b
, c
->io_device_latencies
)
1499 if (path_equal(b
->path
, path
)) {
1505 a
= new0(CGroupIODeviceLatency
, 1);
1509 a
->path
= strdup(path
);
1514 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, a
);
1517 a
->target_usec
= target
;
1523 r
= sd_bus_message_exit_container(message
);
1527 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1528 _cleanup_free_
char *buf
= NULL
;
1529 _cleanup_fclose_
FILE *f
= NULL
;
1533 while (c
->io_device_latencies
)
1534 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
1536 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1538 f
= open_memstream_unlocked(&buf
, &size
);
1542 fputs("IODeviceLatencyTargetSec=\n", f
);
1543 LIST_FOREACH(device_latencies
, a
, c
->io_device_latencies
)
1544 fprintf(f
, "IODeviceLatencyTargetSec=%s %s\n",
1545 a
->path
, FORMAT_TIMESPAN(a
->target_usec
, 1));
1547 r
= fflush_and_check(f
);
1550 unit_write_setting(u
, flags
, name
, buf
);
1555 } else if (STR_IN_SET(name
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
1561 read
= streq(name
, "BlockIOReadBandwidth");
1563 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1567 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1569 if (!path_is_normalized(path
))
1570 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1572 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1573 CGroupBlockIODeviceBandwidth
*a
= NULL
;
1575 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
)
1576 if (path_equal(path
, b
->path
)) {
1582 a
= new0(CGroupBlockIODeviceBandwidth
, 1);
1586 a
->rbps
= CGROUP_LIMIT_MAX
;
1587 a
->wbps
= CGROUP_LIMIT_MAX
;
1588 a
->path
= strdup(path
);
1594 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, a
);
1608 r
= sd_bus_message_exit_container(message
);
1612 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1613 _cleanup_free_
char *buf
= NULL
;
1614 _cleanup_fclose_
FILE *f
= NULL
;
1618 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
) {
1620 a
->rbps
= CGROUP_LIMIT_MAX
;
1622 a
->wbps
= CGROUP_LIMIT_MAX
;
1625 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1627 f
= open_memstream_unlocked(&buf
, &size
);
1632 fputs("BlockIOReadBandwidth=\n", f
);
1633 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1634 if (a
->rbps
!= CGROUP_LIMIT_MAX
)
1635 fprintf(f
, "BlockIOReadBandwidth=%s %" PRIu64
"\n", a
->path
, a
->rbps
);
1637 fputs("BlockIOWriteBandwidth=\n", f
);
1638 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1639 if (a
->wbps
!= CGROUP_LIMIT_MAX
)
1640 fprintf(f
, "BlockIOWriteBandwidth=%s %" PRIu64
"\n", a
->path
, a
->wbps
);
1643 r
= fflush_and_check(f
);
1647 unit_write_setting(u
, flags
, name
, buf
);
1652 } else if (streq(name
, "BlockIODeviceWeight")) {
1657 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1661 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1663 if (!path_is_normalized(path
))
1664 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1666 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight
) || weight
== CGROUP_BLKIO_WEIGHT_INVALID
)
1667 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "BlockIODeviceWeight= out of range");
1669 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1670 CGroupBlockIODeviceWeight
*a
= NULL
;
1672 LIST_FOREACH(device_weights
, b
, c
->blockio_device_weights
)
1673 if (path_equal(b
->path
, path
)) {
1679 a
= new0(CGroupBlockIODeviceWeight
, 1);
1683 a
->path
= strdup(path
);
1688 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, a
);
1697 r
= sd_bus_message_exit_container(message
);
1701 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1702 _cleanup_free_
char *buf
= NULL
;
1703 _cleanup_fclose_
FILE *f
= NULL
;
1707 while (c
->blockio_device_weights
)
1708 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
1710 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1712 f
= open_memstream_unlocked(&buf
, &size
);
1716 fputs("BlockIODeviceWeight=\n", f
);
1717 LIST_FOREACH(device_weights
, a
, c
->blockio_device_weights
)
1718 fprintf(f
, "BlockIODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1720 r
= fflush_and_check(f
);
1724 unit_write_setting(u
, flags
, name
, buf
);
1729 } else if (streq(name
, "DevicePolicy")) {
1731 CGroupDevicePolicy p
;
1733 r
= sd_bus_message_read(message
, "s", &policy
);
1737 p
= cgroup_device_policy_from_string(policy
);
1741 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1742 c
->device_policy
= p
;
1743 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1744 unit_write_settingf(u
, flags
, name
, "DevicePolicy=%s", policy
);
1749 } else if (streq(name
, "DeviceAllow")) {
1750 const char *path
, *rwm
;
1753 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
1757 while ((r
= sd_bus_message_read(message
, "(ss)", &path
, &rwm
)) > 0) {
1759 if (!valid_device_allow_pattern(path
) || strpbrk(path
, WHITESPACE
))
1760 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires device node or pattern");
1764 else if (!in_charset(rwm
, "rwm"))
1765 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires combination of rwm flags");
1767 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1768 CGroupDeviceAllow
*a
= NULL
;
1770 LIST_FOREACH(device_allow
, b
, c
->device_allow
)
1771 if (path_equal(b
->path
, path
)) {
1777 a
= new0(CGroupDeviceAllow
, 1);
1781 a
->path
= strdup(path
);
1787 LIST_PREPEND(device_allow
, c
->device_allow
, a
);
1790 a
->r
= strchr(rwm
, 'r');
1791 a
->w
= strchr(rwm
, 'w');
1792 a
->m
= strchr(rwm
, 'm');
1800 r
= sd_bus_message_exit_container(message
);
1804 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1805 _cleanup_free_
char *buf
= NULL
;
1806 _cleanup_fclose_
FILE *f
= NULL
;
1810 while (c
->device_allow
)
1811 cgroup_context_free_device_allow(c
, c
->device_allow
);
1813 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1815 f
= open_memstream_unlocked(&buf
, &size
);
1819 fputs("DeviceAllow=\n", f
);
1820 LIST_FOREACH(device_allow
, a
, c
->device_allow
)
1821 fprintf(f
, "DeviceAllow=%s %s%s%s\n", a
->path
, a
->r
? "r" : "", a
->w
? "w" : "", a
->m
? "m" : "");
1823 r
= fflush_and_check(f
);
1826 unit_write_setting(u
, flags
, name
, buf
);
1831 } else if (streq(name
, "IPAccounting")) {
1834 r
= sd_bus_message_read(message
, "b", &b
);
1838 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1839 c
->ip_accounting
= b
;
1841 unit_invalidate_cgroup_bpf(u
);
1842 unit_write_settingf(u
, flags
, name
, "IPAccounting=%s", yes_no(b
));
1847 } else if (STR_IN_SET(name
, "IPAddressAllow", "IPAddressDeny")) {
1848 _cleanup_set_free_ Set
*new_prefixes
= NULL
;
1851 r
= sd_bus_message_enter_container(message
, 'a', "(iayu)");
1861 r
= sd_bus_message_enter_container(message
, 'r', "iayu");
1867 r
= sd_bus_message_read(message
, "i", &family
);
1871 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1872 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects IPv4 or IPv6 addresses only.", name
);
1874 r
= sd_bus_message_read_array(message
, 'y', &ap
, &an
);
1878 if (an
!= FAMILY_ADDRESS_SIZE(family
))
1879 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1880 af_to_name(family
), FAMILY_ADDRESS_SIZE(family
), an
);
1882 r
= sd_bus_message_read(message
, "u", &prefixlen
);
1886 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
)*8)
1887 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
));
1889 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1890 struct in_addr_prefix prefix
= {
1892 .prefixlen
= prefixlen
,
1895 memcpy(&prefix
.address
, ap
, an
);
1897 r
= in_addr_prefix_add(&new_prefixes
, &prefix
);
1902 r
= sd_bus_message_exit_container(message
);
1909 r
= sd_bus_message_exit_container(message
);
1913 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1914 _cleanup_free_
char *buf
= NULL
;
1915 _cleanup_fclose_
FILE *f
= NULL
;
1920 unit_invalidate_cgroup_bpf(u
);
1921 f
= open_memstream_unlocked(&buf
, &size
);
1925 prefixes
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow
: &c
->ip_address_deny
;
1926 reduced
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow_reduced
: &c
->ip_address_deny_reduced
;
1930 *prefixes
= set_free(*prefixes
);
1936 r
= in_addr_prefixes_merge(prefixes
, new_prefixes
);
1940 const struct in_addr_prefix
*p
;
1941 SET_FOREACH(p
, new_prefixes
)
1942 fprintf(f
, "%s=%s\n", name
,
1943 IN_ADDR_PREFIX_TO_STRING(p
->family
, &p
->address
, p
->prefixlen
));
1946 r
= fflush_and_check(f
);
1950 unit_write_setting(u
, flags
, name
, buf
);
1956 if (STR_IN_SET(name
, "ManagedOOMSwap", "ManagedOOMMemoryPressure")) {
1957 ManagedOOMMode
*cgroup_mode
= streq(name
, "ManagedOOMSwap") ? &c
->moom_swap
: &c
->moom_mem_pressure
;
1961 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
1962 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
1964 r
= sd_bus_message_read(message
, "s", &mode
);
1968 m
= managed_oom_mode_from_string(mode
);
1972 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1974 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, mode
);
1977 (void) manager_varlink_send_managed_oom_update(u
);
1981 if (streq(name
, "ManagedOOMMemoryPressureLimit")) {
1984 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
1985 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
1987 r
= sd_bus_message_read(message
, "u", &v
);
1991 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1992 c
->moom_mem_pressure_limit
= v
;
1993 unit_write_settingf(u
, flags
, name
,
1994 "ManagedOOMMemoryPressureLimit=" PERMYRIAD_AS_PERCENT_FORMAT_STR
,
1995 PERMYRIAD_AS_PERCENT_FORMAT_VAL(UINT32_SCALE_TO_PERMYRIAD(v
)));
1998 if (c
->moom_mem_pressure
== MANAGED_OOM_KILL
)
1999 (void) manager_varlink_send_managed_oom_update(u
);
2004 if (streq(name
, "ManagedOOMPreference")) {
2005 ManagedOOMPreference p
;
2008 r
= sd_bus_message_read(message
, "s", &pref
);
2012 p
= managed_oom_preference_from_string(pref
);
2016 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2017 c
->moom_preference
= p
;
2018 unit_write_settingf(u
, flags
, name
, "ManagedOOMPreference=%s", pref
);
2023 if (STR_IN_SET(name
, "SocketBindAllow", "SocketBindDeny")) {
2024 CGroupSocketBindItem
**list
;
2025 uint16_t nr_ports
, port_min
;
2027 int32_t family
, ip_protocol
;
2029 list
= streq(name
, "SocketBindAllow") ? &c
->socket_bind_allow
: &c
->socket_bind_deny
;
2031 r
= sd_bus_message_enter_container(message
, 'a', "(iiqq)");
2035 while ((r
= sd_bus_message_read(message
, "(iiqq)", &family
, &ip_protocol
, &nr_ports
, &port_min
)) > 0) {
2037 if (!IN_SET(family
, AF_UNSPEC
, AF_INET
, AF_INET6
))
2038 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects INET or INET6 family, if specified.", name
);
2040 if (!IN_SET(ip_protocol
, 0, IPPROTO_TCP
, IPPROTO_UDP
))
2041 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects TCP or UDP protocol, if specified.", name
);
2043 if (port_min
+ (uint32_t) nr_ports
> (1 << 16))
2044 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects maximum port value lesser than 65536.", name
);
2046 if (port_min
== 0 && nr_ports
!= 0)
2047 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects port range starting with positive value.", name
);
2049 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2050 _cleanup_free_ CGroupSocketBindItem
*item
= NULL
;
2052 item
= new(CGroupSocketBindItem
, 1);
2056 *item
= (CGroupSocketBindItem
) {
2057 .address_family
= family
,
2058 .ip_protocol
= ip_protocol
,
2059 .nr_ports
= nr_ports
,
2060 .port_min
= port_min
2063 LIST_PREPEND(socket_bind_items
, *list
, TAKE_PTR(item
));
2070 r
= sd_bus_message_exit_container(message
);
2074 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2075 _cleanup_free_
char *buf
= NULL
;
2076 _cleanup_fclose_
FILE *f
= NULL
;
2080 cgroup_context_remove_socket_bind(list
);
2082 if ((u
->manager
->cgroup_supported
& CGROUP_MASK_BPF_SOCKET_BIND
) == 0)
2084 "Unit %s configures source compiled BPF programs "
2085 "but the local system does not support that.\n"
2086 "Starting this unit will fail!", u
->id
);
2089 f
= open_memstream_unlocked(&buf
, &size
);
2093 fprintf(f
, "%s:", name
);
2095 LIST_FOREACH(socket_bind_items
, item
, *list
)
2096 cgroup_context_dump_socket_bind_item(item
, f
);
2100 r
= fflush_and_check(f
);
2104 unit_write_setting(u
, flags
, name
, buf
);
2109 if (streq(name
, "RestrictNetworkInterfaces")) {
2111 _cleanup_strv_free_
char **l
= NULL
;
2113 r
= sd_bus_message_enter_container(message
, 'r', "bas");
2117 r
= sd_bus_message_read(message
, "b", &is_allow_list
);
2121 r
= sd_bus_message_read_strv(message
, &l
);
2125 r
= sd_bus_message_exit_container(message
);
2129 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2130 _cleanup_free_
char *joined
= NULL
;
2132 if (strv_isempty(l
)) {
2133 c
->restrict_network_interfaces_is_allow_list
= false;
2134 c
->restrict_network_interfaces
= set_free(c
->restrict_network_interfaces
);
2136 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2140 if (set_isempty(c
->restrict_network_interfaces
))
2141 c
->restrict_network_interfaces_is_allow_list
= is_allow_list
;
2143 STRV_FOREACH(s
, l
) {
2144 if (!ifname_valid(*s
)) {
2145 log_full(LOG_WARNING
, "Invalid interface name, ignoring: %s", *s
);
2148 if (c
->restrict_network_interfaces_is_allow_list
!= (bool) is_allow_list
)
2149 free(set_remove(c
->restrict_network_interfaces
, *s
));
2151 r
= set_put_strdup(&c
->restrict_network_interfaces
, *s
);
2157 joined
= strv_join(l
, " ");
2161 unit_write_settingf(u
, flags
, name
, "%s=%s%s", name
, is_allow_list
? "" : "~", joined
);
2167 if (streq(name
, "DisableControllers") || (u
->transient
&& u
->load_state
== UNIT_STUB
))
2168 return bus_cgroup_set_transient_property(u
, c
, name
, message
, flags
, error
);