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_cgroup_mask(
23 const char *interface
,
25 sd_bus_message
*reply
,
27 sd_bus_error
*error
) {
29 CGroupMask
*mask
= userdata
;
30 CGroupController ctrl
;
36 r
= sd_bus_message_open_container(reply
, 'a', "s");
40 for (ctrl
= 0; ctrl
< _CGROUP_CONTROLLER_MAX
; ctrl
++) {
41 if ((*mask
& CGROUP_CONTROLLER_TO_MASK(ctrl
)) == 0)
44 r
= sd_bus_message_append(reply
, "s", cgroup_controller_to_string(ctrl
));
49 return sd_bus_message_close_container(reply
);
52 static int property_get_delegate_controllers(
55 const char *interface
,
57 sd_bus_message
*reply
,
59 sd_bus_error
*error
) {
61 CGroupContext
*c
= userdata
;
68 return sd_bus_message_append(reply
, "as", 0);
70 return property_get_cgroup_mask(bus
, path
, interface
, property
, reply
, &c
->delegate_controllers
, error
);
73 static int property_get_io_device_weight(
76 const char *interface
,
78 sd_bus_message
*reply
,
80 sd_bus_error
*error
) {
82 CGroupContext
*c
= userdata
;
83 CGroupIODeviceWeight
*w
;
90 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
94 LIST_FOREACH(device_weights
, w
, c
->io_device_weights
) {
95 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
100 return sd_bus_message_close_container(reply
);
103 static int property_get_io_device_limits(
106 const char *interface
,
107 const char *property
,
108 sd_bus_message
*reply
,
110 sd_bus_error
*error
) {
112 CGroupContext
*c
= userdata
;
113 CGroupIODeviceLimit
*l
;
120 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
124 LIST_FOREACH(device_limits
, l
, c
->io_device_limits
) {
125 CGroupIOLimitType type
;
127 type
= cgroup_io_limit_type_from_string(property
);
128 if (type
< 0 || l
->limits
[type
] == cgroup_io_limit_defaults
[type
])
131 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->limits
[type
]);
136 return sd_bus_message_close_container(reply
);
139 static int property_get_io_device_latency(
142 const char *interface
,
143 const char *property
,
144 sd_bus_message
*reply
,
146 sd_bus_error
*error
) {
148 CGroupContext
*c
= userdata
;
149 CGroupIODeviceLatency
*l
;
156 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
160 LIST_FOREACH(device_latencies
, l
, c
->io_device_latencies
) {
161 r
= sd_bus_message_append(reply
, "(st)", l
->path
, l
->target_usec
);
166 return sd_bus_message_close_container(reply
);
169 static int property_get_blockio_device_weight(
172 const char *interface
,
173 const char *property
,
174 sd_bus_message
*reply
,
176 sd_bus_error
*error
) {
178 CGroupContext
*c
= userdata
;
179 CGroupBlockIODeviceWeight
*w
;
186 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
190 LIST_FOREACH(device_weights
, w
, c
->blockio_device_weights
) {
191 r
= sd_bus_message_append(reply
, "(st)", w
->path
, w
->weight
);
196 return sd_bus_message_close_container(reply
);
199 static int property_get_blockio_device_bandwidths(
202 const char *interface
,
203 const char *property
,
204 sd_bus_message
*reply
,
206 sd_bus_error
*error
) {
208 CGroupContext
*c
= userdata
;
209 CGroupBlockIODeviceBandwidth
*b
;
216 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
220 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
223 if (streq(property
, "BlockIOReadBandwidth"))
228 if (v
== CGROUP_LIMIT_MAX
)
231 r
= sd_bus_message_append(reply
, "(st)", b
->path
, v
);
236 return sd_bus_message_close_container(reply
);
239 static int property_get_device_allow(
242 const char *interface
,
243 const char *property
,
244 sd_bus_message
*reply
,
246 sd_bus_error
*error
) {
248 CGroupContext
*c
= userdata
;
249 CGroupDeviceAllow
*a
;
256 r
= sd_bus_message_open_container(reply
, 'a', "(ss)");
260 LIST_FOREACH(device_allow
, a
, c
->device_allow
) {
273 r
= sd_bus_message_append(reply
, "(ss)", a
->path
, rwm
);
278 return sd_bus_message_close_container(reply
);
281 static int property_get_ip_address_access(
284 const char *interface
,
285 const char *property
,
286 sd_bus_message
*reply
,
288 sd_bus_error
*error
) {
290 IPAddressAccessItem
** items
= userdata
, *i
;
293 r
= sd_bus_message_open_container(reply
, 'a', "(iayu)");
297 LIST_FOREACH(items
, i
, *items
) {
299 r
= sd_bus_message_open_container(reply
, 'r', "iayu");
303 r
= sd_bus_message_append(reply
, "i", i
->family
);
307 r
= sd_bus_message_append_array(reply
, 'y', &i
->address
, FAMILY_ADDRESS_SIZE(i
->family
));
311 r
= sd_bus_message_append(reply
, "u", (uint32_t) i
->prefixlen
);
315 r
= sd_bus_message_close_container(reply
);
320 return sd_bus_message_close_container(reply
);
323 const sd_bus_vtable bus_cgroup_vtable
[] = {
324 SD_BUS_VTABLE_START(0),
325 SD_BUS_PROPERTY("Delegate", "b", bus_property_get_bool
, offsetof(CGroupContext
, delegate
), 0),
326 SD_BUS_PROPERTY("DelegateControllers", "as", property_get_delegate_controllers
, 0, 0),
327 SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, cpu_accounting
), 0),
328 SD_BUS_PROPERTY("CPUWeight", "t", NULL
, offsetof(CGroupContext
, cpu_weight
), 0),
329 SD_BUS_PROPERTY("StartupCPUWeight", "t", NULL
, offsetof(CGroupContext
, startup_cpu_weight
), 0),
330 SD_BUS_PROPERTY("CPUShares", "t", NULL
, offsetof(CGroupContext
, cpu_shares
), 0),
331 SD_BUS_PROPERTY("StartupCPUShares", "t", NULL
, offsetof(CGroupContext
, startup_cpu_shares
), 0),
332 SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec
, offsetof(CGroupContext
, cpu_quota_per_sec_usec
), 0),
333 SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, io_accounting
), 0),
334 SD_BUS_PROPERTY("IOWeight", "t", NULL
, offsetof(CGroupContext
, io_weight
), 0),
335 SD_BUS_PROPERTY("StartupIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_io_weight
), 0),
336 SD_BUS_PROPERTY("IODeviceWeight", "a(st)", property_get_io_device_weight
, 0, 0),
337 SD_BUS_PROPERTY("IOReadBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
338 SD_BUS_PROPERTY("IOWriteBandwidthMax", "a(st)", property_get_io_device_limits
, 0, 0),
339 SD_BUS_PROPERTY("IOReadIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
340 SD_BUS_PROPERTY("IOWriteIOPSMax", "a(st)", property_get_io_device_limits
, 0, 0),
341 SD_BUS_PROPERTY("IODeviceLatencyTargetUSec", "a(st)", property_get_io_device_latency
, 0, 0),
342 SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, blockio_accounting
), 0),
343 SD_BUS_PROPERTY("BlockIOWeight", "t", NULL
, offsetof(CGroupContext
, blockio_weight
), 0),
344 SD_BUS_PROPERTY("StartupBlockIOWeight", "t", NULL
, offsetof(CGroupContext
, startup_blockio_weight
), 0),
345 SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight
, 0, 0),
346 SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
347 SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths
, 0, 0),
348 SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, memory_accounting
), 0),
349 SD_BUS_PROPERTY("MemoryMin", "t", NULL
, offsetof(CGroupContext
, memory_min
), 0),
350 SD_BUS_PROPERTY("MemoryLow", "t", NULL
, offsetof(CGroupContext
, memory_low
), 0),
351 SD_BUS_PROPERTY("MemoryHigh", "t", NULL
, offsetof(CGroupContext
, memory_high
), 0),
352 SD_BUS_PROPERTY("MemoryMax", "t", NULL
, offsetof(CGroupContext
, memory_max
), 0),
353 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL
, offsetof(CGroupContext
, memory_swap_max
), 0),
354 SD_BUS_PROPERTY("MemoryLimit", "t", NULL
, offsetof(CGroupContext
, memory_limit
), 0),
355 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy
, offsetof(CGroupContext
, device_policy
), 0),
356 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow
, 0, 0),
357 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, tasks_accounting
), 0),
358 SD_BUS_PROPERTY("TasksMax", "t", NULL
, offsetof(CGroupContext
, tasks_max
), 0),
359 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool
, offsetof(CGroupContext
, ip_accounting
), 0),
360 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_allow
), 0),
361 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access
, offsetof(CGroupContext
, ip_address_deny
), 0),
362 SD_BUS_PROPERTY("DisableControllers", "as", property_get_cgroup_mask
, offsetof(CGroupContext
, disable_controllers
), 0),
366 static int bus_cgroup_set_transient_property(
370 sd_bus_message
*message
,
371 UnitWriteFlags flags
,
372 sd_bus_error
*error
) {
381 flags
|= UNIT_PRIVATE
;
383 if (streq(name
, "Delegate")) {
386 if (!UNIT_VTABLE(u
)->can_delegate
)
387 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
389 r
= sd_bus_message_read(message
, "b", &b
);
393 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
395 c
->delegate_controllers
= b
? _CGROUP_MASK_ALL
: 0;
397 unit_write_settingf(u
, flags
, name
, "Delegate=%s", yes_no(b
));
402 } else if (streq(name
, "DelegateControllers")) {
405 if (!UNIT_VTABLE(u
)->can_delegate
)
406 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delegation not available for unit type");
408 r
= sd_bus_message_enter_container(message
, 'a', "s");
416 r
= sd_bus_message_read(message
, "s", &t
);
422 cc
= cgroup_controller_from_string(t
);
424 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown cgroup controller '%s'", t
);
426 mask
|= CGROUP_CONTROLLER_TO_MASK(cc
);
429 r
= sd_bus_message_exit_container(message
);
433 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
434 _cleanup_free_
char *t
= NULL
;
436 r
= cg_mask_to_string(mask
, &t
);
442 c
->delegate_controllers
= 0;
444 c
->delegate_controllers
|= mask
;
446 unit_write_settingf(u
, flags
, name
, "Delegate=%s", strempty(t
));
455 static int bus_cgroup_set_boolean(
460 sd_bus_message
*message
,
461 UnitWriteFlags flags
,
462 sd_bus_error
*error
) {
468 r
= sd_bus_message_read(message
, "b", &b
);
472 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
474 unit_invalidate_cgroup(u
, mask
);
475 unit_write_settingf(u
, flags
, name
, "%s=%s", name
, yes_no(b
));
481 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
482 static int bus_cgroup_set_##function( \
486 sd_bus_message *message, \
487 UnitWriteFlags flags, \
488 sd_bus_error *error) { \
495 r = sd_bus_message_read(message, "t", &v); \
500 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
501 "Value specified in %s is out of range", name); \
503 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
505 unit_invalidate_cgroup(u, (mask)); \
508 unit_write_settingf(u, flags, name, \
511 unit_write_settingf(u, flags, name, \
512 "%s=%" PRIu64, name, v); \
518 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
519 static int bus_cgroup_set_##function( \
523 sd_bus_message *message, \
524 UnitWriteFlags flags, \
525 sd_bus_error *error) { \
532 r = sd_bus_message_read(message, "t", &v); \
537 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
538 "Value specified in %s is out of range", name); \
540 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
542 unit_invalidate_cgroup(u, (mask)); \
544 if (v == CGROUP_LIMIT_MAX) \
545 unit_write_settingf(u, flags, name, \
546 "%s=infinity", name); \
548 unit_write_settingf(u, flags, name, \
549 "%s=%" PRIu64, name, v); \
554 static int bus_cgroup_set_##function##_scale( \
558 sd_bus_message *message, \
559 UnitWriteFlags flags, \
560 sd_bus_error *error) { \
568 r = sd_bus_message_read(message, "u", &raw); \
572 v = scale(raw, UINT32_MAX); \
573 if (v < minimum || v >= UINT64_MAX) \
574 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
575 "Value specified in %s is out of range", name); \
577 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
581 unit_invalidate_cgroup(u, (mask)); \
583 /* Chop off suffix */ \
584 assert_se(e = endswith(name, "Scale")); \
585 name = strndupa(name, e - name); \
587 unit_write_settingf(u, flags, name, "%s=%" PRIu32 "%%", name, \
588 (uint32_t) (DIV_ROUND_UP((uint64_t) raw * 100U, (uint64_t) UINT32_MAX))); \
594 #pragma GCC diagnostic push
595 #pragma GCC diagnostic ignored "-Wtype-limits"
596 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_weight
, CGROUP_MASK_CPU
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
597 BUS_DEFINE_SET_CGROUP_WEIGHT(cpu_shares
, CGROUP_MASK_CPU
, CGROUP_CPU_SHARES_IS_OK
, CGROUP_CPU_SHARES_INVALID
);
598 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight
, CGROUP_MASK_IO
, CGROUP_WEIGHT_IS_OK
, CGROUP_WEIGHT_INVALID
);
599 BUS_DEFINE_SET_CGROUP_WEIGHT(blockio_weight
, CGROUP_MASK_BLKIO
, CGROUP_BLKIO_WEIGHT_IS_OK
, CGROUP_BLKIO_WEIGHT_INVALID
);
600 BUS_DEFINE_SET_CGROUP_LIMIT(memory
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 1);
601 BUS_DEFINE_SET_CGROUP_LIMIT(swap
, CGROUP_MASK_MEMORY
, physical_memory_scale
, 0);
602 BUS_DEFINE_SET_CGROUP_LIMIT(tasks_max
, CGROUP_MASK_PIDS
, system_tasks_max_scale
, 1);
603 #pragma GCC diagnostic pop
605 int bus_cgroup_set_property(
609 sd_bus_message
*message
,
610 UnitWriteFlags flags
,
611 sd_bus_error
*error
) {
613 CGroupIOLimitType iol_type
;
621 flags
|= UNIT_PRIVATE
;
623 if (streq(name
, "CPUAccounting"))
624 return bus_cgroup_set_boolean(u
, name
, &c
->cpu_accounting
, get_cpu_accounting_mask(), message
, flags
, error
);
626 if (streq(name
, "CPUWeight"))
627 return bus_cgroup_set_cpu_weight(u
, name
, &c
->cpu_weight
, message
, flags
, error
);
629 if (streq(name
, "StartupCPUWeight"))
630 return bus_cgroup_set_cpu_weight(u
, name
, &c
->startup_cpu_weight
, message
, flags
, error
);
632 if (streq(name
, "CPUShares"))
633 return bus_cgroup_set_cpu_shares(u
, name
, &c
->cpu_shares
, message
, flags
, error
);
635 if (streq(name
, "StartupCPUShares"))
636 return bus_cgroup_set_cpu_shares(u
, name
, &c
->startup_cpu_shares
, message
, flags
, error
);
638 if (streq(name
, "IOAccounting"))
639 return bus_cgroup_set_boolean(u
, name
, &c
->io_accounting
, CGROUP_MASK_IO
, message
, flags
, error
);
641 if (streq(name
, "IOWeight"))
642 return bus_cgroup_set_io_weight(u
, name
, &c
->io_weight
, message
, flags
, error
);
644 if (streq(name
, "StartupIOWeight"))
645 return bus_cgroup_set_io_weight(u
, name
, &c
->startup_io_weight
, message
, flags
, error
);
647 if (streq(name
, "BlockIOAccounting"))
648 return bus_cgroup_set_boolean(u
, name
, &c
->blockio_accounting
, CGROUP_MASK_BLKIO
, message
, flags
, error
);
650 if (streq(name
, "BlockIOWeight"))
651 return bus_cgroup_set_blockio_weight(u
, name
, &c
->blockio_weight
, message
, flags
, error
);
653 if (streq(name
, "StartupBlockIOWeight"))
654 return bus_cgroup_set_blockio_weight(u
, name
, &c
->startup_blockio_weight
, message
, flags
, error
);
656 if (streq(name
, "MemoryAccounting"))
657 return bus_cgroup_set_boolean(u
, name
, &c
->memory_accounting
, CGROUP_MASK_MEMORY
, message
, flags
, error
);
659 if (streq(name
, "MemoryMin"))
660 return bus_cgroup_set_memory(u
, name
, &c
->memory_min
, message
, flags
, error
);
662 if (streq(name
, "MemoryLow"))
663 return bus_cgroup_set_memory(u
, name
, &c
->memory_low
, message
, flags
, error
);
665 if (streq(name
, "MemoryHigh"))
666 return bus_cgroup_set_memory(u
, name
, &c
->memory_high
, message
, flags
, error
);
668 if (streq(name
, "MemorySwapMax"))
669 return bus_cgroup_set_swap(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
671 if (streq(name
, "MemoryMax"))
672 return bus_cgroup_set_memory(u
, name
, &c
->memory_max
, message
, flags
, error
);
674 if (streq(name
, "MemoryLimit"))
675 return bus_cgroup_set_memory(u
, name
, &c
->memory_limit
, message
, flags
, error
);
677 if (streq(name
, "MemoryMinScale"))
678 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_min
, message
, flags
, error
);
680 if (streq(name
, "MemoryLowScale"))
681 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_low
, message
, flags
, error
);
683 if (streq(name
, "MemoryHighScale"))
684 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_high
, message
, flags
, error
);
686 if (streq(name
, "MemorySwapMaxScale"))
687 return bus_cgroup_set_swap_scale(u
, name
, &c
->memory_swap_max
, message
, flags
, error
);
689 if (streq(name
, "MemoryMaxScale"))
690 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_max
, message
, flags
, error
);
692 if (streq(name
, "MemoryLimitScale"))
693 return bus_cgroup_set_memory_scale(u
, name
, &c
->memory_limit
, message
, flags
, error
);
695 if (streq(name
, "TasksAccounting"))
696 return bus_cgroup_set_boolean(u
, name
, &c
->tasks_accounting
, CGROUP_MASK_PIDS
, message
, flags
, error
);
698 if (streq(name
, "TasksMax"))
699 return bus_cgroup_set_tasks_max(u
, name
, &c
->tasks_max
, message
, flags
, error
);
701 if (streq(name
, "TasksMaxScale"))
702 return bus_cgroup_set_tasks_max_scale(u
, name
, &c
->tasks_max
, message
, flags
, error
);
704 if (streq(name
, "CPUQuotaPerSecUSec")) {
707 r
= sd_bus_message_read(message
, "t", &u64
);
712 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "CPUQuotaPerSecUSec= value out of range");
714 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
715 c
->cpu_quota_per_sec_usec
= u64
;
716 unit_invalidate_cgroup(u
, CGROUP_MASK_CPU
);
718 if (c
->cpu_quota_per_sec_usec
== USEC_INFINITY
)
719 unit_write_setting(u
, flags
, "CPUQuota", "CPUQuota=");
721 /* config_parse_cpu_quota() requires an integer, so truncating division is used on
723 unit_write_settingf(u
, flags
, "CPUQuota",
725 (double) (c
->cpu_quota_per_sec_usec
/ 10000));
730 } else if ((iol_type
= cgroup_io_limit_type_from_string(name
)) >= 0) {
735 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
739 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
741 if (!path_is_normalized(path
))
742 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
744 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
745 CGroupIODeviceLimit
*a
= NULL
, *b
;
747 LIST_FOREACH(device_limits
, b
, c
->io_device_limits
) {
748 if (path_equal(path
, b
->path
)) {
755 CGroupIOLimitType type
;
757 a
= new0(CGroupIODeviceLimit
, 1);
761 a
->path
= strdup(path
);
767 for (type
= 0; type
< _CGROUP_IO_LIMIT_TYPE_MAX
; type
++)
768 a
->limits
[type
] = cgroup_io_limit_defaults
[type
];
770 LIST_PREPEND(device_limits
, c
->io_device_limits
, a
);
773 a
->limits
[iol_type
] = u64
;
781 r
= sd_bus_message_exit_container(message
);
785 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
786 CGroupIODeviceLimit
*a
;
787 _cleanup_free_
char *buf
= NULL
;
788 _cleanup_fclose_
FILE *f
= NULL
;
792 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
793 a
->limits
[iol_type
] = cgroup_io_limit_defaults
[iol_type
];
796 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
798 f
= open_memstream(&buf
, &size
);
802 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
804 fprintf(f
, "%s=\n", name
);
805 LIST_FOREACH(device_limits
, a
, c
->io_device_limits
)
806 if (a
->limits
[iol_type
] != cgroup_io_limit_defaults
[iol_type
])
807 fprintf(f
, "%s=%s %" PRIu64
"\n", name
, a
->path
, a
->limits
[iol_type
]);
809 r
= fflush_and_check(f
);
812 unit_write_setting(u
, flags
, name
, buf
);
817 } else if (streq(name
, "IODeviceWeight")) {
822 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
826 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
828 if (!path_is_normalized(path
))
829 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
831 if (!CGROUP_WEIGHT_IS_OK(weight
) || weight
== CGROUP_WEIGHT_INVALID
)
832 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IODeviceWeight= value out of range");
834 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
835 CGroupIODeviceWeight
*a
= NULL
, *b
;
837 LIST_FOREACH(device_weights
, b
, c
->io_device_weights
) {
838 if (path_equal(b
->path
, path
)) {
845 a
= new0(CGroupIODeviceWeight
, 1);
849 a
->path
= strdup(path
);
854 LIST_PREPEND(device_weights
, c
->io_device_weights
, a
);
863 r
= sd_bus_message_exit_container(message
);
867 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
868 _cleanup_free_
char *buf
= NULL
;
869 _cleanup_fclose_
FILE *f
= NULL
;
870 CGroupIODeviceWeight
*a
;
874 while (c
->io_device_weights
)
875 cgroup_context_free_io_device_weight(c
, c
->io_device_weights
);
878 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
880 f
= open_memstream(&buf
, &size
);
884 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
886 fputs("IODeviceWeight=\n", f
);
887 LIST_FOREACH(device_weights
, a
, c
->io_device_weights
)
888 fprintf(f
, "IODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
890 r
= fflush_and_check(f
);
893 unit_write_setting(u
, flags
, name
, buf
);
898 } else if (streq(name
, "IODeviceLatencyTargetUSec")) {
903 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
907 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &target
)) > 0) {
909 if (!path_is_normalized(path
))
910 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
912 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
913 CGroupIODeviceLatency
*a
= NULL
, *b
;
915 LIST_FOREACH(device_latencies
, b
, c
->io_device_latencies
) {
916 if (path_equal(b
->path
, path
)) {
923 a
= new0(CGroupIODeviceLatency
, 1);
927 a
->path
= strdup(path
);
932 LIST_PREPEND(device_latencies
, c
->io_device_latencies
, a
);
935 a
->target_usec
= target
;
941 r
= sd_bus_message_exit_container(message
);
945 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
946 _cleanup_free_
char *buf
= NULL
;
947 _cleanup_fclose_
FILE *f
= NULL
;
948 char ts
[FORMAT_TIMESPAN_MAX
];
949 CGroupIODeviceLatency
*a
;
953 while (c
->io_device_latencies
)
954 cgroup_context_free_io_device_latency(c
, c
->io_device_latencies
);
957 unit_invalidate_cgroup(u
, CGROUP_MASK_IO
);
959 f
= open_memstream(&buf
, &size
);
963 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
965 fputs("IODeviceLatencyTargetSec=\n", f
);
966 LIST_FOREACH(device_latencies
, a
, c
->io_device_latencies
)
967 fprintf(f
, "IODeviceLatencyTargetSec=%s %s\n",
968 a
->path
, format_timespan(ts
, sizeof(ts
), a
->target_usec
, 1));
970 r
= fflush_and_check(f
);
973 unit_write_setting(u
, flags
, name
, buf
);
978 } else if (STR_IN_SET(name
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
984 if (streq(name
, "BlockIOWriteBandwidth"))
987 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
991 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &u64
)) > 0) {
993 if (!path_is_normalized(path
))
994 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
996 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
997 CGroupBlockIODeviceBandwidth
*a
= NULL
, *b
;
999 LIST_FOREACH(device_bandwidths
, b
, c
->blockio_device_bandwidths
) {
1000 if (path_equal(path
, b
->path
)) {
1007 a
= new0(CGroupBlockIODeviceBandwidth
, 1);
1011 a
->rbps
= CGROUP_LIMIT_MAX
;
1012 a
->wbps
= CGROUP_LIMIT_MAX
;
1013 a
->path
= strdup(path
);
1019 LIST_PREPEND(device_bandwidths
, c
->blockio_device_bandwidths
, a
);
1033 r
= sd_bus_message_exit_container(message
);
1037 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1038 CGroupBlockIODeviceBandwidth
*a
;
1039 _cleanup_free_
char *buf
= NULL
;
1040 _cleanup_fclose_
FILE *f
= NULL
;
1044 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
) {
1046 a
->rbps
= CGROUP_LIMIT_MAX
;
1048 a
->wbps
= CGROUP_LIMIT_MAX
;
1052 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1054 f
= open_memstream(&buf
, &size
);
1058 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1061 fputs("BlockIOReadBandwidth=\n", f
);
1062 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1063 if (a
->rbps
!= CGROUP_LIMIT_MAX
)
1064 fprintf(f
, "BlockIOReadBandwidth=%s %" PRIu64
"\n", a
->path
, a
->rbps
);
1066 fputs("BlockIOWriteBandwidth=\n", f
);
1067 LIST_FOREACH(device_bandwidths
, a
, c
->blockio_device_bandwidths
)
1068 if (a
->wbps
!= CGROUP_LIMIT_MAX
)
1069 fprintf(f
, "BlockIOWriteBandwidth=%s %" PRIu64
"\n", a
->path
, a
->wbps
);
1072 r
= fflush_and_check(f
);
1076 unit_write_setting(u
, flags
, name
, buf
);
1081 } else if (streq(name
, "BlockIODeviceWeight")) {
1086 r
= sd_bus_message_enter_container(message
, 'a', "(st)");
1090 while ((r
= sd_bus_message_read(message
, "(st)", &path
, &weight
)) > 0) {
1092 if (!path_is_normalized(path
))
1093 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path '%s' specified in %s= is not normalized.", name
, path
);
1095 if (!CGROUP_BLKIO_WEIGHT_IS_OK(weight
) || weight
== CGROUP_BLKIO_WEIGHT_INVALID
)
1096 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "BlockIODeviceWeight= out of range");
1098 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1099 CGroupBlockIODeviceWeight
*a
= NULL
, *b
;
1101 LIST_FOREACH(device_weights
, b
, c
->blockio_device_weights
) {
1102 if (path_equal(b
->path
, path
)) {
1109 a
= new0(CGroupBlockIODeviceWeight
, 1);
1113 a
->path
= strdup(path
);
1118 LIST_PREPEND(device_weights
, c
->blockio_device_weights
, a
);
1127 r
= sd_bus_message_exit_container(message
);
1131 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1132 _cleanup_free_
char *buf
= NULL
;
1133 _cleanup_fclose_
FILE *f
= NULL
;
1134 CGroupBlockIODeviceWeight
*a
;
1138 while (c
->blockio_device_weights
)
1139 cgroup_context_free_blockio_device_weight(c
, c
->blockio_device_weights
);
1142 unit_invalidate_cgroup(u
, CGROUP_MASK_BLKIO
);
1144 f
= open_memstream(&buf
, &size
);
1148 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1150 fputs("BlockIODeviceWeight=\n", f
);
1151 LIST_FOREACH(device_weights
, a
, c
->blockio_device_weights
)
1152 fprintf(f
, "BlockIODeviceWeight=%s %" PRIu64
"\n", a
->path
, a
->weight
);
1154 r
= fflush_and_check(f
);
1158 unit_write_setting(u
, flags
, name
, buf
);
1163 } else if (streq(name
, "DevicePolicy")) {
1165 CGroupDevicePolicy p
;
1167 r
= sd_bus_message_read(message
, "s", &policy
);
1171 p
= cgroup_device_policy_from_string(policy
);
1175 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1176 c
->device_policy
= p
;
1177 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1178 unit_write_settingf(u
, flags
, name
, "DevicePolicy=%s", policy
);
1183 } else if (streq(name
, "DeviceAllow")) {
1184 const char *path
, *rwm
;
1187 r
= sd_bus_message_enter_container(message
, 'a', "(ss)");
1191 while ((r
= sd_bus_message_read(message
, "(ss)", &path
, &rwm
)) > 0) {
1193 if (!valid_device_allow_pattern(path
) || strpbrk(path
, WHITESPACE
))
1194 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires device node or pattern");
1198 else if (!in_charset(rwm
, "rwm"))
1199 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "DeviceAllow= requires combination of rwm flags");
1201 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1202 CGroupDeviceAllow
*a
= NULL
, *b
;
1204 LIST_FOREACH(device_allow
, b
, c
->device_allow
) {
1205 if (path_equal(b
->path
, path
)) {
1212 a
= new0(CGroupDeviceAllow
, 1);
1216 a
->path
= strdup(path
);
1222 LIST_PREPEND(device_allow
, c
->device_allow
, a
);
1225 a
->r
= !!strchr(rwm
, 'r');
1226 a
->w
= !!strchr(rwm
, 'w');
1227 a
->m
= !!strchr(rwm
, 'm');
1235 r
= sd_bus_message_exit_container(message
);
1239 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1240 _cleanup_free_
char *buf
= NULL
;
1241 _cleanup_fclose_
FILE *f
= NULL
;
1242 CGroupDeviceAllow
*a
;
1246 while (c
->device_allow
)
1247 cgroup_context_free_device_allow(c
, c
->device_allow
);
1250 unit_invalidate_cgroup(u
, CGROUP_MASK_DEVICES
);
1252 f
= open_memstream(&buf
, &size
);
1256 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1258 fputs("DeviceAllow=\n", f
);
1259 LIST_FOREACH(device_allow
, a
, c
->device_allow
)
1260 fprintf(f
, "DeviceAllow=%s %s%s%s\n", a
->path
, a
->r
? "r" : "", a
->w
? "w" : "", a
->m
? "m" : "");
1262 r
= fflush_and_check(f
);
1265 unit_write_setting(u
, flags
, name
, buf
);
1270 } else if (streq(name
, "IPAccounting")) {
1273 r
= sd_bus_message_read(message
, "b", &b
);
1277 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1278 c
->ip_accounting
= b
;
1280 unit_invalidate_cgroup_bpf(u
);
1281 unit_write_settingf(u
, flags
, name
, "IPAccounting=%s", yes_no(b
));
1286 } else if (STR_IN_SET(name
, "IPAddressAllow", "IPAddressDeny")) {
1287 IPAddressAccessItem
**list
;
1290 list
= streq(name
, "IPAddressAllow") ? &c
->ip_address_allow
: &c
->ip_address_deny
;
1292 r
= sd_bus_message_enter_container(message
, 'a', "(iayu)");
1302 r
= sd_bus_message_enter_container(message
, 'r', "iayu");
1308 r
= sd_bus_message_read(message
, "i", &family
);
1312 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1313 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "%s= expects IPv4 or IPv6 addresses only.", name
);
1315 r
= sd_bus_message_read_array(message
, 'y', &ap
, &an
);
1319 if (an
!= FAMILY_ADDRESS_SIZE(family
))
1320 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "IP address has wrong size for family (%s, expected %zu, got %zu)",
1321 af_to_name(family
), FAMILY_ADDRESS_SIZE(family
), an
);
1323 r
= sd_bus_message_read(message
, "u", &prefixlen
);
1327 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
)*8)
1328 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
));
1330 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1331 IPAddressAccessItem
*item
;
1333 item
= new0(IPAddressAccessItem
, 1);
1337 item
->family
= family
;
1338 item
->prefixlen
= prefixlen
;
1339 memcpy(&item
->address
, ap
, an
);
1341 LIST_PREPEND(items
, *list
, item
);
1344 r
= sd_bus_message_exit_container(message
);
1351 r
= sd_bus_message_exit_container(message
);
1355 *list
= ip_address_access_reduce(*list
);
1357 if (!UNIT_WRITE_FLAGS_NOOP(flags
)) {
1358 _cleanup_free_
char *buf
= NULL
;
1359 _cleanup_fclose_
FILE *f
= NULL
;
1360 IPAddressAccessItem
*item
;
1364 *list
= ip_address_access_free_all(*list
);
1366 unit_invalidate_cgroup_bpf(u
);
1367 f
= open_memstream(&buf
, &size
);
1371 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
1376 LIST_FOREACH(items
, item
, *list
) {
1377 char buffer
[CONST_MAX(INET_ADDRSTRLEN
, INET6_ADDRSTRLEN
)];
1380 if (!inet_ntop(item
->family
, &item
->address
, buffer
, sizeof(buffer
)))
1381 return errno
> 0 ? -errno
: -EINVAL
;
1383 fprintf(f
, "%s=%s/%u\n", name
, buffer
, item
->prefixlen
);
1386 r
= fflush_and_check(f
);
1390 unit_write_setting(u
, flags
, name
, buf
);
1393 r
= bpf_firewall_supported();
1396 if (r
== BPF_FIREWALL_UNSUPPORTED
) {
1397 static bool warned
= false;
1399 log_full(warned
? LOG_DEBUG
: LOG_WARNING
,
1400 "Transient unit %s configures an IP firewall, but the local system does not support BPF/cgroup firewalling.\n"
1401 "Proceeding WITHOUT firewalling in effect! (This warning is only shown for the first started transient unit using IP firewalling.)", u
->id
);
1411 if (u
->transient
&& u
->load_state
== UNIT_STUB
)
1412 return bus_cgroup_set_transient_property(u
, c
, name
, message
, flags
, error
);