1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #include "bpf-firewall.h"
10 #include "cgroup-util.h"
12 #include "dbus-cgroup.h"
13 #include "dbus-util.h"
16 #include "path-util.h"
18 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_cgroup_device_policy
, cgroup_device_policy
, CGroupDevicePolicy
);
20 static int property_get_delegate_controllers(
23 const char *interface
,
25 sd_bus_message
*reply
,
27 sd_bus_error
*error
) {
29 CGroupContext
*c
= userdata
;
38 return sd_bus_message_append(reply
, "as", 0);
40 r
= sd_bus_message_open_container(reply
, 'a', "s");
44 for (cc
= 0; cc
< _CGROUP_CONTROLLER_MAX
; cc
++) {
45 if ((c
->delegate_controllers
& CGROUP_CONTROLLER_TO_MASK(cc
)) == 0)
48 r
= sd_bus_message_append(reply
, "s", cgroup_controller_to_string(cc
));
53 return sd_bus_message_close_container(reply
);
56 static int property_get_io_device_weight(
59 const char *interface
,
61 sd_bus_message
*reply
,
63 sd_bus_error
*error
) {
65 CGroupContext
*c
= userdata
;
66 CGroupIODeviceWeight
*w
;
73 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
77 LIST_FOREACH(device_weights
, w
, c
->io_device_weights
) {
78 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
83 return sd_bus_message_close_container(reply
);
86 static int property_get_io_device_limits(
89 const char *interface
,
91 sd_bus_message
*reply
,
93 sd_bus_error
*error
) {
95 CGroupContext
*c
= userdata
;
96 CGroupIODeviceLimit
*l
;
103 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
107 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
) {
108 CGroupIOLimitType type
;
110 type
= cgroup_io_limit_type_from_string(property
);
111 if (type
< 0 || l
->limits
[type
] == cgroup_io_limit_defaults
[type
])
114 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->limits
[type
]);
119 return sd_bus_message_close_container(reply
);
122 static int property_get_blockio_device_weight(
125 const char *interface
,
126 const char *property
,
127 sd_bus_message
*reply
,
129 sd_bus_error
*error
) {
131 CGroupContext
*c
= userdata
;
132 CGroupBlockIODeviceWeight
*w
;
139 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
143 LIST_FOREACH(device_weights
, w
, c
->blockio_device_weights
) {
144 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
149 return sd_bus_message_close_container(reply
);
152 static int property_get_blockio_device_bandwidths(
155 const char *interface
,
156 const char *property
,
157 sd_bus_message
*reply
,
159 sd_bus_error
*error
) {
161 CGroupContext
*c
= userdata
;
162 CGroupBlockIODeviceBandwidth
*b
;
169 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
173 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
176 if (streq(property
, "BlockIOReadBandwidth"))
181 if (v
== CGROUP_LIMIT_MAX
)
184 r
= sd_bus_message_append(reply
, "(st)", b
->path
, v
);
189 return sd_bus_message_close_container(reply
);
192 static int property_get_device_allow(
195 const char *interface
,
196 const char *property
,
197 sd_bus_message
*reply
,
199 sd_bus_error
*error
) {
201 CGroupContext
*c
= userdata
;
202 CGroupDeviceAllow
*a
;
209 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
213 LIST_FOREACH(device_allow
, a
, c
->device_allow
) {
226 r
= sd_bus_message_append(reply
, "(ss)", a
->path
, rwm
);
231 return sd_bus_message_close_container(reply
);
234 static int property_get_ip_address_access(
237 const char *interface
,
238 const char *property
,
239 sd_bus_message
*reply
,
241 sd_bus_error
*error
) {
243 IPAddressAccessItem
** items
= userdata
, *i
;
246 r
= sd_bus_message_open_container(reply
, 'a', "(iayu)");
250 LIST_FOREACH(items
, i
, *items
) {
252 r
= sd_bus_message_open_container(reply
, 'r', "iayu");
256 r
= sd_bus_message_append(reply
, "i", i
->family
);
260 r
= sd_bus_message_append_array(reply
, 'y', &i
->address
, FAMILY_ADDRESS_SIZE(i
->family
));
264 r
= sd_bus_message_append(reply
, "u", (uint32_t) i
->prefixlen
);
268 r
= sd_bus_message_close_container(reply
);
273 return sd_bus_message_close_container(reply
);
276 const sd_bus_vtable bus_cgroup_vtable
[] = {
277 SD_BUS_VTABLE_START(0),
278 SD_BUS_PROPERTY("Delegate", "b", bus_property_get_bool
, offsetof(CGroupContext
, delegate
), 0),
279 SD_BUS_PROPERTY("DelegateControllers", "as", property_get_delegate_controllers
, 0, 0),
280 SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, cpu_accounting
), 0),
281 SD_BUS_PROPERTY("CPUWeight", "t", NULL
, offsetof(CGroupContext
, cpu_weight
), 0),
282 SD_BUS_PROPERTY("StartupCPUWeight", "t", NULL
, offsetof(CGroupContext
, startup_cpu_weight
), 0),
283 SD_BUS_PROPERTY("CPUShares", "t", NULL
, offsetof(CGroupContext
, cpu_shares
), 0),
284 SD_BUS_PROPERTY("StartupCPUShares", "t", NULL
, offsetof(CGroupContext
, startup_cpu_shares
), 0),
285 SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_per_sec_usec
), 0),
286 SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, io_accounting
), 0),
287 SD_BUS_PROPERTY("IOWeight", "t", NULL
, offsetof(CGroupContext
, io_weight
), 0),
288 SD_BUS_PROPERTY("StartupIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_io_weight
), 0),
289 SD_BUS_PROPERTY("IODeviceWeight", "a(st)", property_get_io_device_weight
, 0, 0),
290 SD_BUS_PROPERTY("IOReadBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
291 SD_BUS_PROPERTY("IOWriteBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
292 SD_BUS_PROPERTY("IOReadIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
293 SD_BUS_PROPERTY("IOWriteIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
294 SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, blockio_accounting
), 0),
295 SD_BUS_PROPERTY("BlockIOWeight", "t", NULL
, offsetof(CGroupContext
, blockio_weight
), 0),
296 SD_BUS_PROPERTY("StartupBlockIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_blockio_weight
), 0),
297 SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight
, 0, 0),
298 SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
299 SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
300 SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, memory_accounting
), 0),
301 SD_BUS_PROPERTY("MemoryLow", "t", NULL
, offsetof(CGroupContext
, memory_low
), 0),
302 SD_BUS_PROPERTY("MemoryHigh", "t", NULL
, offsetof(CGroupContext
, memory_high
), 0),
303 SD_BUS_PROPERTY("MemoryMax", "t", NULL
, offsetof(CGroupContext
, memory_max
), 0),
304 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL
, offsetof(CGroupContext
, memory_swap_max
), 0),
305 SD_BUS_PROPERTY("MemoryLimit", "t", NULL
, offsetof(CGroupContext
, memory_limit
), 0),
306 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy
, offsetof(CGroupContext
, device_policy
), 0),
307 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow
, 0, 0),
308 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, tasks_accounting
), 0),
309 SD_BUS_PROPERTY("TasksMax", "t", NULL
, offsetof(CGroupContext
, tasks_max
), 0),
310 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, ip_accounting
), 0),
311 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_allow
), 0),
312 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_deny
), 0),
316 static int bus_cgroup_set_transient_property(
320 sd_bus_message
*message
,
321 UnitWriteFlags flags
,
322 sd_bus_error
*error
) {
331 flags
|= UNIT_PRIVATE
;
333 if (streq(name
, "Delegate")) {
336 if (!UNIT_VTABLE(u
)->can_delegate
)
337 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
339 r
= sd_bus_message_read(message
, "b", &b
);
343 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
345 c
->delegate_controllers
= b
? _CGROUP_MASK_ALL
: 0;
347 unit_write_settingf(u
, flags
, name
, "Delegate=%s", yes_no(b
));
352 } else if (streq(name
, "DelegateControllers")) {
355 if (!UNIT_VTABLE(u
)->can_delegate
)
356 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
358 r
= sd_bus_message_enter_container(message
, 'a', "s");
366 r
= sd_bus_message_read(message
, "s", &t
);
372 cc
= cgroup_controller_from_string(t
);
374 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown cgroup controller '%s'", t
);
376 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
379 r
= sd_bus_message_exit_container(message
);
383 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
384 _cleanup_free_
char *t
= NULL
;
386 r
= cg_mask_to_string(mask
, &t
);
392 c
->delegate_controllers
= 0;
394 c
->delegate_controllers
|= mask
;
396 unit_write_settingf(u
, flags
, name
, "Delegate=%s", strempty(t
));
405 static int bus_cgroup_set_boolean(
410 sd_bus_message
*message
,
411 UnitWriteFlags flags
,
412 sd_bus_error
*error
) {
418 r
= sd_bus_message_read(message
, "b", &b
);
422 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
424 unit_invalidate_cgroup(u
, mask
);
425 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, yes_no(b
));
431 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
432 static int bus_cgroup_set_##function( \
436 sd_bus_message *message, \
437 UnitWriteFlags flags, \
438 sd_bus_error *error) { \
445 r = sd_bus_message_read(message, "t", &v); \
450 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
451 "Value specified in %s is out of range", name); \
453 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
455 unit_invalidate_cgroup(u, (mask)); \
458 unit_write_settingf(u, flags, name, \
461 unit_write_settingf(u, flags, name, \
462 "%s=%" PRIu64, name, v); \
468 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
469 static int bus_cgroup_set_##function( \
473 sd_bus_message *message, \
474 UnitWriteFlags flags, \
475 sd_bus_error *error) { \
482 r = sd_bus_message_read(message, "t", &v); \
487 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
488 "Value specified in %s is out of range", name); \
490 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
492 unit_invalidate_cgroup(u, (mask)); \
494 if (v == CGROUP_LIMIT_MAX) \
495 unit_write_settingf(u, flags, name, \
496 "%s=infinity", name); \
498 unit_write_settingf(u, flags, name, \
499 "%s=%" PRIu64, name, v); \
504 static int bus_cgroup_set_##function##_scale( \
508 sd_bus_message *message, \
509 UnitWriteFlags flags, \
510 sd_bus_error *error) { \
518 r = sd_bus_message_read(message, "u", &raw); \
522 v = scale(raw, UINT32_MAX); \
523 if (v < minimum || v >= UINT64_MAX) \
524 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
525 "Value specified in %s is out of range", name); \
527 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
531 unit_invalidate_cgroup(u, (mask)); \
533 /* Chop off suffix */ \
534 assert_se(e = endswith(name, "Scale")); \
535 name = strndupa(name, e - name); \
537 unit_write_settingf(u, flags, name, "%s=%" PRIu32 "%%", name, \
538 (uint32_t) (DIV_ROUND_UP((uint64_t) raw * 100U, (uint64_t) UINT32_MAX))); \
544 #pragma GCC diagnostic push
545 #pragma GCC diagnostic ignored "-Wtype-limits"
546 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_weight
, CGROUP_MASK_CPU
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
547 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares
, CGROUP_MASK_CPU
, CGROUP_CPU_SHARES_IS_OK
, CGROUP_CPU_SHARES_INVALID
);
548 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight
, CGROUP_MASK_IO
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
549 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight
, CGROUP_MASK_BLKIO
, CGROUP_BLKIO_WEIGHT_IS_OK
, CGROUP_BLKIO_WEIGHT_INVALID
);
550 BUS_DEFINE_SET_CGROUP_LIMIT(memory
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 1);
551 BUS_DEFINE_SET_CGROUP_LIMIT(swap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
552 BUS_DEFINE_SET_CGROUP_LIMIT(tasks_max
, CGROUP_MASK_PIDS
, system_tasks_max_scale
, 1);
553 #pragma GCC diagnostic pop
555 int bus_cgroup_set_property(
559 sd_bus_message
*message
,
560 UnitWriteFlags flags
,
561 sd_bus_error
*error
) {
563 CGroupIOLimitType iol_type
;
571 flags
|= UNIT_PRIVATE
;
573 if (streq(name
, "CPUAccounting"))
574 return bus_cgroup_set_boolean(u
, name
, &c
->cpu_accounting
, CGROUP_MASK_CPUACCT
|CGROUP_MASK_CPU
, message
, flags
, error
);
576 if (streq(name
, "CPUWeight"))
577 return bus_cgroup_set_cpu_weight(u
, name
, &c
->cpu_weight
, message
, flags
, error
);
579 if (streq(name
, "StartupCPUWeight"))
580 return bus_cgroup_set_cpu_weight(u
, name
, &c
->startup_cpu_weight
, message
, flags
, error
);
582 if (streq(name
, "CPUShares"))
583 return bus_cgroup_set_cpu_shares(u
, name
, &c
->cpu_shares
, message
, flags
, error
);
585 if (streq(name
, "StartupCPUShares"))
586 return bus_cgroup_set_cpu_shares(u
, name
, &c
->startup_cpu_shares
, message
, flags
, error
);
588 if (streq(name
, "IOAccounting"))
589 return bus_cgroup_set_boolean(u
, name
, &c
->io_accounting
, CGROUP_MASK_IO
, message
, flags
, error
);
591 if (streq(name
, "IOWeight"))
592 return bus_cgroup_set_io_weight(u
, name
, &c
->io_weight
, message
, flags
, error
);
594 if (streq(name
, "StartupIOWeight"))
595 return bus_cgroup_set_io_weight(u
, name
, &c
->startup_io_weight
, message
, flags
, error
);
597 if (streq(name
, "BlockIOAccounting"))
598 return bus_cgroup_set_boolean(u
, name
, &c
->blockio_accounting
, CGROUP_MASK_BLKIO
, message
, flags
, error
);
600 if (streq(name
, "BlockIOWeight"))
601 return bus_cgroup_set_blockio_weight(u
, name
, &c
->blockio_weight
, message
, flags
, error
);
603 if (streq(name
, "StartupBlockIOWeight"))
604 return bus_cgroup_set_blockio_weight(u
, name
, &c
->startup_blockio_weight
, message
, flags
, error
);
606 if (streq(name
, "MemoryAccounting"))
607 return bus_cgroup_set_boolean(u
, name
, &c
->memory_accounting
, CGROUP_MASK_MEMORY
, message
, flags
, error
);
609 if (streq(name
, "MemoryLow"))
610 return bus_cgroup_set_memory(u
, name
, &c
->memory_low
, message
, flags
, error
);
612 if (streq(name
, "MemoryHigh"))
613 return bus_cgroup_set_memory(u
, name
, &c
->memory_high
, message
, flags
, error
);
615 if (streq(name
, "MemorySwapMax"))
616 return bus_cgroup_set_swap(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
618 if (streq(name
, "MemoryMax"))
619 return bus_cgroup_set_memory(u
, name
, &c
->memory_max
, message
, flags
, error
);
621 if (streq(name
, "MemoryLimit"))
622 return bus_cgroup_set_memory(u
, name
, &c
->memory_limit
, message
, flags
, error
);
624 if (streq(name
, "MemoryLowScale"))
625 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_low
, message
, flags
, error
);
627 if (streq(name
, "MemoryHighScale"))
628 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_high
, message
, flags
, error
);
630 if (streq(name
, "MemorySwapMaxScale"))
631 return bus_cgroup_set_swap_scale(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
633 if (streq(name
, "MemoryMaxScale"))
634 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_max
, message
, flags
, error
);
636 if (streq(name
, "MemoryLimitScale"))
637 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_limit
, message
, flags
, error
);
639 if (streq(name
, "TasksAccounting"))
640 return bus_cgroup_set_boolean(u
, name
, &c
->tasks_accounting
, CGROUP_MASK_PIDS
, message
, flags
, error
);
642 if (streq(name
, "TasksMax"))
643 return bus_cgroup_set_tasks_max(u
, name
, &c
->tasks_max
, message
, flags
, error
);
645 if (streq(name
, "TasksMaxScale"))
646 return bus_cgroup_set_tasks_max_scale(u
, name
, &c
->tasks_max
, message
, flags
, error
);
648 if (streq(name
, "CPUQuotaPerSecUSec")) {
651 r
= sd_bus_message_read(message
, "t", &u64
);
656 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "CPUQuotaPerSecUSec= value out of range");
658 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
659 c
->cpu_quota_per_sec_usec
= u64
;
660 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
662 if (c
->cpu_quota_per_sec_usec
== USEC_INFINITY
)
663 unit_write_setting(u
, flags
, "CPUQuota", "CPUQuota=");
665 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
667 unit_write_settingf(u
, flags
, "CPUQuota",
669 (double) (c
->cpu_quota_per_sec_usec
/ 10000));
674 } else if ((iol_type
= cgroup_io_limit_type_from_string(name
)) >= 0) {
679 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
683 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
685 if (!path_is_normalized(path
))
686 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
688 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
689 CGroupIODeviceLimit
*a
= NULL
, *b
;
691 LIST_FOREACH(device_limits
, b
, c
->io_device_limits
) {
692 if (path_equal(path
, b
->path
)) {
699 CGroupIOLimitType type
;
701 a
= new0(CGroupIODeviceLimit
, 1);
705 a
->path
= strdup(path
);
711 for (type
= 0; type
< _CGROUP_IO_LIMIT_TYPE_MAX
; type
++)
712 a
->limits
[type
] = cgroup_io_limit_defaults
[type
];
714 LIST_PREPEND(device_limits
, c
->io_device_limits
, a
);
717 a
->limits
[iol_type
] = u64
;
725 r
= sd_bus_message_exit_container(message
);
729 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
730 CGroupIODeviceLimit
*a
;
731 _cleanup_free_
char *buf
= NULL
;
732 _cleanup_fclose_
FILE *f
= NULL
;
736 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
737 a
->limits
[iol_type
] = cgroup_io_limit_defaults
[iol_type
];
740 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
742 f
= open_memstream(&buf
, &size
);
746 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
748 fprintf(f
, "%s=\n", name
);
749 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
750 if (a
->limits
[iol_type
] != cgroup_io_limit_defaults
[iol_type
])
751 fprintf(f
, "%s=%s %" PRIu64
"\n", name
, a
->path
, a
->limits
[iol_type
]);
753 r
= fflush_and_check(f
);
756 unit_write_setting(u
, flags
, name
, buf
);
761 } else if (streq(name
, "IODeviceWeight")) {
766 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
770 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
772 if (!path_is_normalized(path
))
773 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
775 if (!CGROUP_WEIGHT_IS_OK(weight
) || weight
== CGROUP_WEIGHT_INVALID
)
776 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IODeviceWeight= value out of range");
778 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
779 CGroupIODeviceWeight
*a
= NULL
, *b
;
781 LIST_FOREACH(device_weights
, b
, c
->io_device_weights
) {
782 if (path_equal(b
->path
, path
)) {
789 a
= new0(CGroupIODeviceWeight
, 1);
793 a
->path
= strdup(path
);
798 LIST_PREPEND(device_weights
, c
->io_device_weights
, a
);
807 r
= sd_bus_message_exit_container(message
);
811 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
812 _cleanup_free_
char *buf
= NULL
;
813 _cleanup_fclose_
FILE *f
= NULL
;
814 CGroupIODeviceWeight
*a
;
818 while (c
->io_device_weights
)
819 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
822 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
824 f
= open_memstream(&buf
, &size
);
828 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
830 fputs("IODeviceWeight=\n", f
);
831 LIST_FOREACH(device_weights
, a
, c
->io_device_weights
)
832 fprintf(f
, "IODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
834 r
= fflush_and_check(f
);
837 unit_write_setting(u
, flags
, name
, buf
);
842 } else if (STR_IN_SET(name
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
848 if (streq(name
, "BlockIOWriteBandwidth"))
851 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
855 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
857 if (!path_is_normalized(path
))
858 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
860 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
861 CGroupBlockIODeviceBandwidth
*a
= NULL
, *b
;
863 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
864 if (path_equal(path
, b
->path
)) {
871 a
= new0(CGroupBlockIODeviceBandwidth
, 1);
875 a
->rbps
= CGROUP_LIMIT_MAX
;
876 a
->wbps
= CGROUP_LIMIT_MAX
;
877 a
->path
= strdup(path
);
883 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, a
);
897 r
= sd_bus_message_exit_container(message
);
901 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
902 CGroupBlockIODeviceBandwidth
*a
;
903 _cleanup_free_
char *buf
= NULL
;
904 _cleanup_fclose_
FILE *f
= NULL
;
908 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
) {
910 a
->rbps
= CGROUP_LIMIT_MAX
;
912 a
->wbps
= CGROUP_LIMIT_MAX
;
916 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
918 f
= open_memstream(&buf
, &size
);
922 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
925 fputs("BlockIOReadBandwidth=\n", f
);
926 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
927 if (a
->rbps
!= CGROUP_LIMIT_MAX
)
928 fprintf(f
, "BlockIOReadBandwidth=%s %" PRIu64
"\n", a
->path
, a
->rbps
);
930 fputs("BlockIOWriteBandwidth=\n", f
);
931 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
932 if (a
->wbps
!= CGROUP_LIMIT_MAX
)
933 fprintf(f
, "BlockIOWriteBandwidth=%s %" PRIu64
"\n", a
->path
, a
->wbps
);
936 r
= fflush_and_check(f
);
940 unit_write_setting(u
, flags
, name
, buf
);
945 } else if (streq(name
, "BlockIODeviceWeight")) {
950 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
954 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
956 if (!path_is_normalized(path
))
957 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
959 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight
) || weight
== CGROUP_BLKIO_WEIGHT_INVALID
)
960 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "BlockIODeviceWeight= out of range");
962 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
963 CGroupBlockIODeviceWeight
*a
= NULL
, *b
;
965 LIST_FOREACH(device_weights
, b
, c
->blockio_device_weights
) {
966 if (path_equal(b
->path
, path
)) {
973 a
= new0(CGroupBlockIODeviceWeight
, 1);
977 a
->path
= strdup(path
);
982 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, a
);
991 r
= sd_bus_message_exit_container(message
);
995 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
996 _cleanup_free_
char *buf
= NULL
;
997 _cleanup_fclose_
FILE *f
= NULL
;
998 CGroupBlockIODeviceWeight
*a
;
1002 while (c
->blockio_device_weights
)
1003 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
1006 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1008 f
= open_memstream(&buf
, &size
);
1012 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1014 fputs("BlockIODeviceWeight=\n", f
);
1015 LIST_FOREACH(device_weights
, a
, c
->blockio_device_weights
)
1016 fprintf(f
, "BlockIODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1018 r
= fflush_and_check(f
);
1022 unit_write_setting(u
, flags
, name
, buf
);
1027 } else if (streq(name
, "DevicePolicy")) {
1029 CGroupDevicePolicy p
;
1031 r
= sd_bus_message_read(message
, "s", &policy
);
1035 p
= cgroup_device_policy_from_string(policy
);
1039 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1040 c
->device_policy
= p
;
1041 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1042 unit_write_settingf(u
, flags
, name
, "DevicePolicy=%s", policy
);
1047 } else if (streq(name
, "DeviceAllow")) {
1048 const char *path
, *rwm
;
1051 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
1055 while ((r
= sd_bus_message_read(message
, "(ss)", &path
, &rwm
)) > 0) {
1057 if (!valid_device_allow_pattern(path
) || strpbrk(path
, WHITESPACE
))
1058 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires device node or pattern");
1062 else if (!in_charset(rwm
, "rwm"))
1063 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires combination of rwm flags");
1065 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1066 CGroupDeviceAllow
*a
= NULL
, *b
;
1068 LIST_FOREACH(device_allow
, b
, c
->device_allow
) {
1069 if (path_equal(b
->path
, path
)) {
1076 a
= new0(CGroupDeviceAllow
, 1);
1080 a
->path
= strdup(path
);
1086 LIST_PREPEND(device_allow
, c
->device_allow
, a
);
1089 a
->r
= !!strchr(rwm
, 'r');
1090 a
->w
= !!strchr(rwm
, 'w');
1091 a
->m
= !!strchr(rwm
, 'm');
1099 r
= sd_bus_message_exit_container(message
);
1103 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1104 _cleanup_free_
char *buf
= NULL
;
1105 _cleanup_fclose_
FILE *f
= NULL
;
1106 CGroupDeviceAllow
*a
;
1110 while (c
->device_allow
)
1111 cgroup_context_free_device_allow(c
, c
->device_allow
);
1114 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1116 f
= open_memstream(&buf
, &size
);
1120 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1122 fputs("DeviceAllow=\n", f
);
1123 LIST_FOREACH(device_allow
, a
, c
->device_allow
)
1124 fprintf(f
, "DeviceAllow=%s %s%s%s\n", a
->path
, a
->r
? "r" : "", a
->w
? "w" : "", a
->m
? "m" : "");
1126 r
= fflush_and_check(f
);
1129 unit_write_setting(u
, flags
, name
, buf
);
1134 } else if (streq(name
, "IPAccounting")) {
1137 r
= sd_bus_message_read(message
, "b", &b
);
1141 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1142 c
->ip_accounting
= b
;
1144 unit_invalidate_cgroup_bpf(u
);
1145 unit_write_settingf(u
, flags
, name
, "IPAccounting=%s", yes_no(b
));
1150 } else if (STR_IN_SET(name
, "IPAddressAllow", "IPAddressDeny")) {
1151 IPAddressAccessItem
**list
;
1154 list
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow
: &c
->ip_address_deny
;
1156 r
= sd_bus_message_enter_container(message
, 'a', "(iayu)");
1166 r
= sd_bus_message_enter_container(message
, 'r', "iayu");
1172 r
= sd_bus_message_read(message
, "i", &family
);
1176 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1177 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects IPv4 or IPv6 addresses only.", name
);
1179 r
= sd_bus_message_read_array(message
, 'y', &ap
, &an
);
1183 if (an
!= FAMILY_ADDRESS_SIZE(family
))
1184 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1185 af_to_name(family
), FAMILY_ADDRESS_SIZE(family
), an
);
1187 r
= sd_bus_message_read(message
, "u", &prefixlen
);
1191 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
)*8)
1192 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
));
1194 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1195 IPAddressAccessItem
*item
;
1197 item
= new0(IPAddressAccessItem
, 1);
1201 item
->family
= family
;
1202 item
->prefixlen
= prefixlen
;
1203 memcpy(&item
->address
, ap
, an
);
1205 LIST_PREPEND(items
, *list
, item
);
1208 r
= sd_bus_message_exit_container(message
);
1215 r
= sd_bus_message_exit_container(message
);
1219 *list
= ip_address_access_reduce(*list
);
1221 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1222 _cleanup_free_
char *buf
= NULL
;
1223 _cleanup_fclose_
FILE *f
= NULL
;
1224 IPAddressAccessItem
*item
;
1228 *list
= ip_address_access_free_all(*list
);
1230 unit_invalidate_cgroup_bpf(u
);
1231 f
= open_memstream(&buf
, &size
);
1235 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1240 LIST_FOREACH(items
, item
, *list
) {
1241 char buffer
[CONST_MAX(INET_ADDRSTRLEN
, INET6_ADDRSTRLEN
)];
1244 if (!inet_ntop(item
->family
, &item
->address
, buffer
, sizeof(buffer
)))
1245 return errno
> 0 ? -errno
: -EINVAL
;
1247 fprintf(f
, "%s=%s/%u\n", name
, buffer
, item
->prefixlen
);
1250 r
= fflush_and_check(f
);
1254 unit_write_setting(u
, flags
, name
, buf
);
1257 r
= bpf_firewall_supported();
1260 if (r
== BPF_FIREWALL_UNSUPPORTED
) {
1261 static bool warned
= false;
1263 log_full(warned
? LOG_DEBUG
: LOG_WARNING
,
1264 "Transient unit %s configures an IP firewall, but the local system does not support BPF/cgroup firewalling.\n"
1265 "Proceeding WITHOUT firewalling in effect! (This warning is only shown for the first started transient unit using IP firewalling.)", u
->id
);
1275 if (u
->transient
&& u
->load_state
== UNIT_STUB
)
1276 return bus_cgroup_set_transient_property(u
, c
, name
, message
, flags
, error
);