1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "bpf-firewall.h"
8 #include "bpf-foreign.h"
9 #include "bus-get-properties.h"
10 #include "cgroup-util.h"
12 #include "core-varlink.h"
13 #include "dbus-cgroup.h"
14 #include "dbus-util.h"
15 #include "errno-util.h"
18 #include "in-addr-prefix-util.h"
19 #include "ip-protocol-list.h"
20 #include "limits-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "percent-util.h"
24 #include "socket-util.h"
26 BUS_DEFINE_PROPERTY_GET(bus_property_get_tasks_max
, "t", TasksMax
, tasks_max_resolve
);
28 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_cgroup_device_policy
, cgroup_device_policy
, CGroupDevicePolicy
);
29 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_mode
, managed_oom_mode
, ManagedOOMMode
);
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_preference
, managed_oom_preference
, ManagedOOMPreference
);
32 static int property_get_cgroup_mask(
35 const char *interface
,
37 sd_bus_message
*reply
,
39 sd_bus_error
*error
) {
41 CGroupMask
*mask
= userdata
;
47 r
= sd_bus_message_open_container(reply
, 'a', "s");
51 for (CGroupController ctrl
= 0; ctrl
< _CGROUP_CONTROLLER_MAX
; ctrl
++) {
52 if ((*mask
& CGROUP_CONTROLLER_TO_MASK(ctrl
)) == 0)
55 r
= sd_bus_message_append(reply
, "s", cgroup_controller_to_string(ctrl
));
60 return sd_bus_message_close_container(reply
);
63 static int property_get_delegate_controllers(
66 const char *interface
,
68 sd_bus_message
*reply
,
70 sd_bus_error
*error
) {
72 CGroupContext
*c
= 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
= userdata
;
94 _cleanup_free_
uint8_t *array
= NULL
;
101 (void) cpu_set_to_dbus(cpus
, &array
, &allocated
);
102 return sd_bus_message_append_array(reply
, 'y', array
, allocated
);
105 static int property_get_io_device_weight(
108 const char *interface
,
109 const char *property
,
110 sd_bus_message
*reply
,
112 sd_bus_error
*error
) {
114 CGroupContext
*c
= userdata
;
121 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
125 LIST_FOREACH(device_weights
, w
, c
->io_device_weights
) {
126 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
131 return sd_bus_message_close_container(reply
);
134 static int property_get_io_device_limits(
137 const char *interface
,
138 const char *property
,
139 sd_bus_message
*reply
,
141 sd_bus_error
*error
) {
143 CGroupContext
*c
= userdata
;
150 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
154 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
) {
155 CGroupIOLimitType type
;
157 type
= cgroup_io_limit_type_from_string(property
);
158 if (type
< 0 || l
->limits
[type
] == cgroup_io_limit_defaults
[type
])
161 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->limits
[type
]);
166 return sd_bus_message_close_container(reply
);
169 static int property_get_io_device_latency(
172 const char *interface
,
173 const char *property
,
174 sd_bus_message
*reply
,
176 sd_bus_error
*error
) {
178 CGroupContext
*c
= userdata
;
185 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
189 LIST_FOREACH(device_latencies
, l
, c
->io_device_latencies
) {
190 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->target_usec
);
195 return sd_bus_message_close_container(reply
);
198 static int property_get_blockio_device_weight(
201 const char *interface
,
202 const char *property
,
203 sd_bus_message
*reply
,
205 sd_bus_error
*error
) {
207 CGroupContext
*c
= userdata
;
214 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
218 LIST_FOREACH(device_weights
, w
, c
->blockio_device_weights
) {
219 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
224 return sd_bus_message_close_container(reply
);
227 static int property_get_blockio_device_bandwidths(
230 const char *interface
,
231 const char *property
,
232 sd_bus_message
*reply
,
234 sd_bus_error
*error
) {
236 CGroupContext
*c
= userdata
;
243 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
247 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
250 if (streq(property
, "BlockIOReadBandwidth"))
255 if (v
== CGROUP_LIMIT_MAX
)
258 r
= sd_bus_message_append(reply
, "(st)", b
->path
, v
);
263 return sd_bus_message_close_container(reply
);
266 static int property_get_device_allow(
269 const char *interface
,
270 const char *property
,
271 sd_bus_message
*reply
,
273 sd_bus_error
*error
) {
275 CGroupContext
*c
= userdata
;
282 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
286 LIST_FOREACH(device_allow
, a
, c
->device_allow
) {
299 r
= sd_bus_message_append(reply
, "(ss)", a
->path
, rwm
);
304 return sd_bus_message_close_container(reply
);
307 static int property_get_ip_address_access(
310 const char *interface
,
311 const char *property
,
312 sd_bus_message
*reply
,
314 sd_bus_error
*error
) {
316 Set
**prefixes
= userdata
;
317 struct in_addr_prefix
*i
;
322 r
= sd_bus_message_open_container(reply
, 'a', "(iayu)");
326 SET_FOREACH(i
, *prefixes
) {
328 r
= sd_bus_message_open_container(reply
, 'r', "iayu");
332 r
= sd_bus_message_append(reply
, "i", i
->family
);
336 r
= sd_bus_message_append_array(reply
, 'y', &i
->address
, FAMILY_ADDRESS_SIZE(i
->family
));
340 r
= sd_bus_message_append(reply
, "u", (uint32_t) i
->prefixlen
);
344 r
= sd_bus_message_close_container(reply
);
349 return sd_bus_message_close_container(reply
);
352 static int property_get_bpf_foreign_program(
355 const char *interface
,
356 const char *property
,
357 sd_bus_message
*reply
,
359 sd_bus_error
*error
) {
360 CGroupContext
*c
= userdata
;
363 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
367 LIST_FOREACH(programs
, p
, c
->bpf_foreign_programs
) {
368 const char *attach_type
= bpf_cgroup_attach_type_to_string(p
->attach_type
);
370 r
= sd_bus_message_append(reply
, "(ss)", attach_type
, p
->bpffs_path
);
375 return sd_bus_message_close_container(reply
);
378 static int property_get_socket_bind(
381 const char *interface
,
382 const char *property
,
383 sd_bus_message
*reply
,
385 sd_bus_error
*error
) {
387 CGroupSocketBindItem
**items
= userdata
;
392 r
= sd_bus_message_open_container(reply
, 'a', "(iiqq)");
396 LIST_FOREACH(socket_bind_items
, i
, *items
) {
397 r
= sd_bus_message_append(reply
, "(iiqq)", i
->address_family
, i
->ip_protocol
, i
->nr_ports
, i
->port_min
);
402 return sd_bus_message_close_container(reply
);
405 static int property_get_restrict_network_interfaces(
408 const char *interface
,
409 const char *property
,
410 sd_bus_message
*reply
,
412 sd_bus_error
*error
) {
414 CGroupContext
*c
= userdata
;
421 r
= sd_bus_message_open_container(reply
, 'r', "bas");
425 r
= sd_bus_message_append(reply
, "b", c
->restrict_network_interfaces_is_allow_list
);
429 r
= sd_bus_message_open_container(reply
, 'a', "s");
433 SET_FOREACH(iface
, c
->restrict_network_interfaces
) {
434 r
= sd_bus_message_append(reply
, "s", iface
);
439 r
= sd_bus_message_close_container(reply
);
443 return sd_bus_message_close_container(reply
);
446 const sd_bus_vtable bus_cgroup_vtable
[] = {
447 SD_BUS_VTABLE_START(0),
448 SD_BUS_PROPERTY("Delegate", "b", bus_property_get_bool
, offsetof(CGroupContext
, delegate
), 0),
449 SD_BUS_PROPERTY("DelegateControllers", "as", property_get_delegate_controllers
, 0, 0),
450 SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, cpu_accounting
), 0),
451 SD_BUS_PROPERTY("CPUWeight", "t", NULL
, offsetof(CGroupContext
, cpu_weight
), 0),
452 SD_BUS_PROPERTY("StartupCPUWeight", "t", NULL
, offsetof(CGroupContext
, startup_cpu_weight
), 0),
453 SD_BUS_PROPERTY("CPUShares", "t", NULL
, offsetof(CGroupContext
, cpu_shares
), 0),
454 SD_BUS_PROPERTY("StartupCPUShares", "t", NULL
, offsetof(CGroupContext
, startup_cpu_shares
), 0),
455 SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_per_sec_usec
), 0),
456 SD_BUS_PROPERTY("CPUQuotaPeriodUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_period_usec
), 0),
457 SD_BUS_PROPERTY("AllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_cpus
), 0),
458 SD_BUS_PROPERTY("StartupAllowedCPUs", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_cpus
), 0),
459 SD_BUS_PROPERTY("AllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, cpuset_mems
), 0),
460 SD_BUS_PROPERTY("StartupAllowedMemoryNodes", "ay", property_get_cpuset
, offsetof(CGroupContext
, startup_cpuset_mems
), 0),
461 SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, io_accounting
), 0),
462 SD_BUS_PROPERTY("IOWeight", "t", NULL
, offsetof(CGroupContext
, io_weight
), 0),
463 SD_BUS_PROPERTY("StartupIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_io_weight
), 0),
464 SD_BUS_PROPERTY("IODeviceWeight", "a(st)", property_get_io_device_weight
, 0, 0),
465 SD_BUS_PROPERTY("IOReadBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
466 SD_BUS_PROPERTY("IOWriteBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
467 SD_BUS_PROPERTY("IOReadIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
468 SD_BUS_PROPERTY("IOWriteIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
469 SD_BUS_PROPERTY("IODeviceLatencyTargetUSec", "a(st)", property_get_io_device_latency
, 0, 0),
470 SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, blockio_accounting
), 0),
471 SD_BUS_PROPERTY("BlockIOWeight", "t", NULL
, offsetof(CGroupContext
, blockio_weight
), 0),
472 SD_BUS_PROPERTY("StartupBlockIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_blockio_weight
), 0),
473 SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight
, 0, 0),
474 SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
475 SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
476 SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, memory_accounting
), 0),
477 SD_BUS_PROPERTY("DefaultMemoryLow", "t", NULL
, offsetof(CGroupContext
, default_memory_low
), 0),
478 SD_BUS_PROPERTY("DefaultMemoryMin", "t", NULL
, offsetof(CGroupContext
, default_memory_min
), 0),
479 SD_BUS_PROPERTY("MemoryMin", "t", NULL
, offsetof(CGroupContext
, memory_min
), 0),
480 SD_BUS_PROPERTY("MemoryLow", "t", NULL
, offsetof(CGroupContext
, memory_low
), 0),
481 SD_BUS_PROPERTY("MemoryHigh", "t", NULL
, offsetof(CGroupContext
, memory_high
), 0),
482 SD_BUS_PROPERTY("MemoryMax", "t", NULL
, offsetof(CGroupContext
, memory_max
), 0),
483 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL
, offsetof(CGroupContext
, memory_swap_max
), 0),
484 SD_BUS_PROPERTY("MemoryLimit", "t", NULL
, offsetof(CGroupContext
, memory_limit
), 0),
485 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy
, offsetof(CGroupContext
, device_policy
), 0),
486 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow
, 0, 0),
487 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, tasks_accounting
), 0),
488 SD_BUS_PROPERTY("TasksMax", "t", bus_property_get_tasks_max
, offsetof(CGroupContext
, tasks_max
), 0),
489 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, ip_accounting
), 0),
490 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_allow
), 0),
491 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_deny
), 0),
492 SD_BUS_PROPERTY("IPIngressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_ingress
), 0),
493 SD_BUS_PROPERTY("IPEgressFilterPath", "as", NULL
, offsetof(CGroupContext
, ip_filters_egress
), 0),
494 SD_BUS_PROPERTY("DisableControllers", "as", property_get_cgroup_mask
, offsetof(CGroupContext
, disable_controllers
), 0),
495 SD_BUS_PROPERTY("ManagedOOMSwap", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_swap
), 0),
496 SD_BUS_PROPERTY("ManagedOOMMemoryPressure", "s", property_get_managed_oom_mode
, offsetof(CGroupContext
, moom_mem_pressure
), 0),
497 SD_BUS_PROPERTY("ManagedOOMMemoryPressureLimit", "u", NULL
, offsetof(CGroupContext
, moom_mem_pressure_limit
), 0),
498 SD_BUS_PROPERTY("ManagedOOMPreference", "s", property_get_managed_oom_preference
, offsetof(CGroupContext
, moom_preference
), 0),
499 SD_BUS_PROPERTY("BPFProgram", "a(ss)", property_get_bpf_foreign_program
, 0, 0),
500 SD_BUS_PROPERTY("SocketBindAllow", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_allow
), 0),
501 SD_BUS_PROPERTY("SocketBindDeny", "a(iiqq)", property_get_socket_bind
, offsetof(CGroupContext
, socket_bind_deny
), 0),
502 SD_BUS_PROPERTY("RestrictNetworkInterfaces", "(bas)", property_get_restrict_network_interfaces
, 0, 0),
506 static int bus_cgroup_set_transient_property(
510 sd_bus_message
*message
,
511 UnitWriteFlags flags
,
512 sd_bus_error
*error
) {
521 flags
|= UNIT_PRIVATE
;
523 if (streq(name
, "Delegate")) {
526 if (!UNIT_VTABLE(u
)->can_delegate
)
527 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
529 r
= sd_bus_message_read(message
, "b", &b
);
533 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
535 c
->delegate_controllers
= b
? _CGROUP_MASK_ALL
: 0;
537 unit_write_settingf(u
, flags
, name
, "Delegate=%s", yes_no(b
));
542 } else if (STR_IN_SET(name
, "DelegateControllers", "DisableControllers")) {
545 if (streq(name
, "DelegateControllers") && !UNIT_VTABLE(u
)->can_delegate
)
546 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
548 r
= sd_bus_message_enter_container(message
, 'a', "s");
556 r
= sd_bus_message_read(message
, "s", &t
);
562 cc
= cgroup_controller_from_string(t
);
564 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown cgroup controller '%s'", t
);
566 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
569 r
= sd_bus_message_exit_container(message
);
573 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
574 _cleanup_free_
char *t
= NULL
;
576 r
= cg_mask_to_string(mask
, &t
);
580 if (streq(name
, "DelegateControllers")) {
584 c
->delegate_controllers
= 0;
586 c
->delegate_controllers
|= mask
;
588 unit_write_settingf(u
, flags
, name
, "Delegate=%s", strempty(t
));
590 } else if (streq(name
, "DisableControllers")) {
593 c
->disable_controllers
= 0;
595 c
->disable_controllers
|= mask
;
597 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, strempty(t
));
602 } else if (STR_IN_SET(name
, "IPIngressFilterPath", "IPEgressFilterPath")) {
606 filters
= streq(name
, "IPIngressFilterPath") ? &c
->ip_filters_ingress
: &c
->ip_filters_egress
;
607 r
= sd_bus_message_enter_container(message
, 'a', "s");
614 r
= sd_bus_message_read(message
, "s", &path
);
620 if (!path_is_normalized(path
) || !path_is_absolute(path
))
621 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects a normalized absolute path.", name
);
623 if (!UNIT_WRITE_FLAGS_NOOP(flags
) && !strv_contains(*filters
, path
)) {
624 r
= strv_extend(filters
, path
);
630 r
= sd_bus_message_exit_container(message
);
634 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
635 _cleanup_free_
char *buf
= NULL
;
636 _cleanup_fclose_
FILE *f
= NULL
;
640 *filters
= strv_free(*filters
);
642 unit_invalidate_cgroup_bpf(u
);
643 f
= open_memstream_unlocked(&buf
, &size
);
650 STRV_FOREACH(entry
, *filters
)
651 fprintf(f
, "%s=%s\n", name
, *entry
);
653 r
= fflush_and_check(f
);
657 unit_write_setting(u
, flags
, name
, buf
);
660 r
= bpf_firewall_supported();
663 if (r
!= BPF_FIREWALL_SUPPORTED_WITH_MULTI
) {
664 static bool warned
= false;
666 log_full(warned
? LOG_DEBUG
: LOG_WARNING
,
667 "Transient unit %s configures an IP firewall with BPF, but the local system does not support BPF/cgroup firewalling with multiple filters.\n"
668 "Starting this unit will fail! (This warning is only shown for the first started transient unit using IP firewalling.)", u
->id
);
675 } else if (streq(name
, "BPFProgram")) {
679 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
683 while ((r
= sd_bus_message_read(message
, "(ss)", &a
, &p
)) > 0) {
684 int attach_type
= bpf_cgroup_attach_type_from_string(a
);
686 return sd_bus_error_setf(
688 SD_BUS_ERROR_INVALID_ARGS
,
689 "%s expects a valid BPF attach type, got '%s'.",
692 if (!path_is_normalized(p
) || !path_is_absolute(p
))
693 return sd_bus_error_setf(
695 SD_BUS_ERROR_INVALID_ARGS
,
696 "%s= expects a normalized absolute path.",
699 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
700 r
= cgroup_add_bpf_foreign_program(c
, attach_type
, p
);
709 r
= sd_bus_message_exit_container(message
);
713 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
714 _cleanup_free_
char *buf
= NULL
;
715 _cleanup_fclose_
FILE *f
= NULL
;
719 while (c
->bpf_foreign_programs
)
720 cgroup_context_remove_bpf_foreign_program(c
, c
->bpf_foreign_programs
);
722 f
= open_memstream_unlocked(&buf
, &size
);
729 LIST_FOREACH(programs
, fp
, c
->bpf_foreign_programs
)
730 fprintf(f
, "%s=%s:%s\n", name
,
731 bpf_cgroup_attach_type_to_string(fp
->attach_type
),
734 r
= fflush_and_check(f
);
738 unit_write_setting(u
, flags
, name
, buf
);
740 if (c
->bpf_foreign_programs
) {
741 r
= bpf_foreign_supported();
746 "Transient unit %s configures a BPF program pinned to BPF "
747 "filesystem, but the local system does not support that.\n"
748 "Starting this unit will fail!", u
->id
);
758 static int bus_cgroup_set_boolean(
763 sd_bus_message
*message
,
764 UnitWriteFlags flags
,
765 sd_bus_error
*error
) {
771 r
= sd_bus_message_read(message
, "b", &b
);
775 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
777 unit_invalidate_cgroup(u
, mask
);
778 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, yes_no(b
));
784 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
785 static int bus_cgroup_set_##function( \
789 sd_bus_message *message, \
790 UnitWriteFlags flags, \
791 sd_bus_error *error) { \
798 r = sd_bus_message_read(message, "t", &v); \
803 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
804 "Value specified in %s is out of range", name); \
806 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
808 unit_invalidate_cgroup(u, mask); \
811 unit_write_settingf(u, flags, name, \
814 unit_write_settingf(u, flags, name, \
815 "%s=%" PRIu64, name, v); \
821 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
822 static int bus_cgroup_set_##function( \
826 sd_bus_message *message, \
827 UnitWriteFlags flags, \
828 sd_bus_error *error) { \
835 r = sd_bus_message_read(message, "t", &v); \
840 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
841 "Value specified in %s is out of range", name); \
843 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
845 unit_invalidate_cgroup(u, mask); \
847 if (v == CGROUP_LIMIT_MAX) \
848 unit_write_settingf(u, flags, name, \
849 "%s=infinity", name); \
851 unit_write_settingf(u, flags, name, \
852 "%s=%" PRIu64, name, v); \
857 static int bus_cgroup_set_##function##_scale( \
861 sd_bus_message *message, \
862 UnitWriteFlags flags, \
863 sd_bus_error *error) { \
871 r = sd_bus_message_read(message, "u", &raw); \
875 v = scale(raw, UINT32_MAX); \
876 if (v < minimum || v >= UINT64_MAX) \
877 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
878 "Value specified in %s is out of range", name); \
880 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
882 unit_invalidate_cgroup(u, mask); \
884 /* Prepare to chop off suffix */ \
885 assert_se(endswith(name, "Scale")); \
887 int scaled = UINT32_SCALE_TO_PERMYRIAD(raw); \
888 unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR, \
889 (int)(strlen(name) - strlen("Scale")), name, \
890 PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled)); \
896 DISABLE_WARNING_TYPE_LIMITS
;
897 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_weight
, CGROUP_MASK_CPU
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
898 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares
, CGROUP_MASK_CPU
, CGROUP_CPU_SHARES_IS_OK
, CGROUP_CPU_SHARES_INVALID
);
899 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight
, CGROUP_MASK_IO
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
900 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight
, CGROUP_MASK_BLKIO
, CGROUP_BLKIO_WEIGHT_IS_OK
, CGROUP_BLKIO_WEIGHT_INVALID
);
901 BUS_DEFINE_SET_CGROUP_LIMIT(memory
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 1);
902 BUS_DEFINE_SET_CGROUP_LIMIT(memory_protection
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
903 BUS_DEFINE_SET_CGROUP_LIMIT(swap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
906 static int bus_cgroup_set_tasks_max(
910 sd_bus_message
*message
,
911 UnitWriteFlags flags
,
912 sd_bus_error
*error
) {
919 r
= sd_bus_message_read(message
, "t", &v
);
924 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
925 "Value specified in %s is out of range", name
);
927 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
928 *p
= (TasksMax
) { .value
= v
, .scale
= 0 }; /* When .scale==0, .value is the absolute value */
929 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
931 if (v
== CGROUP_LIMIT_MAX
)
932 unit_write_settingf(u
, flags
, name
,
933 "%s=infinity", name
);
935 unit_write_settingf(u
, flags
, name
,
936 "%s=%" PRIu64
, name
, v
);
942 static int bus_cgroup_set_tasks_max_scale(
946 sd_bus_message
*message
,
947 UnitWriteFlags flags
,
948 sd_bus_error
*error
) {
955 r
= sd_bus_message_read(message
, "u", &v
);
959 if (v
< 1 || v
>= UINT32_MAX
)
960 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
961 "Value specified in %s is out of range", name
);
963 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
964 *p
= (TasksMax
) { v
, UINT32_MAX
}; /* .scale is not 0, so this is interpreted as v/UINT32_MAX. */
965 unit_invalidate_cgroup(u
, CGROUP_MASK_PIDS
);
967 uint32_t scaled
= DIV_ROUND_UP((uint64_t) v
* 100U, (uint64_t) UINT32_MAX
);
968 unit_write_settingf(u
, flags
, name
, "%s=%" PRIu32
".%" PRIu32
"%%", "TasksMax",
969 scaled
/ 10, scaled
% 10);
975 int bus_cgroup_set_property(
979 sd_bus_message
*message
,
980 UnitWriteFlags flags
,
981 sd_bus_error
*error
) {
983 CGroupIOLimitType iol_type
;
991 flags
|= UNIT_PRIVATE
;
993 if (streq(name
, "CPUAccounting"))
994 return bus_cgroup_set_boolean(u
, name
, &c
->cpu_accounting
, get_cpu_accounting_mask(), message
, flags
, error
);
996 if (streq(name
, "CPUWeight"))
997 return bus_cgroup_set_cpu_weight(u
, name
, &c
->cpu_weight
, message
, flags
, error
);
999 if (streq(name
, "StartupCPUWeight"))
1000 return bus_cgroup_set_cpu_weight(u
, name
, &c
->startup_cpu_weight
, message
, flags
, error
);
1002 if (streq(name
, "CPUShares"))
1003 return bus_cgroup_set_cpu_shares(u
, name
, &c
->cpu_shares
, message
, flags
, error
);
1005 if (streq(name
, "StartupCPUShares"))
1006 return bus_cgroup_set_cpu_shares(u
, name
, &c
->startup_cpu_shares
, message
, flags
, error
);
1008 if (streq(name
, "IOAccounting"))
1009 return bus_cgroup_set_boolean(u
, name
, &c
->io_accounting
, CGROUP_MASK_IO
, message
, flags
, error
);
1011 if (streq(name
, "IOWeight"))
1012 return bus_cgroup_set_io_weight(u
, name
, &c
->io_weight
, message
, flags
, error
);
1014 if (streq(name
, "StartupIOWeight"))
1015 return bus_cgroup_set_io_weight(u
, name
, &c
->startup_io_weight
, message
, flags
, error
);
1017 if (streq(name
, "BlockIOAccounting"))
1018 return bus_cgroup_set_boolean(u
, name
, &c
->blockio_accounting
, CGROUP_MASK_BLKIO
, message
, flags
, error
);
1020 if (streq(name
, "BlockIOWeight"))
1021 return bus_cgroup_set_blockio_weight(u
, name
, &c
->blockio_weight
, message
, flags
, error
);
1023 if (streq(name
, "StartupBlockIOWeight"))
1024 return bus_cgroup_set_blockio_weight(u
, name
, &c
->startup_blockio_weight
, message
, flags
, error
);
1026 if (streq(name
, "MemoryAccounting"))
1027 return bus_cgroup_set_boolean(u
, name
, &c
->memory_accounting
, CGROUP_MASK_MEMORY
, message
, flags
, error
);
1029 if (streq(name
, "MemoryMin")) {
1030 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_min
, message
, flags
, error
);
1032 c
->memory_min_set
= true;
1036 if (streq(name
, "MemoryLow")) {
1037 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->memory_low
, message
, flags
, error
);
1039 c
->memory_low_set
= true;
1043 if (streq(name
, "DefaultMemoryMin")) {
1044 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1046 c
->default_memory_min_set
= true;
1050 if (streq(name
, "DefaultMemoryLow")) {
1051 r
= bus_cgroup_set_memory_protection(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1053 c
->default_memory_low_set
= true;
1057 if (streq(name
, "MemoryHigh"))
1058 return bus_cgroup_set_memory(u
, name
, &c
->memory_high
, message
, flags
, error
);
1060 if (streq(name
, "MemorySwapMax"))
1061 return bus_cgroup_set_swap(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1063 if (streq(name
, "MemoryMax"))
1064 return bus_cgroup_set_memory(u
, name
, &c
->memory_max
, message
, flags
, error
);
1066 if (streq(name
, "MemoryLimit"))
1067 return bus_cgroup_set_memory(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1069 if (streq(name
, "MemoryMinScale")) {
1070 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_min
, message
, flags
, error
);
1072 c
->memory_min_set
= true;
1076 if (streq(name
, "MemoryLowScale")) {
1077 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->memory_low
, message
, flags
, error
);
1079 c
->memory_low_set
= true;
1083 if (streq(name
, "DefaultMemoryMinScale")) {
1084 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_min
, message
, flags
, error
);
1086 c
->default_memory_min_set
= true;
1090 if (streq(name
, "DefaultMemoryLowScale")) {
1091 r
= bus_cgroup_set_memory_protection_scale(u
, name
, &c
->default_memory_low
, message
, flags
, error
);
1093 c
->default_memory_low_set
= true;
1097 if (streq(name
, "MemoryHighScale"))
1098 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_high
, message
, flags
, error
);
1100 if (streq(name
, "MemorySwapMaxScale"))
1101 return bus_cgroup_set_swap_scale(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
1103 if (streq(name
, "MemoryMaxScale"))
1104 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_max
, message
, flags
, error
);
1106 if (streq(name
, "MemoryLimitScale"))
1107 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_limit
, message
, flags
, error
);
1109 if (streq(name
, "TasksAccounting"))
1110 return bus_cgroup_set_boolean(u
, name
, &c
->tasks_accounting
, CGROUP_MASK_PIDS
, message
, flags
, error
);
1112 if (streq(name
, "TasksMax"))
1113 return bus_cgroup_set_tasks_max(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1115 if (streq(name
, "TasksMaxScale"))
1116 return bus_cgroup_set_tasks_max_scale(u
, name
, &c
->tasks_max
, message
, flags
, error
);
1118 if (streq(name
, "CPUQuotaPerSecUSec")) {
1121 r
= sd_bus_message_read(message
, "t", &u64
);
1126 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "CPUQuotaPerSecUSec= value out of range");
1128 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1129 c
->cpu_quota_per_sec_usec
= u64
;
1130 u
->warned_clamping_cpu_quota_period
= false;
1131 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1133 if (c
->cpu_quota_per_sec_usec
== USEC_INFINITY
)
1134 unit_write_setting(u
, flags
, "CPUQuota", "CPUQuota=");
1136 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
1138 unit_write_settingf(u
, flags
, "CPUQuota",
1140 (double) (c
->cpu_quota_per_sec_usec
/ 10000));
1145 } else if (streq(name
, "CPUQuotaPeriodUSec")) {
1148 r
= sd_bus_message_read(message
, "t", &u64
);
1152 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1153 c
->cpu_quota_period_usec
= u64
;
1154 u
->warned_clamping_cpu_quota_period
= false;
1155 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
1156 if (c
->cpu_quota_period_usec
== USEC_INFINITY
)
1157 unit_write_setting(u
, flags
, "CPUQuotaPeriodSec", "CPUQuotaPeriodSec=");
1159 unit_write_settingf(u
, flags
, "CPUQuotaPeriodSec",
1160 "CPUQuotaPeriodSec=%s",
1161 FORMAT_TIMESPAN(c
->cpu_quota_period_usec
, 1));
1166 } else if (STR_IN_SET(name
, "AllowedCPUs", "StartupAllowedCPUs", "AllowedMemoryNodes", "StartupAllowedMemoryNodes")) {
1169 _cleanup_(cpu_set_reset
) CPUSet new_set
= {};
1171 r
= sd_bus_message_read_array(message
, 'y', &a
, &n
);
1175 r
= cpu_set_from_dbus(a
, n
, &new_set
);
1179 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1180 _cleanup_free_
char *setstr
= NULL
;
1183 setstr
= cpu_set_to_range_string(&new_set
);
1187 if (streq(name
, "AllowedCPUs"))
1188 set
= &c
->cpuset_cpus
;
1189 else if (streq(name
, "StartupAllowedCPUs"))
1190 set
= &c
->startup_cpuset_cpus
;
1191 else if (streq(name
, "AllowedMemoryNodes"))
1192 set
= &c
->cpuset_mems
;
1193 else if (streq(name
, "StartupAllowedMemoryNodes"))
1194 set
= &c
->startup_cpuset_mems
;
1200 new_set
= (CPUSet
) {};
1202 unit_invalidate_cgroup(u
, CGROUP_MASK_CPUSET
);
1203 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, setstr
);
1208 } else if ((iol_type
= cgroup_io_limit_type_from_string(name
)) >= 0) {
1213 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1217 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1219 if (!path_is_normalized(path
))
1220 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1222 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1223 CGroupIODeviceLimit
*a
= NULL
;
1225 LIST_FOREACH(device_limits
, b
, c
->io_device_limits
)
1226 if (path_equal(path
, b
->path
)) {
1232 CGroupIOLimitType type
;
1234 a
= new0(CGroupIODeviceLimit
, 1);
1238 a
->path
= strdup(path
);
1244 for (type
= 0; type
< _CGROUP_IO_LIMIT_TYPE_MAX
; type
++)
1245 a
->limits
[type
] = cgroup_io_limit_defaults
[type
];
1247 LIST_PREPEND(device_limits
, c
->io_device_limits
, a
);
1250 a
->limits
[iol_type
] = u64
;
1258 r
= sd_bus_message_exit_container(message
);
1262 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1263 _cleanup_free_
char *buf
= NULL
;
1264 _cleanup_fclose_
FILE *f
= NULL
;
1268 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1269 a
->limits
[iol_type
] = cgroup_io_limit_defaults
[iol_type
];
1271 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1273 f
= open_memstream_unlocked(&buf
, &size
);
1277 fprintf(f
, "%s=\n", name
);
1278 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
1279 if (a
->limits
[iol_type
] != cgroup_io_limit_defaults
[iol_type
])
1280 fprintf(f
, "%s=%s %" PRIu64
"\n", name
, a
->path
, a
->limits
[iol_type
]);
1282 r
= fflush_and_check(f
);
1285 unit_write_setting(u
, flags
, name
, buf
);
1290 } else if (streq(name
, "IODeviceWeight")) {
1295 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1299 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1301 if (!path_is_normalized(path
))
1302 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1304 if (!CGROUP_WEIGHT_IS_OK(weight
) || weight
== CGROUP_WEIGHT_INVALID
)
1305 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "IODeviceWeight= value out of range");
1307 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1308 CGroupIODeviceWeight
*a
= NULL
;
1310 LIST_FOREACH(device_weights
, b
, c
->io_device_weights
)
1311 if (path_equal(b
->path
, path
)) {
1317 a
= new0(CGroupIODeviceWeight
, 1);
1321 a
->path
= strdup(path
);
1326 LIST_PREPEND(device_weights
, c
->io_device_weights
, a
);
1335 r
= sd_bus_message_exit_container(message
);
1339 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1340 _cleanup_free_
char *buf
= NULL
;
1341 _cleanup_fclose_
FILE *f
= NULL
;
1345 while (c
->io_device_weights
)
1346 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
1348 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1350 f
= open_memstream_unlocked(&buf
, &size
);
1354 fputs("IODeviceWeight=\n", f
);
1355 LIST_FOREACH(device_weights
, a
, c
->io_device_weights
)
1356 fprintf(f
, "IODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1358 r
= fflush_and_check(f
);
1361 unit_write_setting(u
, flags
, name
, buf
);
1366 } else if (streq(name
, "IODeviceLatencyTargetUSec")) {
1371 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1375 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &target
)) > 0) {
1377 if (!path_is_normalized(path
))
1378 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1380 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1381 CGroupIODeviceLatency
*a
= NULL
;
1383 LIST_FOREACH(device_latencies
, b
, c
->io_device_latencies
)
1384 if (path_equal(b
->path
, path
)) {
1390 a
= new0(CGroupIODeviceLatency
, 1);
1394 a
->path
= strdup(path
);
1399 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, a
);
1402 a
->target_usec
= target
;
1408 r
= sd_bus_message_exit_container(message
);
1412 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1413 _cleanup_free_
char *buf
= NULL
;
1414 _cleanup_fclose_
FILE *f
= NULL
;
1418 while (c
->io_device_latencies
)
1419 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
1421 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
1423 f
= open_memstream_unlocked(&buf
, &size
);
1427 fputs("IODeviceLatencyTargetSec=\n", f
);
1428 LIST_FOREACH(device_latencies
, a
, c
->io_device_latencies
)
1429 fprintf(f
, "IODeviceLatencyTargetSec=%s %s\n",
1430 a
->path
, FORMAT_TIMESPAN(a
->target_usec
, 1));
1432 r
= fflush_and_check(f
);
1435 unit_write_setting(u
, flags
, name
, buf
);
1440 } else if (STR_IN_SET(name
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
1446 read
= streq(name
, "BlockIOReadBandwidth");
1448 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1452 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
1454 if (!path_is_normalized(path
))
1455 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1457 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1458 CGroupBlockIODeviceBandwidth
*a
= NULL
;
1460 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
)
1461 if (path_equal(path
, b
->path
)) {
1467 a
= new0(CGroupBlockIODeviceBandwidth
, 1);
1471 a
->rbps
= CGROUP_LIMIT_MAX
;
1472 a
->wbps
= CGROUP_LIMIT_MAX
;
1473 a
->path
= strdup(path
);
1479 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, a
);
1493 r
= sd_bus_message_exit_container(message
);
1497 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1498 _cleanup_free_
char *buf
= NULL
;
1499 _cleanup_fclose_
FILE *f
= NULL
;
1503 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
) {
1505 a
->rbps
= CGROUP_LIMIT_MAX
;
1507 a
->wbps
= CGROUP_LIMIT_MAX
;
1510 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1512 f
= open_memstream_unlocked(&buf
, &size
);
1517 fputs("BlockIOReadBandwidth=\n", f
);
1518 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1519 if (a
->rbps
!= CGROUP_LIMIT_MAX
)
1520 fprintf(f
, "BlockIOReadBandwidth=%s %" PRIu64
"\n", a
->path
, a
->rbps
);
1522 fputs("BlockIOWriteBandwidth=\n", f
);
1523 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1524 if (a
->wbps
!= CGROUP_LIMIT_MAX
)
1525 fprintf(f
, "BlockIOWriteBandwidth=%s %" PRIu64
"\n", a
->path
, a
->wbps
);
1528 r
= fflush_and_check(f
);
1532 unit_write_setting(u
, flags
, name
, buf
);
1537 } else if (streq(name
, "BlockIODeviceWeight")) {
1542 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1546 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1548 if (!path_is_normalized(path
))
1549 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1551 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight
) || weight
== CGROUP_BLKIO_WEIGHT_INVALID
)
1552 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "BlockIODeviceWeight= out of range");
1554 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1555 CGroupBlockIODeviceWeight
*a
= NULL
;
1557 LIST_FOREACH(device_weights
, b
, c
->blockio_device_weights
)
1558 if (path_equal(b
->path
, path
)) {
1564 a
= new0(CGroupBlockIODeviceWeight
, 1);
1568 a
->path
= strdup(path
);
1573 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, a
);
1582 r
= sd_bus_message_exit_container(message
);
1586 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1587 _cleanup_free_
char *buf
= NULL
;
1588 _cleanup_fclose_
FILE *f
= NULL
;
1592 while (c
->blockio_device_weights
)
1593 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
1595 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1597 f
= open_memstream_unlocked(&buf
, &size
);
1601 fputs("BlockIODeviceWeight=\n", f
);
1602 LIST_FOREACH(device_weights
, a
, c
->blockio_device_weights
)
1603 fprintf(f
, "BlockIODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1605 r
= fflush_and_check(f
);
1609 unit_write_setting(u
, flags
, name
, buf
);
1614 } else if (streq(name
, "DevicePolicy")) {
1616 CGroupDevicePolicy p
;
1618 r
= sd_bus_message_read(message
, "s", &policy
);
1622 p
= cgroup_device_policy_from_string(policy
);
1626 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1627 c
->device_policy
= p
;
1628 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1629 unit_write_settingf(u
, flags
, name
, "DevicePolicy=%s", policy
);
1634 } else if (streq(name
, "DeviceAllow")) {
1635 const char *path
, *rwm
;
1638 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
1642 while ((r
= sd_bus_message_read(message
, "(ss)", &path
, &rwm
)) > 0) {
1644 if (!valid_device_allow_pattern(path
) || strpbrk(path
, WHITESPACE
))
1645 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires device node or pattern");
1649 else if (!in_charset(rwm
, "rwm"))
1650 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires combination of rwm flags");
1652 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1653 CGroupDeviceAllow
*a
= NULL
;
1655 LIST_FOREACH(device_allow
, b
, c
->device_allow
)
1656 if (path_equal(b
->path
, path
)) {
1662 a
= new0(CGroupDeviceAllow
, 1);
1666 a
->path
= strdup(path
);
1672 LIST_PREPEND(device_allow
, c
->device_allow
, a
);
1675 a
->r
= strchr(rwm
, 'r');
1676 a
->w
= strchr(rwm
, 'w');
1677 a
->m
= strchr(rwm
, 'm');
1685 r
= sd_bus_message_exit_container(message
);
1689 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1690 _cleanup_free_
char *buf
= NULL
;
1691 _cleanup_fclose_
FILE *f
= NULL
;
1695 while (c
->device_allow
)
1696 cgroup_context_free_device_allow(c
, c
->device_allow
);
1698 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1700 f
= open_memstream_unlocked(&buf
, &size
);
1704 fputs("DeviceAllow=\n", f
);
1705 LIST_FOREACH(device_allow
, a
, c
->device_allow
)
1706 fprintf(f
, "DeviceAllow=%s %s%s%s\n", a
->path
, a
->r
? "r" : "", a
->w
? "w" : "", a
->m
? "m" : "");
1708 r
= fflush_and_check(f
);
1711 unit_write_setting(u
, flags
, name
, buf
);
1716 } else if (streq(name
, "IPAccounting")) {
1719 r
= sd_bus_message_read(message
, "b", &b
);
1723 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1724 c
->ip_accounting
= b
;
1726 unit_invalidate_cgroup_bpf(u
);
1727 unit_write_settingf(u
, flags
, name
, "IPAccounting=%s", yes_no(b
));
1732 } else if (STR_IN_SET(name
, "IPAddressAllow", "IPAddressDeny")) {
1733 _cleanup_set_free_ Set
*new_prefixes
= NULL
;
1736 r
= sd_bus_message_enter_container(message
, 'a', "(iayu)");
1746 r
= sd_bus_message_enter_container(message
, 'r', "iayu");
1752 r
= sd_bus_message_read(message
, "i", &family
);
1756 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1757 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects IPv4 or IPv6 addresses only.", name
);
1759 r
= sd_bus_message_read_array(message
, 'y', &ap
, &an
);
1763 if (an
!= FAMILY_ADDRESS_SIZE(family
))
1764 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1765 af_to_name(family
), FAMILY_ADDRESS_SIZE(family
), an
);
1767 r
= sd_bus_message_read(message
, "u", &prefixlen
);
1771 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
)*8)
1772 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
));
1774 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1775 struct in_addr_prefix prefix
= {
1777 .prefixlen
= prefixlen
,
1780 memcpy(&prefix
.address
, ap
, an
);
1782 r
= in_addr_prefix_add(&new_prefixes
, &prefix
);
1787 r
= sd_bus_message_exit_container(message
);
1794 r
= sd_bus_message_exit_container(message
);
1798 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1799 _cleanup_free_
char *buf
= NULL
;
1800 _cleanup_fclose_
FILE *f
= NULL
;
1805 unit_invalidate_cgroup_bpf(u
);
1806 f
= open_memstream_unlocked(&buf
, &size
);
1810 prefixes
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow
: &c
->ip_address_deny
;
1811 reduced
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow_reduced
: &c
->ip_address_deny_reduced
;
1815 *prefixes
= set_free(*prefixes
);
1821 r
= in_addr_prefixes_merge(prefixes
, new_prefixes
);
1825 const struct in_addr_prefix
*p
;
1826 SET_FOREACH(p
, new_prefixes
)
1827 fprintf(f
, "%s=%s\n", name
,
1828 IN_ADDR_PREFIX_TO_STRING(p
->family
, &p
->address
, p
->prefixlen
));
1831 r
= fflush_and_check(f
);
1835 unit_write_setting(u
, flags
, name
, buf
);
1841 if (STR_IN_SET(name
, "ManagedOOMSwap", "ManagedOOMMemoryPressure")) {
1842 ManagedOOMMode
*cgroup_mode
= streq(name
, "ManagedOOMSwap") ? &c
->moom_swap
: &c
->moom_mem_pressure
;
1846 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
1847 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
1849 r
= sd_bus_message_read(message
, "s", &mode
);
1853 m
= managed_oom_mode_from_string(mode
);
1857 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1859 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, mode
);
1862 (void) manager_varlink_send_managed_oom_update(u
);
1866 if (streq(name
, "ManagedOOMMemoryPressureLimit")) {
1869 if (!UNIT_VTABLE(u
)->can_set_managed_oom
)
1870 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot set %s for this unit type", name
);
1872 r
= sd_bus_message_read(message
, "u", &v
);
1876 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1877 c
->moom_mem_pressure_limit
= v
;
1878 unit_write_settingf(u
, flags
, name
,
1879 "ManagedOOMMemoryPressureLimit=" PERMYRIAD_AS_PERCENT_FORMAT_STR
,
1880 PERMYRIAD_AS_PERCENT_FORMAT_VAL(UINT32_SCALE_TO_PERMYRIAD(v
)));
1883 if (c
->moom_mem_pressure
== MANAGED_OOM_KILL
)
1884 (void) manager_varlink_send_managed_oom_update(u
);
1889 if (streq(name
, "ManagedOOMPreference")) {
1890 ManagedOOMPreference p
;
1893 r
= sd_bus_message_read(message
, "s", &pref
);
1897 p
= managed_oom_preference_from_string(pref
);
1901 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1902 c
->moom_preference
= p
;
1903 unit_write_settingf(u
, flags
, name
, "ManagedOOMPreference=%s", pref
);
1908 if (STR_IN_SET(name
, "SocketBindAllow", "SocketBindDeny")) {
1909 CGroupSocketBindItem
**list
;
1910 uint16_t nr_ports
, port_min
;
1912 int32_t family
, ip_protocol
;
1914 list
= streq(name
, "SocketBindAllow") ? &c
->socket_bind_allow
: &c
->socket_bind_deny
;
1916 r
= sd_bus_message_enter_container(message
, 'a', "(iiqq)");
1920 while ((r
= sd_bus_message_read(message
, "(iiqq)", &family
, &ip_protocol
, &nr_ports
, &port_min
)) > 0) {
1922 if (!IN_SET(family
, AF_UNSPEC
, AF_INET
, AF_INET6
))
1923 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects INET or INET6 family, if specified.", name
);
1925 if (!IN_SET(ip_protocol
, 0, IPPROTO_TCP
, IPPROTO_UDP
))
1926 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects TCP or UDP protocol, if specified.", name
);
1928 if (port_min
+ (uint32_t) nr_ports
> (1 << 16))
1929 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects maximum port value lesser than 65536.", name
);
1931 if (port_min
== 0 && nr_ports
!= 0)
1932 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects port range starting with positive value.", name
);
1934 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1935 _cleanup_free_ CGroupSocketBindItem
*item
= NULL
;
1937 item
= new(CGroupSocketBindItem
, 1);
1941 *item
= (CGroupSocketBindItem
) {
1942 .address_family
= family
,
1943 .ip_protocol
= ip_protocol
,
1944 .nr_ports
= nr_ports
,
1945 .port_min
= port_min
1948 LIST_PREPEND(socket_bind_items
, *list
, TAKE_PTR(item
));
1955 r
= sd_bus_message_exit_container(message
);
1959 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1960 _cleanup_free_
char *buf
= NULL
;
1961 _cleanup_fclose_
FILE *f
= NULL
;
1965 cgroup_context_remove_socket_bind(list
);
1967 if ((u
->manager
->cgroup_supported
& CGROUP_MASK_BPF_SOCKET_BIND
) == 0)
1969 "Unit %s configures source compiled BPF programs "
1970 "but the local system does not support that.\n"
1971 "Starting this unit will fail!", u
->id
);
1974 f
= open_memstream_unlocked(&buf
, &size
);
1978 fprintf(f
, "%s:", name
);
1980 LIST_FOREACH(socket_bind_items
, item
, *list
)
1981 cgroup_context_dump_socket_bind_item(item
, f
);
1985 r
= fflush_and_check(f
);
1989 unit_write_setting(u
, flags
, name
, buf
);
1994 if (streq(name
, "RestrictNetworkInterfaces")) {
1996 _cleanup_strv_free_
char **l
= NULL
;
1998 r
= sd_bus_message_enter_container(message
, 'r', "bas");
2002 r
= sd_bus_message_read(message
, "b", &is_allow_list
);
2006 r
= sd_bus_message_read_strv(message
, &l
);
2010 r
= sd_bus_message_exit_container(message
);
2014 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
2015 _cleanup_free_
char *joined
= NULL
;
2017 if (strv_isempty(l
)) {
2018 c
->restrict_network_interfaces_is_allow_list
= false;
2019 c
->restrict_network_interfaces
= set_free(c
->restrict_network_interfaces
);
2021 unit_write_settingf(u
, flags
, name
, "%s=", name
);
2025 if (set_isempty(c
->restrict_network_interfaces
))
2026 c
->restrict_network_interfaces_is_allow_list
= is_allow_list
;
2028 STRV_FOREACH(s
, l
) {
2029 if (!ifname_valid(*s
)) {
2030 log_full(LOG_WARNING
, "Invalid interface name, ignoring: %s", *s
);
2033 if (c
->restrict_network_interfaces_is_allow_list
!= (bool) is_allow_list
)
2034 free(set_remove(c
->restrict_network_interfaces
, *s
));
2036 r
= set_put_strdup(&c
->restrict_network_interfaces
, *s
);
2042 joined
= strv_join(l
, " ");
2046 unit_write_settingf(u
, flags
, name
, "%s=%s%s", name
, is_allow_list
? "" : "~", joined
);
2052 if (streq(name
, "DisableControllers") || (u
->transient
&& u
->load_state
== UNIT_STUB
))
2053 return bus_cgroup_set_transient_property(u
, c
, name
, message
, flags
, error
);