Debian Codegraph @

src/core/dbus-cgroup.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "af-list.h"
4 #include "alloc-util.h"
5 #include "bpf-program.h"
6 #include "bus-get-properties.h"
7 #include "bus-message-util.h"
8 #include "cgroup.h"
9 #include "cgroup-util.h"
10 #include "dbus-cgroup.h"
11 #include "escape.h"
12 #include "in-addr-prefix-util.h"
13 #include "limits-util.h"
14 #include "manager.h"
15 #include "memstream-util.h"
16 #include "parse-util.h"
17 #include "path-util.h"
18 #include "percent-util.h"
19 #include "set.h"
20 #include "socket-util.h"
21 #include "string-util.h"
22 #include "strv.h"
23 #include "varlink.h"
24
25 BUS_DEFINE_PROPERTY_GET(bus_property_get_tasks_max, "t", CGroupTasksMax, cgroup_tasks_max_resolve);
26 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_cgroup_pressure_watch, cgroup_pressure_watch, CGroupPressureWatch);
27
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_cpuset_partition, cpuset_partition, CPUSetPartition);
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_mode, managed_oom_mode, ManagedOOMMode);
31 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_managed_oom_preference, managed_oom_preference, ManagedOOMPreference);
32
33 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_blockio_ast, "a(st)", 0);
34
35 static int property_get_cgroup_mask(
36 sd_bus *bus,
37 const char *path,
38 const char *interface,
39 const char *property,
40 sd_bus_message *reply,
41 void *userdata,
42 sd_bus_error *reterr_error) {
43
44 CGroupMask *mask = userdata;
45 int r;
46
47 assert(bus);
48 assert(reply);
49
50 r = sd_bus_message_open_container(reply, 'a', "s");
51 if (r < 0)
52 return r;
53
54 for (CGroupController ctrl = 0; ctrl < _CGROUP_CONTROLLER_MAX; ctrl++) {
55 if ((*mask & CGROUP_CONTROLLER_TO_MASK(ctrl)) == 0)
56 continue;
57
58 r = sd_bus_message_append(reply, "s", cgroup_controller_to_string(ctrl));
59 if (r < 0)
60 return r;
61 }
62
63 return sd_bus_message_close_container(reply);
64 }
65
66 static int property_get_delegate_controllers(
67 sd_bus *bus,
68 const char *path,
69 const char *interface,
70 const char *property,
71 sd_bus_message *reply,
72 void *userdata,
73 sd_bus_error *reterr_error) {
74
75 CGroupContext *c = ASSERT_PTR(userdata);
76
77 assert(bus);
78 assert(reply);
79
80 if (!c->delegate)
81 return sd_bus_message_append(reply, "as", 0);
82
83 return property_get_cgroup_mask(bus, path, interface, property, reply, &c->delegate_controllers, reterr_error);
84 }
85
86 static int property_get_cpuset(
87 sd_bus *bus,
88 const char *path,
89 const char *interface,
90 const char *property,
91 sd_bus_message *reply,
92 void *userdata,
93 sd_bus_error *reterr_error) {
94
95 CPUSet *cpus = ASSERT_PTR(userdata);
96 _cleanup_free_ uint8_t *array = NULL;
97 size_t allocated;
98
99 assert(bus);
100 assert(reply);
101
102 (void) cpu_set_to_dbus(cpus, &array, &allocated);
103 return sd_bus_message_append_array(reply, 'y', array, allocated);
104 }
105
106 static int property_get_io_device_weight(
107 sd_bus *bus,
108 const char *path,
109 const char *interface,
110 const char *property,
111 sd_bus_message *reply,
112 void *userdata,
113 sd_bus_error *reterr_error) {
114
115 CGroupContext *c = ASSERT_PTR(userdata);
116 int r;
117
118 assert(bus);
119 assert(reply);
120
121 r = sd_bus_message_open_container(reply, 'a', "(st)");
122 if (r < 0)
123 return r;
124
125 LIST_FOREACH(device_weights, w, c->io_device_weights) {
126 r = sd_bus_message_append(reply, "(st)", w->path, w->weight);
127 if (r < 0)
128 return r;
129 }
130
131 return sd_bus_message_close_container(reply);
132 }
133
134 static int property_get_io_device_limits(
135 sd_bus *bus,
136 const char *path,
137 const char *interface,
138 const char *property,
139 sd_bus_message *reply,
140 void *userdata,
141 sd_bus_error *reterr_error) {
142
143 CGroupContext *c = ASSERT_PTR(userdata);
144 int r;
145
146 assert(bus);
147 assert(reply);
148
149 r = sd_bus_message_open_container(reply, 'a', "(st)");
150 if (r < 0)
151 return r;
152
153 LIST_FOREACH(device_limits, l, c->io_device_limits) {
154 CGroupIOLimitType type;
155
156 type = cgroup_io_limit_type_from_string(property);
157 if (type < 0 || l->limits[type] == cgroup_io_limit_defaults[type])
158 continue;
159
160 r = sd_bus_message_append(reply, "(st)", l->path, l->limits[type]);
161 if (r < 0)
162 return r;
163 }
164
165 return sd_bus_message_close_container(reply);
166 }
167
168 static int property_get_io_device_latency(
169 sd_bus *bus,
170 const char *path,
171 const char *interface,
172 const char *property,
173 sd_bus_message *reply,
174 void *userdata,
175 sd_bus_error *reterr_error) {
176
177 CGroupContext *c = ASSERT_PTR(userdata);
178 int r;
179
180 assert(bus);
181 assert(reply);
182
183 r = sd_bus_message_open_container(reply, 'a', "(st)");
184 if (r < 0)
185 return r;
186
187 LIST_FOREACH(device_latencies, l, c->io_device_latencies) {
188 r = sd_bus_message_append(reply, "(st)", l->path, l->target_usec);
189 if (r < 0)
190 return r;
191 }
192
193 return sd_bus_message_close_container(reply);
194 }
195
196 static int property_get_device_allow(
197 sd_bus *bus,
198 const char *path,
199 const char *interface,
200 const char *property,
201 sd_bus_message *reply,
202 void *userdata,
203 sd_bus_error *reterr_error) {
204
205 CGroupContext *c = ASSERT_PTR(userdata);
206 int r;
207
208 assert(bus);
209 assert(reply);
210
211 r = sd_bus_message_open_container(reply, 'a', "(ss)");
212 if (r < 0)
213 return r;
214
215 LIST_FOREACH(device_allow, a, c->device_allow) {
216 r = sd_bus_message_append(reply, "(ss)", a->path, cgroup_device_permissions_to_string(a->permissions));
217 if (r < 0)
218 return r;
219 }
220
221 return sd_bus_message_close_container(reply);
222 }
223
224 static int property_get_ip_address_access(
225 sd_bus *bus,
226 const char *path,
227 const char *interface,
228 const char *property,
229 sd_bus_message *reply,
230 void *userdata,
231 sd_bus_error *reterr_error) {
232
233 Set **prefixes = ASSERT_PTR(userdata);
234 struct in_addr_prefix *i;
235 int r;
236
237 r = sd_bus_message_open_container(reply, 'a', "(iayu)");
238 if (r < 0)
239 return r;
240
241 SET_FOREACH(i, *prefixes) {
242
243 r = sd_bus_message_open_container(reply, 'r', "iayu");
244 if (r < 0)
245 return r;
246
247 r = sd_bus_message_append(reply, "i", i->family);
248 if (r < 0)
249 return r;
250
251 r = sd_bus_message_append_array(reply, 'y', &i->address, FAMILY_ADDRESS_SIZE(i->family));
252 if (r < 0)
253 return r;
254
255 r = sd_bus_message_append(reply, "u", (uint32_t) i->prefixlen);
256 if (r < 0)
257 return r;
258
259 r = sd_bus_message_close_container(reply);
260 if (r < 0)
261 return r;
262 }
263
264 return sd_bus_message_close_container(reply);
265 }
266
267 static int property_get_bpf_foreign_program(
268 sd_bus *bus,
269 const char *path,
270 const char *interface,
271 const char *property,
272 sd_bus_message *reply,
273 void *userdata,
274 sd_bus_error *reterr_error) {
275 CGroupContext *c = userdata;
276 int r;
277
278 r = sd_bus_message_open_container(reply, 'a', "(ss)");
279 if (r < 0)
280 return r;
281
282 LIST_FOREACH(programs, p, c->bpf_foreign_programs) {
283 const char *attach_type = bpf_cgroup_attach_type_to_string(p->attach_type);
284
285 r = sd_bus_message_append(reply, "(ss)", attach_type, p->bpffs_path);
286 if (r < 0)
287 return r;
288 }
289
290 return sd_bus_message_close_container(reply);
291 }
292
293 static int property_get_socket_bind(
294 sd_bus *bus,
295 const char *path,
296 const char *interface,
297 const char *property,
298 sd_bus_message *reply,
299 void *userdata,
300 sd_bus_error *reterr_error) {
301
302 CGroupSocketBindItem **items = ASSERT_PTR(userdata);
303 int r;
304
305 r = sd_bus_message_open_container(reply, 'a', "(iiqq)");
306 if (r < 0)
307 return r;
308
309 LIST_FOREACH(socket_bind_items, i, *items) {
310 r = sd_bus_message_append(reply, "(iiqq)", i->address_family, i->ip_protocol, i->nr_ports, i->port_min);
311 if (r < 0)
312 return r;
313 }
314
315 return sd_bus_message_close_container(reply);
316 }
317
318 static int property_get_restrict_network_interfaces(
319 sd_bus *bus,
320 const char *path,
321 const char *interface,
322 const char *property,
323 sd_bus_message *reply,
324 void *userdata,
325 sd_bus_error *reterr_error) {
326
327 CGroupContext *c = ASSERT_PTR(userdata);
328 int r;
329
330 assert(bus);
331 assert(reply);
332
333 r = sd_bus_message_open_container(reply, 'r', "bas");
334 if (r < 0)
335 return r;
336
337 r = sd_bus_message_append(reply, "b", c->restrict_network_interfaces_is_allow_list);
338 if (r < 0)
339 return r;
340
341 r = bus_message_append_string_set(reply, c->restrict_network_interfaces);
342 if (r < 0)
343 return r;
344
345 return sd_bus_message_close_container(reply);
346 }
347
348 static int property_get_cgroup_nft_set(
349 sd_bus *bus,
350 const char *path,
351 const char *interface,
352 const char *property,
353 sd_bus_message *reply,
354 void *userdata,
355 sd_bus_error *reterr_error) {
356 int r;
357 CGroupContext *c = userdata;
358
359 assert(bus);
360 assert(reply);
361 assert(c);
362
363 r = sd_bus_message_open_container(reply, 'a', "(iiss)");
364 if (r < 0)
365 return r;
366
367 FOREACH_ARRAY(nft_set, c->nft_set_context.sets, c->nft_set_context.n_sets) {
368 r = sd_bus_message_append(reply, "(iiss)", nft_set->source, nft_set->nfproto, nft_set->table, nft_set->set);
369 if (r < 0)
370 return r;
371 }
372
373 return sd_bus_message_close_container(reply);
374 }
375
376 const sd_bus_vtable bus_cgroup_vtable[] = {
377 SD_BUS_VTABLE_START(0),
378 SD_BUS_PROPERTY("Delegate", "b", bus_property_get_bool, offsetof(CGroupContext, delegate), 0),
379 SD_BUS_PROPERTY("DelegateControllers", "as", property_get_delegate_controllers, 0, 0),
380 SD_BUS_PROPERTY("DelegateSubgroup", "s", NULL, offsetof(CGroupContext, delegate_subgroup), 0),
381 SD_BUS_PROPERTY("CPUWeight", "t", NULL, offsetof(CGroupContext, cpu_weight), 0),
382 SD_BUS_PROPERTY("StartupCPUWeight", "t", NULL, offsetof(CGroupContext, startup_cpu_weight), 0),
383 SD_BUS_PROPERTY("CPUQuotaPerSecUSec", "t", bus_property_get_usec, offsetof(CGroupContext, cpu_quota_per_sec_usec), 0),
384 SD_BUS_PROPERTY("CPUQuotaPeriodUSec", "t", bus_property_get_usec, offsetof(CGroupContext, cpu_quota_period_usec), 0),
385 SD_BUS_PROPERTY("AllowedCPUs", "ay", property_get_cpuset, offsetof(CGroupContext, cpuset_cpus), 0),
386 SD_BUS_PROPERTY("StartupAllowedCPUs", "ay", property_get_cpuset, offsetof(CGroupContext, startup_cpuset_cpus), 0),
387 SD_BUS_PROPERTY("AllowedMemoryNodes", "ay", property_get_cpuset, offsetof(CGroupContext, cpuset_mems), 0),
388 SD_BUS_PROPERTY("StartupAllowedMemoryNodes", "ay", property_get_cpuset, offsetof(CGroupContext, startup_cpuset_mems), 0),
389 SD_BUS_PROPERTY("CPUSetPartition", "s", property_get_cpuset_partition, offsetof(CGroupContext, cpuset_partition), 0),
390 SD_BUS_PROPERTY("IOAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, io_accounting), 0),
391 SD_BUS_PROPERTY("IOWeight", "t", NULL, offsetof(CGroupContext, io_weight), 0),
392 SD_BUS_PROPERTY("StartupIOWeight", "t", NULL, offsetof(CGroupContext, startup_io_weight), 0),
393 SD_BUS_PROPERTY("IODeviceWeight", "a(st)", property_get_io_device_weight, 0, 0),
394 SD_BUS_PROPERTY("IOReadBandwidthMax", "a(st)", property_get_io_device_limits, 0, 0),
395 SD_BUS_PROPERTY("IOWriteBandwidthMax", "a(st)", property_get_io_device_limits, 0, 0),
396 SD_BUS_PROPERTY("IOReadIOPSMax", "a(st)", property_get_io_device_limits, 0, 0),
397 SD_BUS_PROPERTY("IOWriteIOPSMax", "a(st)", property_get_io_device_limits, 0, 0),
398 SD_BUS_PROPERTY("IODeviceLatencyTargetUSec", "a(st)", property_get_io_device_latency, 0, 0),
399 SD_BUS_PROPERTY("MemoryAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, memory_accounting), 0),
400 SD_BUS_PROPERTY("MemoryMin", "t", NULL, offsetof(CGroupContext, memory_min), 0),
401 SD_BUS_PROPERTY("MemoryLow", "t", NULL, offsetof(CGroupContext, memory_low), 0),
402 SD_BUS_PROPERTY("StartupMemoryLow", "t", NULL, offsetof(CGroupContext, startup_memory_low), 0),
403 SD_BUS_PROPERTY("MemoryHigh", "t", NULL, offsetof(CGroupContext, memory_high), 0),
404 SD_BUS_PROPERTY("StartupMemoryHigh", "t", NULL, offsetof(CGroupContext, startup_memory_high), 0),
405 SD_BUS_PROPERTY("MemoryMax", "t", NULL, offsetof(CGroupContext, memory_max), 0),
406 SD_BUS_PROPERTY("StartupMemoryMax", "t", NULL, offsetof(CGroupContext, startup_memory_max), 0),
407 SD_BUS_PROPERTY("MemorySwapMax", "t", NULL, offsetof(CGroupContext, memory_swap_max), 0),
408 SD_BUS_PROPERTY("StartupMemorySwapMax", "t", NULL, offsetof(CGroupContext, startup_memory_swap_max), 0),
409 SD_BUS_PROPERTY("MemoryZSwapMax", "t", NULL, offsetof(CGroupContext, memory_zswap_max), 0),
410 SD_BUS_PROPERTY("StartupMemoryZSwapMax", "t", NULL, offsetof(CGroupContext, startup_memory_zswap_max), 0),
411 SD_BUS_PROPERTY("MemoryZSwapWriteback", "b", bus_property_get_bool, offsetof(CGroupContext, memory_zswap_writeback), 0),
412 SD_BUS_PROPERTY("DevicePolicy", "s", property_get_cgroup_device_policy, offsetof(CGroupContext, device_policy), 0),
413 SD_BUS_PROPERTY("DeviceAllow", "a(ss)", property_get_device_allow, 0, 0),
414 SD_BUS_PROPERTY("TasksAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, tasks_accounting), 0),
415 SD_BUS_PROPERTY("TasksMax", "t", bus_property_get_tasks_max, offsetof(CGroupContext, tasks_max), 0),
416 SD_BUS_PROPERTY("IPAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, ip_accounting), 0),
417 SD_BUS_PROPERTY("IPAddressAllow", "a(iayu)", property_get_ip_address_access, offsetof(CGroupContext, ip_address_allow), 0),
418 SD_BUS_PROPERTY("IPAddressDeny", "a(iayu)", property_get_ip_address_access, offsetof(CGroupContext, ip_address_deny), 0),
419 SD_BUS_PROPERTY("IPIngressFilterPath", "as", NULL, offsetof(CGroupContext, ip_filters_ingress), 0),
420 SD_BUS_PROPERTY("IPEgressFilterPath", "as", NULL, offsetof(CGroupContext, ip_filters_egress), 0),
421 SD_BUS_PROPERTY("DisableControllers", "as", property_get_cgroup_mask, offsetof(CGroupContext, disable_controllers), 0),
422 SD_BUS_PROPERTY("ManagedOOMSwap", "s", property_get_managed_oom_mode, offsetof(CGroupContext, moom_swap), 0),
423 SD_BUS_PROPERTY("ManagedOOMMemoryPressure", "s", property_get_managed_oom_mode, offsetof(CGroupContext, moom_mem_pressure), 0),
424 SD_BUS_PROPERTY("ManagedOOMMemoryPressureLimit", "u", NULL, offsetof(CGroupContext, moom_mem_pressure_limit), 0),
425 SD_BUS_PROPERTY("ManagedOOMMemoryPressureDurationUSec", "t", bus_property_get_usec, offsetof(CGroupContext, moom_mem_pressure_duration_usec), 0),
426 SD_BUS_PROPERTY("ManagedOOMPreference", "s", property_get_managed_oom_preference, offsetof(CGroupContext, moom_preference), 0),
427 SD_BUS_PROPERTY("OOMRules", "as", NULL, offsetof(CGroupContext, moom_rules), 0),
428 SD_BUS_PROPERTY("BPFProgram", "a(ss)", property_get_bpf_foreign_program, 0, 0),
429 SD_BUS_PROPERTY("SocketBindAllow", "a(iiqq)", property_get_socket_bind, offsetof(CGroupContext, socket_bind_allow), 0),
430 SD_BUS_PROPERTY("SocketBindDeny", "a(iiqq)", property_get_socket_bind, offsetof(CGroupContext, socket_bind_deny), 0),
431 SD_BUS_PROPERTY("RestrictNetworkInterfaces", "(bas)", property_get_restrict_network_interfaces, 0, 0),
432 SD_BUS_PROPERTY("BindNetworkInterface", "s", NULL, offsetof(CGroupContext, bind_network_interface), 0),
433 SD_BUS_PROPERTY("MemoryPressureWatch", "s", bus_property_get_cgroup_pressure_watch, offsetof(CGroupContext, pressure[PRESSURE_MEMORY].watch), 0),
434 SD_BUS_PROPERTY("MemoryPressureThresholdUSec", "t", bus_property_get_usec, offsetof(CGroupContext, pressure[PRESSURE_MEMORY].threshold_usec), 0),
435 SD_BUS_PROPERTY("CPUPressureWatch", "s", bus_property_get_cgroup_pressure_watch, offsetof(CGroupContext, pressure[PRESSURE_CPU].watch), 0),
436 SD_BUS_PROPERTY("CPUPressureThresholdUSec", "t", bus_property_get_usec, offsetof(CGroupContext, pressure[PRESSURE_CPU].threshold_usec), 0),
437 SD_BUS_PROPERTY("IOPressureWatch", "s", bus_property_get_cgroup_pressure_watch, offsetof(CGroupContext, pressure[PRESSURE_IO].watch), 0),
438 SD_BUS_PROPERTY("IOPressureThresholdUSec", "t", bus_property_get_usec, offsetof(CGroupContext, pressure[PRESSURE_IO].threshold_usec), 0),
439 SD_BUS_PROPERTY("NFTSet", "a(iiss)", property_get_cgroup_nft_set, 0, 0),
440 SD_BUS_PROPERTY("CoredumpReceive", "b", bus_property_get_bool, offsetof(CGroupContext, coredump_receive), 0),
441
442 /* deprecated cgroup v1 properties */
443 SD_BUS_PROPERTY("MemoryLimit", "t", bus_property_get_uint64_max, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
444 SD_BUS_PROPERTY("CPUShares", "t", bus_property_get_uint64_max, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
445 SD_BUS_PROPERTY("StartupCPUShares", "t", bus_property_get_uint64_max, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
446 SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool_false, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
447 SD_BUS_PROPERTY("BlockIOWeight", "t", bus_property_get_uint64_max, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
448 SD_BUS_PROPERTY("StartupBlockIOWeight", "t", bus_property_get_uint64_max, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
449 SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_ast, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
450 SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_ast, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
451 SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_ast, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
452 /* since kernel v4.15 CPU accounting requires no controller, i.e. is available everywhere */
453 SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool_true, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
454 /* since kernel v5.7 kernel would take care of these when cgroup is mounted with memory_recursiveprot */
455 SD_BUS_PROPERTY("DefaultMemoryMin", "t", bus_property_get_uint64_0, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
456 SD_BUS_PROPERTY("DefaultMemoryLow", "t", bus_property_get_uint64_0, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
457 SD_BUS_PROPERTY("DefaultStartupMemoryLow", "t", bus_property_get_uint64_0, 0, SD_BUS_VTABLE_DEPRECATED|SD_BUS_VTABLE_HIDDEN),
458
459 SD_BUS_VTABLE_END
460 };
461
462 static int bus_cgroup_set_transient_property(
463 Unit *u,
464 CGroupContext *c,
465 const char *name,
466 sd_bus_message *message,
467 UnitWriteFlags flags,
468 sd_bus_error *reterr_error) {
469
470 int r;
471
472 assert(u);
473 assert(c);
474 assert(name);
475 assert(message);
476
477 flags |= UNIT_PRIVATE;
478
479 if (streq(name, "Delegate")) {
480 int b;
481
482 if (!UNIT_VTABLE(u)->can_delegate)
483 return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Delegation not available for unit type");
484
485 r = sd_bus_message_read(message, "b", &b);
486 if (r < 0)
487 return r;
488
489 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
490 c->delegate = b;
491 c->delegate_controllers = b ? CGROUP_MASK_DELEGATE : 0;
492
493 unit_write_settingf(u, flags, name, "Delegate=%s", yes_no(b));
494 }
495
496 return 1;
497
498 } else if (streq(name, "DelegateSubgroup")) {
499 const char *s;
500
501 if (!UNIT_VTABLE(u)->can_delegate)
502 return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Delegation not available for unit type");
503
504 r = sd_bus_message_read(message, "s", &s);
505 if (r < 0)
506 return r;
507
508 if (!isempty(s) && cg_needs_escape(s))
509 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid control group name: %s", s);
510
511 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
512 if (isempty(s))
513 c->delegate_subgroup = mfree(c->delegate_subgroup);
514 else {
515 r = free_and_strdup_warn(&c->delegate_subgroup, s);
516 if (r < 0)
517 return r;
518 }
519
520 unit_write_settingf(u, flags, name, "DelegateSubgroup=%s", s);
521 }
522
523 return 1;
524
525 } else if (STR_IN_SET(name, "DelegateControllers", "DisableControllers")) {
526 CGroupMask mask = 0;
527
528 if (streq(name, "DelegateControllers") && !UNIT_VTABLE(u)->can_delegate)
529 return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Delegation not available for unit type");
530
531 r = sd_bus_message_enter_container(message, 'a', "s");
532 if (r < 0)
533 return r;
534
535 for (;;) {
536 CGroupController cc;
537 const char *t;
538
539 r = sd_bus_message_read(message, "s", &t);
540 if (r < 0)
541 return r;
542 if (r == 0)
543 break;
544
545 cc = cgroup_controller_from_string(t);
546 if (cc < 0)
547 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Unknown cgroup controller '%s'", t);
548
549 mask |= CGROUP_CONTROLLER_TO_MASK(cc);
550 }
551
552 r = sd_bus_message_exit_container(message);
553 if (r < 0)
554 return r;
555
556 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
557 _cleanup_free_ char *t = NULL;
558
559 r = cg_mask_to_string(mask, &t);
560 if (r < 0)
561 return r;
562
563 if (streq(name, "DelegateControllers")) {
564
565 c->delegate = true;
566 if (mask == 0)
567 c->delegate_controllers = 0;
568 else
569 c->delegate_controllers |= mask;
570
571 unit_write_settingf(u, flags, name, "Delegate=%s", strempty(t));
572
573 } else if (streq(name, "DisableControllers")) {
574
575 if (mask == 0)
576 c->disable_controllers = 0;
577 else
578 c->disable_controllers |= mask;
579
580 unit_write_settingf(u, flags, name, "%s=%s", name, strempty(t));
581 }
582 }
583
584 return 1;
585 } else if (STR_IN_SET(name, "IPIngressFilterPath", "IPEgressFilterPath")) {
586 char ***filters;
587 size_t n = 0;
588
589 filters = streq(name, "IPIngressFilterPath") ? &c->ip_filters_ingress : &c->ip_filters_egress;
590 r = sd_bus_message_enter_container(message, 'a', "s");
591 if (r < 0)
592 return r;
593
594 for (;;) {
595 const char *path;
596
597 r = sd_bus_message_read(message, "s", &path);
598 if (r < 0)
599 return r;
600 if (r == 0)
601 break;
602
603 if (!path_is_normalized(path) || !path_is_absolute(path))
604 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "%s= expects a normalized absolute path.", name);
605
606 if (!UNIT_WRITE_FLAGS_NOOP(flags) && !strv_contains(*filters, path)) {
607 r = strv_extend(filters, path);
608 if (r < 0)
609 return log_oom();
610 }
611 n++;
612 }
613 r = sd_bus_message_exit_container(message);
614 if (r < 0)
615 return r;
616
617 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
618 _cleanup_(memstream_done) MemStream m = {};
619 _cleanup_free_ char *buf = NULL;
620 FILE *f;
621
622 if (n == 0)
623 *filters = strv_free(*filters);
624
625 unit_invalidate_cgroup_bpf_firewall(u);
626
627 f = memstream_init(&m);
628 if (!f)
629 return -ENOMEM;
630
631 fputs(name, f);
632 fputs("=\n", f);
633
634 STRV_FOREACH(entry, *filters)
635 fprintf(f, "%s=%s\n", name, *entry);
636
637 r = memstream_finalize(&m, &buf, NULL);
638 if (r < 0)
639 return r;
640
641 unit_write_setting(u, flags, name, buf);
642
643 if (*filters && bpf_program_supported() <= 0) {
644 static bool warned = false;
645
646 log_full(warned ? LOG_DEBUG : LOG_WARNING,
647 "Transient unit %s configures an IP firewall with BPF, but the local system does not support BPF/cgroup firewalling with multiple filters.\n"
648 "Starting this unit will fail! (This warning is only shown for the first started transient unit using IP firewalling.)", u->id);
649 warned = true;
650 }
651 }
652
653 return 1;
654 } else if (streq(name, "BPFProgram")) {
655 const char *a, *p;
656 size_t n = 0;
657
658 r = sd_bus_message_enter_container(message, 'a', "(ss)");
659 if (r < 0)
660 return r;
661
662 while ((r = sd_bus_message_read(message, "(ss)", &a, &p)) > 0) {
663 int attach_type = bpf_cgroup_attach_type_from_string(a);
664 if (attach_type < 0)
665 return sd_bus_error_setf(
666 reterr_error,
667 SD_BUS_ERROR_INVALID_ARGS,
668 "%s expects a valid BPF attach type, got '%s'.",
669 name, a);
670
671 if (!path_is_normalized(p) || !path_is_absolute(p))
672 return sd_bus_error_setf(
673 reterr_error,
674 SD_BUS_ERROR_INVALID_ARGS,
675 "%s= expects a normalized absolute path.",
676 name);
677
678 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
679 r = cgroup_context_add_bpf_foreign_program(c, attach_type, p);
680 if (r < 0)
681 return r;
682 }
683 n++;
684 }
685 if (r < 0)
686 return r;
687
688 r = sd_bus_message_exit_container(message);
689 if (r < 0)
690 return r;
691
692 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
693 _cleanup_(memstream_done) MemStream m = {};
694 _cleanup_free_ char *buf = NULL;
695 FILE *f;
696
697 if (n == 0)
698 while (c->bpf_foreign_programs)
699 cgroup_context_remove_bpf_foreign_program(c, c->bpf_foreign_programs);
700
701 f = memstream_init(&m);
702 if (!f)
703 return -ENOMEM;
704
705 fputs(name, f);
706 fputs("=\n", f);
707
708 LIST_FOREACH(programs, fp, c->bpf_foreign_programs)
709 fprintf(f, "%s=%s:%s\n", name,
710 bpf_cgroup_attach_type_to_string(fp->attach_type),
711 fp->bpffs_path);
712
713 r = memstream_finalize(&m, &buf, NULL);
714 if (r < 0)
715 return r;
716
717 unit_write_setting(u, flags, name, buf);
718 }
719
720 return 1;
721
722 } else if (STR_IN_SET(name, "MemoryPressureWatch", "CPUPressureWatch", "IOPressureWatch")) {
723 CGroupPressureWatch p;
724 const char *t;
725
726 PressureResource pt = streq(name, "MemoryPressureWatch") ? PRESSURE_MEMORY :
727 streq(name, "CPUPressureWatch") ? PRESSURE_CPU : PRESSURE_IO;
728
729 r = sd_bus_message_read(message, "s", &t);
730 if (r < 0)
731 return r;
732
733 if (isempty(t))
734 p = _CGROUP_PRESSURE_WATCH_INVALID;
735 else {
736 p = cgroup_pressure_watch_from_string(t);
737 if (p < 0)
738 return p;
739 }
740
741 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
742 c->pressure[pt].watch = p;
743 unit_write_settingf(u, flags, name, "%s=%s", name, strempty(cgroup_pressure_watch_to_string(p)));
744 }
745
746 return 1;
747
748 } else if (STR_IN_SET(name, "MemoryPressureThresholdUSec", "CPUPressureThresholdUSec", "IOPressureThresholdUSec")) {
749 uint64_t t;
750
751 PressureResource pt = streq(name, "MemoryPressureThresholdUSec") ? PRESSURE_MEMORY :
752 streq(name, "CPUPressureThresholdUSec") ? PRESSURE_CPU : PRESSURE_IO;
753
754 r = sd_bus_message_read(message, "t", &t);
755 if (r < 0)
756 return r;
757
758 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
759 c->pressure[pt].threshold_usec = t;
760
761 if (t == UINT64_MAX)
762 unit_write_settingf(u, flags, name, "%s=", name);
763 else
764 unit_write_settingf(u, flags, name, "%s=%" PRIu64, name, t);
765 }
766
767 return 1;
768 } else if (streq(name, "CoredumpReceive")) {
769 int b;
770
771 if (!UNIT_VTABLE(u)->can_delegate)
772 return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Delegation not available for unit type");
773
774 r = sd_bus_message_read(message, "b", &b);
775 if (r < 0)
776 return r;
777
778 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
779 c->coredump_receive = b;
780
781 unit_write_settingf(u, flags, name, "CoredumpReceive=%s", yes_no(b));
782 }
783
784 return 1;
785 }
786
787 return 0;
788 }
789
790 static int bus_cgroup_set_boolean(
791 Unit *u,
792 const char *name,
793 bool *p,
794 CGroupMask mask,
795 sd_bus_message *message,
796 UnitWriteFlags flags,
797 sd_bus_error *reterr_error) {
798
799 int b, r;
800
801 assert(p);
802
803 r = sd_bus_message_read(message, "b", &b);
804 if (r < 0)
805 return r;
806
807 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
808 *p = b;
809 unit_invalidate_cgroup(u, mask);
810 unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(b));
811 }
812
813 return 1;
814 }
815
816 #define BUS_DEFINE_SET_CGROUP_WEIGHT(function, mask, check, val) \
817 static int bus_cgroup_set_##function( \
818 Unit *u, \
819 const char *name, \
820 uint64_t *p, \
821 sd_bus_message *message, \
822 UnitWriteFlags flags, \
823 sd_bus_error *reterr_error) { \
824 \
825 uint64_t v; \
826 int r; \
827 \
828 assert(p); \
829 \
830 r = sd_bus_message_read(message, "t", &v); \
831 if (r < 0) \
832 return r; \
833 \
834 if (!check(v)) \
835 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, \
836 "Value specified in %s is out of range", name); \
837 \
838 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
839 *p = v; \
840 unit_invalidate_cgroup(u, mask); \
841 \
842 if (v == (val)) \
843 unit_write_settingf(u, flags, name, \
844 "%s=", name); \
845 else \
846 unit_write_settingf(u, flags, name, \
847 "%s=%" PRIu64, name, v); \
848 } \
849 \
850 return 1; \
851 }
852
853 #define BUS_DEFINE_SET_CGROUP_LIMIT(function, mask, scale, minimum) \
854 static int bus_cgroup_set_##function( \
855 Unit *u, \
856 const char *name, \
857 uint64_t *p, \
858 sd_bus_message *message, \
859 UnitWriteFlags flags, \
860 sd_bus_error *reterr_error) { \
861 \
862 uint64_t v; \
863 int r; \
864 \
865 assert(p); \
866 \
867 r = sd_bus_message_read(message, "t", &v); \
868 if (r < 0) \
869 return r; \
870 \
871 if (v < minimum) \
872 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, \
873 "Value specified in %s is out of range", name); \
874 \
875 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
876 *p = v; \
877 unit_invalidate_cgroup(u, mask); \
878 \
879 if (v == CGROUP_LIMIT_MAX) \
880 unit_write_settingf(u, flags, name, \
881 "%s=infinity", name); \
882 else \
883 unit_write_settingf(u, flags, name, \
884 "%s=%" PRIu64, name, v); \
885 } \
886 \
887 return 1; \
888 } \
889 static int bus_cgroup_set_##function##_scale( \
890 Unit *u, \
891 const char *name, \
892 uint64_t *p, \
893 sd_bus_message *message, \
894 UnitWriteFlags flags, \
895 sd_bus_error *reterr_error) { \
896 \
897 uint64_t v; \
898 uint32_t raw; \
899 int r; \
900 \
901 assert(p); \
902 \
903 r = sd_bus_message_read(message, "u", &raw); \
904 if (r < 0) \
905 return r; \
906 \
907 v = scale(raw, UINT32_MAX); \
908 if (v < minimum || v >= UINT64_MAX) \
909 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, \
910 "Value specified in %s is out of range", name); \
911 \
912 if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
913 *p = v; \
914 unit_invalidate_cgroup(u, mask); \
915 \
916 /* Prepare to chop off suffix */ \
917 assert_se(endswith(name, "Scale")); \
918 \
919 int scaled = UINT32_SCALE_TO_PERMYRIAD(raw); \
920 unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR, \
921 (int)(strlen(name) - strlen("Scale")), name, \
922 PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled)); \
923 } \
924 \
925 return 1; \
926 }
927
928 DISABLE_WARNING_TYPE_LIMITS;
929 BUS_DEFINE_SET_CGROUP_WEIGHT(io_weight, CGROUP_MASK_IO, CGROUP_WEIGHT_IS_OK, CGROUP_WEIGHT_INVALID);
930 BUS_DEFINE_SET_CGROUP_LIMIT(memory, CGROUP_MASK_MEMORY, physical_memory_scale, 1);
931 BUS_DEFINE_SET_CGROUP_LIMIT(memory_protection, CGROUP_MASK_MEMORY, physical_memory_scale, 0);
932 BUS_DEFINE_SET_CGROUP_LIMIT(swap, CGROUP_MASK_MEMORY, physical_memory_scale, 0);
933 BUS_DEFINE_SET_CGROUP_LIMIT(zswap, CGROUP_MASK_MEMORY, physical_memory_scale, 0);
934 REENABLE_WARNING;
935
936 static int bus_cgroup_set_cpu_weight(
937 Unit *u,
938 const char *name,
939 uint64_t *p,
940 sd_bus_message *message,
941 UnitWriteFlags flags,
942 sd_bus_error *reterr_error) {
943 uint64_t v;
944 int r;
945 assert(p);
946 r = sd_bus_message_read(message, "t", &v);
947 if (r < 0)
948 return r;
949 if (!CGROUP_WEIGHT_IS_OK(v) && v != CGROUP_WEIGHT_IDLE)
950 return sd_bus_error_setf(
951 reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Value specified in %s is out of range", name);
952 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
953 *p = v;
954 unit_invalidate_cgroup(u, CGROUP_MASK_CPU);
955 if (v == CGROUP_WEIGHT_INVALID)
956 unit_write_settingf(u, flags, name, "%s=", name);
957 else if (v == CGROUP_WEIGHT_IDLE)
958 unit_write_settingf(u, flags, name, "%s=idle", name);
959 else
960 unit_write_settingf(u, flags, name, "%s=%" PRIu64, name, v);
961 }
962 return 1;
963 }
964
965 static int bus_cgroup_set_tasks_max(
966 Unit *u,
967 const char *name,
968 CGroupTasksMax *p,
969 sd_bus_message *message,
970 UnitWriteFlags flags,
971 sd_bus_error *reterr_error) {
972
973 uint64_t v;
974 int r;
975
976 assert(p);
977
978 r = sd_bus_message_read(message, "t", &v);
979 if (r < 0)
980 return r;
981
982 if (v < 1)
983 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
984 "Value specified in %s is out of range", name);
985
986 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
987 *p = (CGroupTasksMax) { .value = v, .scale = 0 }; /* When .scale==0, .value is the absolute value */
988 unit_invalidate_cgroup(u, CGROUP_MASK_PIDS);
989
990 if (v == CGROUP_LIMIT_MAX)
991 unit_write_settingf(u, flags, name,
992 "%s=infinity", name);
993 else
994 unit_write_settingf(u, flags, name,
995 "%s=%" PRIu64, name, v);
996 }
997
998 return 1;
999 }
1000
1001 static int bus_cgroup_set_tasks_max_scale(
1002 Unit *u,
1003 const char *name,
1004 CGroupTasksMax *p,
1005 sd_bus_message *message,
1006 UnitWriteFlags flags,
1007 sd_bus_error *reterr_error) {
1008
1009 uint32_t v;
1010 int r;
1011
1012 assert(p);
1013
1014 r = sd_bus_message_read(message, "u", &v);
1015 if (r < 0)
1016 return r;
1017
1018 if (v < 1 || v >= UINT32_MAX)
1019 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
1020 "Value specified in %s is out of range", name);
1021
1022 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1023 *p = (CGroupTasksMax) { v, UINT32_MAX }; /* .scale is not 0, so this is interpreted as v/UINT32_MAX. */
1024 unit_invalidate_cgroup(u, CGROUP_MASK_PIDS);
1025
1026 int scaled = UINT32_SCALE_TO_PERMYRIAD(v);
1027 unit_write_settingf(u, flags, name, "TasksMax=" PERMYRIAD_AS_PERCENT_FORMAT_STR,
1028 PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled));
1029 }
1030
1031 return 1;
1032 }
1033
1034 int bus_cgroup_set_property(
1035 Unit *u,
1036 CGroupContext *c,
1037 const char *name,
1038 sd_bus_message *message,
1039 UnitWriteFlags flags,
1040 sd_bus_error *reterr_error) {
1041
1042 CGroupIOLimitType iol_type;
1043 int r;
1044
1045 assert(u);
1046 assert(c);
1047 assert(name);
1048 assert(message);
1049
1050 flags |= UNIT_PRIVATE;
1051
1052 if (streq(name, "CPUWeight"))
1053 return bus_cgroup_set_cpu_weight(u, name, &c->cpu_weight, message, flags, reterr_error);
1054
1055 if (streq(name, "StartupCPUWeight"))
1056 return bus_cgroup_set_cpu_weight(u, name, &c->startup_cpu_weight, message, flags, reterr_error);
1057
1058 if (streq(name, "IOAccounting"))
1059 return bus_cgroup_set_boolean(u, name, &c->io_accounting, CGROUP_MASK_IO, message, flags, reterr_error);
1060
1061 if (streq(name, "IOWeight"))
1062 return bus_cgroup_set_io_weight(u, name, &c->io_weight, message, flags, reterr_error);
1063
1064 if (streq(name, "StartupIOWeight"))
1065 return bus_cgroup_set_io_weight(u, name, &c->startup_io_weight, message, flags, reterr_error);
1066
1067 if (streq(name, "MemoryAccounting"))
1068 return bus_cgroup_set_boolean(u, name, &c->memory_accounting, CGROUP_MASK_MEMORY, message, flags, reterr_error);
1069
1070 if (streq(name, "MemoryMin"))
1071 return bus_cgroup_set_memory_protection(u, name, &c->memory_min, message, flags, reterr_error);
1072
1073 if (streq(name, "MemoryLow"))
1074 return bus_cgroup_set_memory_protection(u, name, &c->memory_low, message, flags, reterr_error);
1075
1076 if (streq(name, "StartupMemoryLow")) {
1077 r = bus_cgroup_set_memory_protection(u, name, &c->startup_memory_low, message, flags, reterr_error);
1078 if (r > 0)
1079 c->startup_memory_low_set = true;
1080 return r;
1081 }
1082
1083 if (streq(name, "MemoryHigh"))
1084 return bus_cgroup_set_memory(u, name, &c->memory_high, message, flags, reterr_error);
1085
1086 if (streq(name, "StartupMemoryHigh")) {
1087 r = bus_cgroup_set_memory(u, name, &c->startup_memory_high, message, flags, reterr_error);
1088 if (r > 0)
1089 c->startup_memory_high_set = true;
1090 return r;
1091 }
1092
1093 if (streq(name, "MemorySwapMax"))
1094 return bus_cgroup_set_swap(u, name, &c->memory_swap_max, message, flags, reterr_error);
1095
1096 if (streq(name, "StartupMemorySwapMax")) {
1097 r = bus_cgroup_set_swap(u, name, &c->startup_memory_swap_max, message, flags, reterr_error);
1098 if (r > 0)
1099 c->startup_memory_swap_max_set = true;
1100 return r;
1101 }
1102
1103 if (streq(name, "MemoryZSwapMax"))
1104 return bus_cgroup_set_zswap(u, name, &c->memory_zswap_max, message, flags, reterr_error);
1105
1106 if (streq(name, "StartupMemoryZSwapMax")) {
1107 r = bus_cgroup_set_zswap(u, name, &c->startup_memory_zswap_max, message, flags, reterr_error);
1108 if (r > 0)
1109 c->startup_memory_zswap_max_set = true;
1110 return r;
1111 }
1112
1113 if (streq(name, "MemoryMax"))
1114 return bus_cgroup_set_memory(u, name, &c->memory_max, message, flags, reterr_error);
1115
1116 if (streq(name, "StartupMemoryMax")) {
1117 r = bus_cgroup_set_memory(u, name, &c->startup_memory_max, message, flags, reterr_error);
1118 if (r > 0)
1119 c->startup_memory_max_set = true;
1120 return r;
1121 }
1122
1123 if (streq(name, "MemoryMinScale"))
1124 return bus_cgroup_set_memory_protection_scale(u, name, &c->memory_min, message, flags, reterr_error);
1125
1126 if (streq(name, "MemoryLowScale"))
1127 return bus_cgroup_set_memory_protection_scale(u, name, &c->memory_low, message, flags, reterr_error);
1128
1129 if (streq(name, "MemoryHighScale"))
1130 return bus_cgroup_set_memory_scale(u, name, &c->memory_high, message, flags, reterr_error);
1131
1132 if (streq(name, "MemorySwapMaxScale"))
1133 return bus_cgroup_set_swap_scale(u, name, &c->memory_swap_max, message, flags, reterr_error);
1134
1135 if (streq(name, "MemoryZSwapMaxScale"))
1136 return bus_cgroup_set_zswap_scale(u, name, &c->memory_zswap_max, message, flags, reterr_error);
1137
1138 if (streq(name, "MemoryMaxScale"))
1139 return bus_cgroup_set_memory_scale(u, name, &c->memory_max, message, flags, reterr_error);
1140
1141 if (streq(name, "MemoryZSwapWriteback"))
1142 return bus_cgroup_set_boolean(u, name, &c->memory_zswap_writeback, CGROUP_MASK_MEMORY, message, flags, reterr_error);
1143
1144 if (streq(name, "TasksAccounting"))
1145 return bus_cgroup_set_boolean(u, name, &c->tasks_accounting, CGROUP_MASK_PIDS, message, flags, reterr_error);
1146
1147 if (streq(name, "TasksMax"))
1148 return bus_cgroup_set_tasks_max(u, name, &c->tasks_max, message, flags, reterr_error);
1149
1150 if (streq(name, "TasksMaxScale"))
1151 return bus_cgroup_set_tasks_max_scale(u, name, &c->tasks_max, message, flags, reterr_error);
1152
1153 if (streq(name, "CPUQuotaPerSecUSec")) {
1154 uint64_t u64;
1155
1156 r = sd_bus_message_read(message, "t", &u64);
1157 if (r < 0)
1158 return r;
1159
1160 if (u64 <= 0)
1161 return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "CPUQuotaPerSecUSec= value out of range");
1162
1163 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1164 c->cpu_quota_per_sec_usec = u64;
1165 CGroupRuntime *crt = unit_get_cgroup_runtime(u);
1166 if (crt)
1167 crt->warned_clamping_cpu_quota_period = false;
1168 unit_invalidate_cgroup(u, CGROUP_MASK_CPU);
1169
1170 if (c->cpu_quota_per_sec_usec == USEC_INFINITY)
1171 unit_write_setting(u, flags, "CPUQuota", "CPUQuota=");
1172 else
1173 unit_write_settingf(u, flags, "CPUQuota",
1174 "CPUQuota=" USEC_FMT ".%02" PRI_USEC "%%",
1175 c->cpu_quota_per_sec_usec / 10000,
1176 (c->cpu_quota_per_sec_usec % 10000) / 100);
1177 }
1178
1179 return 1;
1180
1181 } else if (streq(name, "CPUQuotaPeriodUSec")) {
1182 uint64_t u64;
1183
1184 r = sd_bus_message_read(message, "t", &u64);
1185 if (r < 0)
1186 return r;
1187
1188 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1189 c->cpu_quota_period_usec = u64;
1190 CGroupRuntime *crt = unit_get_cgroup_runtime(u);
1191 if (crt)
1192 crt->warned_clamping_cpu_quota_period = false;
1193 unit_invalidate_cgroup(u, CGROUP_MASK_CPU);
1194 if (c->cpu_quota_period_usec == USEC_INFINITY)
1195 unit_write_setting(u, flags, "CPUQuotaPeriodSec", "CPUQuotaPeriodSec=");
1196 else
1197 unit_write_settingf(u, flags, "CPUQuotaPeriodSec",
1198 "CPUQuotaPeriodSec=%s",
1199 FORMAT_TIMESPAN(c->cpu_quota_period_usec, 1));
1200 }
1201
1202 return 1;
1203
1204 } else if (STR_IN_SET(name, "AllowedCPUs", "StartupAllowedCPUs", "AllowedMemoryNodes", "StartupAllowedMemoryNodes")) {
1205 const void *a;
1206 size_t n;
1207 _cleanup_(cpu_set_done) CPUSet new_set = {};
1208
1209 r = sd_bus_message_read_array(message, 'y', &a, &n);
1210 if (r < 0)
1211 return r;
1212
1213 r = cpu_set_from_dbus(a, n, &new_set);
1214 if (r < 0)
1215 return r;
1216
1217 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1218 _cleanup_free_ char *setstr = NULL;
1219 CPUSet *set = NULL;
1220
1221 setstr = cpu_set_to_range_string(&new_set);
1222 if (!setstr)
1223 return -ENOMEM;
1224
1225 if (streq(name, "AllowedCPUs"))
1226 set = &c->cpuset_cpus;
1227 else if (streq(name, "StartupAllowedCPUs"))
1228 set = &c->startup_cpuset_cpus;
1229 else if (streq(name, "AllowedMemoryNodes"))
1230 set = &c->cpuset_mems;
1231 else if (streq(name, "StartupAllowedMemoryNodes"))
1232 set = &c->startup_cpuset_mems;
1233 else
1234 assert_not_reached();
1235
1236 cpu_set_done_and_replace(*set, new_set);
1237
1238 unit_invalidate_cgroup(u, CGROUP_MASK_CPUSET);
1239 unit_write_settingf(u, flags, name, "%s=\n%s=%s", name, name, setstr);
1240 }
1241
1242 return 1;
1243
1244 } else if ((iol_type = cgroup_io_limit_type_from_string(name)) >= 0) {
1245 const char *path;
1246 unsigned n = 0;
1247 uint64_t u64;
1248
1249 r = sd_bus_message_enter_container(message, 'a', "(st)");
1250 if (r < 0)
1251 return r;
1252
1253 while ((r = sd_bus_message_read(message, "(st)", &path, &u64)) > 0) {
1254
1255 if (!path_is_normalized(path))
1256 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
1257
1258 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1259 CGroupIODeviceLimit *a = NULL;
1260
1261 LIST_FOREACH(device_limits, b, c->io_device_limits)
1262 if (path_equal(path, b->path)) {
1263 a = b;
1264 break;
1265 }
1266
1267 if (!a) {
1268 CGroupIOLimitType type;
1269
1270 a = new0(CGroupIODeviceLimit, 1);
1271 if (!a)
1272 return -ENOMEM;
1273
1274 a->path = strdup(path);
1275 if (!a->path) {
1276 free(a);
1277 return -ENOMEM;
1278 }
1279
1280 for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++)
1281 a->limits[type] = cgroup_io_limit_defaults[type];
1282
1283 LIST_APPEND(device_limits, c->io_device_limits, a);
1284 }
1285
1286 a->limits[iol_type] = u64;
1287 }
1288
1289 n++;
1290 }
1291 if (r < 0)
1292 return r;
1293
1294 r = sd_bus_message_exit_container(message);
1295 if (r < 0)
1296 return r;
1297
1298 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1299 _cleanup_(memstream_done) MemStream m = {};
1300 _cleanup_free_ char *buf = NULL;
1301 FILE *f;
1302
1303 if (n == 0)
1304 LIST_FOREACH(device_limits, a, c->io_device_limits)
1305 a->limits[iol_type] = cgroup_io_limit_defaults[iol_type];
1306
1307 unit_invalidate_cgroup(u, CGROUP_MASK_IO);
1308
1309 f = memstream_init(&m);
1310 if (!f)
1311 return -ENOMEM;
1312
1313 fprintf(f, "%s=\n", name);
1314 LIST_FOREACH(device_limits, a, c->io_device_limits)
1315 if (a->limits[iol_type] != cgroup_io_limit_defaults[iol_type])
1316 fprintf(f, "%s=%s %" PRIu64 "\n", name, a->path, a->limits[iol_type]);
1317
1318 r = memstream_finalize(&m, &buf, NULL);
1319 if (r < 0)
1320 return r;
1321
1322 unit_write_setting(u, flags, name, buf);
1323 }
1324
1325 return 1;
1326
1327 } else if (streq(name, "IODeviceWeight")) {
1328 const char *path;
1329 uint64_t weight;
1330 unsigned n = 0;
1331
1332 r = sd_bus_message_enter_container(message, 'a', "(st)");
1333 if (r < 0)
1334 return r;
1335
1336 while ((r = sd_bus_message_read(message, "(st)", &path, &weight)) > 0) {
1337
1338 if (!path_is_normalized(path))
1339 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
1340
1341 if (!CGROUP_WEIGHT_IS_OK(weight) || weight == CGROUP_WEIGHT_INVALID)
1342 return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "IODeviceWeight= value out of range");
1343
1344 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1345 CGroupIODeviceWeight *a = NULL;
1346
1347 LIST_FOREACH(device_weights, b, c->io_device_weights)
1348 if (path_equal(b->path, path)) {
1349 a = b;
1350 break;
1351 }
1352
1353 if (!a) {
1354 a = new0(CGroupIODeviceWeight, 1);
1355 if (!a)
1356 return -ENOMEM;
1357
1358 a->path = strdup(path);
1359 if (!a->path) {
1360 free(a);
1361 return -ENOMEM;
1362 }
1363 LIST_APPEND(device_weights, c->io_device_weights, a);
1364 }
1365
1366 a->weight = weight;
1367 }
1368
1369 n++;
1370 }
1371
1372 r = sd_bus_message_exit_container(message);
1373 if (r < 0)
1374 return r;
1375
1376 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1377 _cleanup_(memstream_done) MemStream m = {};
1378 _cleanup_free_ char *buf = NULL;
1379 FILE *f;
1380
1381 if (n == 0)
1382 while (c->io_device_weights)
1383 cgroup_context_free_io_device_weight(c, c->io_device_weights);
1384
1385 unit_invalidate_cgroup(u, CGROUP_MASK_IO);
1386
1387 f = memstream_init(&m);
1388 if (!f)
1389 return -ENOMEM;
1390
1391 fputs("IODeviceWeight=\n", f);
1392 LIST_FOREACH(device_weights, a, c->io_device_weights)
1393 fprintf(f, "IODeviceWeight=%s %" PRIu64 "\n", a->path, a->weight);
1394
1395 r = memstream_finalize(&m, &buf, NULL);
1396 if (r < 0)
1397 return r;
1398
1399 unit_write_setting(u, flags, name, buf);
1400 }
1401
1402 return 1;
1403
1404 } else if (streq(name, "IODeviceLatencyTargetUSec")) {
1405 const char *path;
1406 uint64_t target;
1407 unsigned n = 0;
1408
1409 r = sd_bus_message_enter_container(message, 'a', "(st)");
1410 if (r < 0)
1411 return r;
1412
1413 while ((r = sd_bus_message_read(message, "(st)", &path, &target)) > 0) {
1414
1415 if (!path_is_normalized(path))
1416 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Path '%s' specified in %s= is not normalized.", name, path);
1417
1418 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1419 CGroupIODeviceLatency *a = NULL;
1420
1421 LIST_FOREACH(device_latencies, b, c->io_device_latencies)
1422 if (path_equal(b->path, path)) {
1423 a = b;
1424 break;
1425 }
1426
1427 if (!a) {
1428 a = new0(CGroupIODeviceLatency, 1);
1429 if (!a)
1430 return -ENOMEM;
1431
1432 a->path = strdup(path);
1433 if (!a->path) {
1434 free(a);
1435 return -ENOMEM;
1436 }
1437 LIST_APPEND(device_latencies, c->io_device_latencies, a);
1438 }
1439
1440 a->target_usec = target;
1441 }
1442
1443 n++;
1444 }
1445
1446 r = sd_bus_message_exit_container(message);
1447 if (r < 0)
1448 return r;
1449
1450 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1451 _cleanup_(memstream_done) MemStream m = {};
1452 _cleanup_free_ char *buf = NULL;
1453 FILE *f;
1454
1455 if (n == 0)
1456 while (c->io_device_latencies)
1457 cgroup_context_free_io_device_latency(c, c->io_device_latencies);
1458
1459 unit_invalidate_cgroup(u, CGROUP_MASK_IO);
1460
1461 f = memstream_init(&m);
1462 if (!f)
1463 return -ENOMEM;
1464
1465 fputs("IODeviceLatencyTargetSec=\n", f);
1466 LIST_FOREACH(device_latencies, a, c->io_device_latencies)
1467 fprintf(f, "IODeviceLatencyTargetSec=%s %s\n",
1468 a->path, FORMAT_TIMESPAN(a->target_usec, 1));
1469
1470 r = memstream_finalize(&m, &buf, NULL);
1471 if (r < 0)
1472 return r;
1473
1474 unit_write_setting(u, flags, name, buf);
1475 }
1476
1477 return 1;
1478
1479 } else if (streq(name, "DevicePolicy")) {
1480 const char *policy;
1481 CGroupDevicePolicy p;
1482
1483 r = sd_bus_message_read(message, "s", &policy);
1484 if (r < 0)
1485 return r;
1486
1487 p = cgroup_device_policy_from_string(policy);
1488 if (p < 0)
1489 return p;
1490
1491 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1492 c->device_policy = p;
1493 unit_invalidate_cgroup(u, CGROUP_MASK_BPF_DEVICES);
1494 unit_write_settingf(u, flags, name, "DevicePolicy=%s", policy);
1495 }
1496
1497 return 1;
1498
1499 } else if (streq(name, "CPUSetPartition")) {
1500 const char *partition_str;
1501 CPUSetPartition p;
1502
1503 r = sd_bus_message_read(message, "s", &partition_str);
1504 if (r < 0)
1505 return r;
1506
1507 if (isempty(partition_str))
1508 p = _CPUSET_PARTITION_INVALID;
1509 else {
1510 p = cpuset_partition_from_string(partition_str);
1511 if (p < 0)
1512 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
1513 "Invalid CPUSetPartition value: %s", partition_str);
1514 }
1515
1516 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1517 c->cpuset_partition = p;
1518 unit_invalidate_cgroup(u, CGROUP_MASK_CPUSET);
1519
1520 if (p == _CPUSET_PARTITION_INVALID)
1521 unit_write_settingf(u, flags, name, "%s=", name);
1522 else
1523 unit_write_settingf(u, flags, name, "%s=%s", name, partition_str);
1524 }
1525
1526 return 1;
1527
1528 } else if (streq(name, "DeviceAllow")) {
1529 const char *path, *rwm;
1530 unsigned n = 0;
1531
1532 r = sd_bus_message_enter_container(message, 'a', "(ss)");
1533 if (r < 0)
1534 return r;
1535
1536 while ((r = sd_bus_message_read(message, "(ss)", &path, &rwm)) > 0) {
1537 CGroupDevicePermissions p;
1538
1539 if (!valid_device_allow_pattern(path) || strpbrk(path, WHITESPACE))
1540 return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "DeviceAllow= requires device node or pattern");
1541
1542 if (isempty(rwm))
1543 p = _CGROUP_DEVICE_PERMISSIONS_ALL;
1544 else {
1545 p = cgroup_device_permissions_from_string(rwm);
1546 if (p < 0)
1547 return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "DeviceAllow= requires combination of rwm flags");
1548 }
1549
1550 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1551 r = cgroup_context_add_or_update_device_allow(c, path, p);
1552 if (r < 0)
1553 return r;
1554 }
1555
1556 n++;
1557 }
1558 if (r < 0)
1559 return r;
1560
1561 r = sd_bus_message_exit_container(message);
1562 if (r < 0)
1563 return r;
1564
1565 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1566 _cleanup_(memstream_done) MemStream m = {};
1567 _cleanup_free_ char *buf = NULL;
1568 FILE *f;
1569
1570 if (n == 0)
1571 while (c->device_allow)
1572 cgroup_context_free_device_allow(c, c->device_allow);
1573
1574 unit_invalidate_cgroup(u, CGROUP_MASK_BPF_DEVICES);
1575
1576 f = memstream_init(&m);
1577 if (!f)
1578 return -ENOMEM;
1579
1580 fputs("DeviceAllow=\n", f);
1581 LIST_FOREACH(device_allow, a, c->device_allow)
1582 fprintf(f, "DeviceAllow=%s %s\n", a->path, cgroup_device_permissions_to_string(a->permissions));
1583
1584 r = memstream_finalize(&m, &buf, NULL);
1585 if (r < 0)
1586 return r;
1587
1588 unit_write_setting(u, flags, name, buf);
1589 }
1590
1591 return 1;
1592
1593 } else if (streq(name, "IPAccounting")) {
1594 int b;
1595
1596 r = sd_bus_message_read(message, "b", &b);
1597 if (r < 0)
1598 return r;
1599
1600 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1601 c->ip_accounting = b;
1602
1603 unit_invalidate_cgroup_bpf_firewall(u);
1604 unit_write_settingf(u, flags, name, "IPAccounting=%s", yes_no(b));
1605 }
1606
1607 return 1;
1608
1609 } else if (STR_IN_SET(name, "IPAddressAllow", "IPAddressDeny")) {
1610 _cleanup_set_free_ Set *new_prefixes = NULL;
1611 size_t n = 0;
1612
1613 r = sd_bus_message_enter_container(message, 'a', "(iayu)");
1614 if (r < 0)
1615 return r;
1616
1617 for (;;) {
1618 r = sd_bus_message_enter_container(message, 'r', "iayu");
1619 if (r < 0)
1620 return r;
1621 if (r == 0)
1622 break;
1623
1624 struct in_addr_prefix prefix;
1625 r = bus_message_read_in_addr_auto(message, reterr_error, &prefix.family, &prefix.address);
1626 if (r < 0)
1627 return r;
1628
1629 uint32_t prefixlen;
1630 r = sd_bus_message_read(message, "u", &prefixlen);
1631 if (r < 0)
1632 return r;
1633
1634 if (prefixlen > FAMILY_ADDRESS_SIZE(prefix.family)*8)
1635 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
1636 "Prefix length %" PRIu32 " too large for address family %s.",
1637 prefixlen, af_to_name(prefix.family));
1638
1639 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1640 prefix.prefixlen = (uint8_t) prefixlen;
1641
1642 r = in_addr_prefix_add(&new_prefixes, &prefix);
1643 if (r < 0)
1644 return r;
1645 }
1646
1647 r = sd_bus_message_exit_container(message);
1648 if (r < 0)
1649 return r;
1650
1651 n++;
1652 }
1653
1654 r = sd_bus_message_exit_container(message);
1655 if (r < 0)
1656 return r;
1657
1658 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1659 _cleanup_(memstream_done) MemStream m = {};
1660 _cleanup_free_ char *buf = NULL;
1661 Set **prefixes;
1662 bool *reduced;
1663 FILE *f;
1664
1665 unit_invalidate_cgroup_bpf_firewall(u);
1666
1667 f = memstream_init(&m);
1668 if (!f)
1669 return -ENOMEM;
1670
1671 prefixes = streq(name, "IPAddressAllow") ? &c->ip_address_allow : &c->ip_address_deny;
1672 reduced = streq(name, "IPAddressAllow") ? &c->ip_address_allow_reduced : &c->ip_address_deny_reduced;
1673
1674 fputs(name, f);
1675 fputs("=\n", f);
1676
1677 if (n == 0) {
1678 *reduced = true;
1679 *prefixes = set_free(*prefixes);
1680 } else {
1681 *reduced = false;
1682
1683 r = in_addr_prefixes_merge(prefixes, new_prefixes);
1684 if (r < 0)
1685 return r;
1686
1687 const struct in_addr_prefix *p;
1688 SET_FOREACH(p, *prefixes)
1689 fprintf(f, "%s=%s\n", name,
1690 IN_ADDR_PREFIX_TO_STRING(p->family, &p->address, p->prefixlen));
1691 }
1692
1693 r = memstream_finalize(&m, &buf, NULL);
1694 if (r < 0)
1695 return r;
1696
1697 unit_write_setting(u, flags, name, buf);
1698 }
1699
1700 return 1;
1701 }
1702
1703 if (STR_IN_SET(name, "ManagedOOMSwap", "ManagedOOMMemoryPressure")) {
1704 ManagedOOMMode *cgroup_mode = streq(name, "ManagedOOMSwap") ? &c->moom_swap : &c->moom_mem_pressure;
1705 ManagedOOMMode m;
1706 const char *mode;
1707
1708 if (!UNIT_VTABLE(u)->can_set_managed_oom)
1709 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set %s for this unit type", name);
1710
1711 r = sd_bus_message_read(message, "s", &mode);
1712 if (r < 0)
1713 return r;
1714
1715 m = managed_oom_mode_from_string(mode);
1716 if (m < 0)
1717 return -EINVAL;
1718
1719 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1720 *cgroup_mode = m;
1721 unit_write_settingf(u, flags, name, "%s=%s", name, mode);
1722 }
1723
1724 (void) manager_varlink_send_managed_oom_update(u);
1725 return 1;
1726 }
1727
1728 if (streq(name, "ManagedOOMMemoryPressureLimit")) {
1729 uint32_t v;
1730
1731 if (!UNIT_VTABLE(u)->can_set_managed_oom)
1732 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set %s for this unit type", name);
1733
1734 r = sd_bus_message_read(message, "u", &v);
1735 if (r < 0)
1736 return r;
1737
1738 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1739 c->moom_mem_pressure_limit = v;
1740 unit_write_settingf(u, flags, name,
1741 "ManagedOOMMemoryPressureLimit=" PERMYRIAD_AS_PERCENT_FORMAT_STR,
1742 PERMYRIAD_AS_PERCENT_FORMAT_VAL(UINT32_SCALE_TO_PERMYRIAD(v)));
1743 }
1744
1745 if (c->moom_mem_pressure == MANAGED_OOM_KILL)
1746 (void) manager_varlink_send_managed_oom_update(u);
1747
1748 return 1;
1749 }
1750
1751 if (streq(name, "ManagedOOMMemoryPressureDurationUSec")) {
1752 uint64_t t;
1753
1754 if (!UNIT_VTABLE(u)->can_set_managed_oom)
1755 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set %s for this unit type", name);
1756
1757 r = sd_bus_message_read(message, "t", &t);
1758 if (r < 0)
1759 return r;
1760
1761 if (t < 1 * USEC_PER_SEC)
1762 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "%s= must be at least 1s, got %s", name,
1763 FORMAT_TIMESPAN(t, USEC_PER_SEC));
1764
1765 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1766 c->moom_mem_pressure_duration_usec = t;
1767 if (c->moom_mem_pressure_duration_usec == USEC_INFINITY)
1768 unit_write_setting(u, flags, name, "ManagedOOMMemoryPressureDurationSec=");
1769 else
1770 unit_write_settingf(u, flags, name,
1771 "ManagedOOMMemoryPressureDurationSec=%s",
1772 FORMAT_TIMESPAN(c->moom_mem_pressure_duration_usec, 1));
1773 }
1774
1775 if (c->moom_mem_pressure == MANAGED_OOM_KILL)
1776 (void) manager_varlink_send_managed_oom_update(u);
1777
1778 return 1;
1779 }
1780
1781 if (streq(name, "ManagedOOMPreference")) {
1782 ManagedOOMPreference p;
1783 const char *pref;
1784
1785 r = sd_bus_message_read(message, "s", &pref);
1786 if (r < 0)
1787 return r;
1788
1789 p = managed_oom_preference_from_string(pref);
1790 if (p < 0)
1791 return p;
1792
1793 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1794 c->moom_preference = p;
1795 unit_write_settingf(u, flags, name, "ManagedOOMPreference=%s", pref);
1796 }
1797
1798 return 1;
1799 }
1800
1801 if (streq(name, "OOMRules")) {
1802 _cleanup_strv_free_ char **oom_rules = NULL;
1803
1804 if (!UNIT_VTABLE(u)->can_set_managed_oom)
1805 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set %s for this unit type", name);
1806
1807 r = sd_bus_message_read_strv(message, &oom_rules);
1808 if (r < 0)
1809 return r;
1810
1811 STRV_FOREACH(rule, oom_rules)
1812 if (!string_is_safe(*rule, STRING_FILENAME))
1813 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid rule name: %s", *rule);
1814
1815 strv_uniq(oom_rules);
1816
1817 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1818 _cleanup_free_ char *joined = strv_join(oom_rules, " ");
1819 if (!joined)
1820 return -ENOMEM;
1821
1822 strv_free_and_replace(c->moom_rules, oom_rules);
1823
1824 unit_write_settingf(u, flags, name, "OOMRules=\nOOMRules=%s", joined);
1825
1826 (void) manager_varlink_send_managed_oom_update(u);
1827 }
1828
1829 return 1;
1830 }
1831
1832 if (STR_IN_SET(name, "SocketBindAllow", "SocketBindDeny")) {
1833 CGroupSocketBindItem **list;
1834 uint16_t nr_ports, port_min;
1835 size_t n = 0;
1836 int32_t family, ip_protocol;
1837
1838 list = streq(name, "SocketBindAllow") ? &c->socket_bind_allow : &c->socket_bind_deny;
1839
1840 r = sd_bus_message_enter_container(message, 'a', "(iiqq)");
1841 if (r < 0)
1842 return r;
1843
1844 while ((r = sd_bus_message_read(message, "(iiqq)", &family, &ip_protocol, &nr_ports, &port_min)) > 0) {
1845
1846 if (!IN_SET(family, AF_UNSPEC, AF_INET, AF_INET6))
1847 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "%s= expects INET or INET6 family, if specified.", name);
1848
1849 if (!IN_SET(ip_protocol, 0, IPPROTO_TCP, IPPROTO_UDP))
1850 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "%s= expects TCP or UDP protocol, if specified.", name);
1851
1852 if (port_min + (uint32_t) nr_ports > (1 << 16))
1853 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "%s= expects maximum port value lesser than 65536.", name);
1854
1855 if (port_min == 0 && nr_ports != 0)
1856 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "%s= expects port range starting with positive value.", name);
1857
1858 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1859 _cleanup_free_ CGroupSocketBindItem *item = NULL;
1860
1861 item = new(CGroupSocketBindItem, 1);
1862 if (!item)
1863 return log_oom();
1864
1865 *item = (CGroupSocketBindItem) {
1866 .address_family = family,
1867 .ip_protocol = ip_protocol,
1868 .nr_ports = nr_ports,
1869 .port_min = port_min
1870 };
1871
1872 LIST_PREPEND(socket_bind_items, *list, TAKE_PTR(item));
1873 }
1874 n++;
1875 }
1876 if (r < 0)
1877 return r;
1878
1879 r = sd_bus_message_exit_container(message);
1880 if (r < 0)
1881 return r;
1882
1883 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1884 _cleanup_(memstream_done) MemStream m = {};
1885 _cleanup_free_ char *buf = NULL;
1886 FILE *f;
1887
1888 if (n == 0)
1889 cgroup_context_remove_socket_bind(list);
1890 else {
1891 if ((u->manager->cgroup_supported & CGROUP_MASK_BPF_SOCKET_BIND) == 0)
1892 log_full(LOG_DEBUG,
1893 "Unit %s configures source compiled BPF programs "
1894 "but the local system does not support that.\n"
1895 "Starting this unit will fail!", u->id);
1896 }
1897
1898 f = memstream_init(&m);
1899 if (!f)
1900 return -ENOMEM;
1901
1902 if (n == 0)
1903 fprintf(f, "%s=\n", name);
1904 else
1905 LIST_FOREACH(socket_bind_items, item, *list) {
1906 fprintf(f, "%s=", name);
1907 cgroup_context_dump_socket_bind_item(item, f);
1908 fputc('\n', f);
1909 }
1910
1911 r = memstream_finalize(&m, &buf, NULL);
1912 if (r < 0)
1913 return r;
1914
1915 unit_write_setting(u, flags, name, buf);
1916 }
1917
1918 return 1;
1919 }
1920
1921 if (streq(name, "RestrictNetworkInterfaces")) {
1922 int is_allow_list;
1923 _cleanup_strv_free_ char **l = NULL;
1924
1925 r = sd_bus_message_enter_container(message, 'r', "bas");
1926 if (r < 0)
1927 return r;
1928
1929 r = sd_bus_message_read(message, "b", &is_allow_list);
1930 if (r < 0)
1931 return r;
1932
1933 r = sd_bus_message_read_strv(message, &l);
1934 if (r < 0)
1935 return r;
1936
1937 r = sd_bus_message_exit_container(message);
1938 if (r < 0)
1939 return r;
1940
1941 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1942 _cleanup_free_ char *joined = NULL;
1943
1944 if (strv_isempty(l)) {
1945 c->restrict_network_interfaces_is_allow_list = false;
1946 c->restrict_network_interfaces = set_free(c->restrict_network_interfaces);
1947
1948 unit_write_settingf(u, flags, name, "%s=", name);
1949 return 1;
1950 }
1951
1952 if (set_isempty(c->restrict_network_interfaces))
1953 c->restrict_network_interfaces_is_allow_list = is_allow_list;
1954
1955 STRV_FOREACH(s, l) {
1956 if (!ifname_valid_full(*s, IFNAME_VALID_ALTERNATIVE)) {
1957 log_full(LOG_WARNING, "Invalid interface name, ignoring: %s", *s);
1958 continue;
1959 }
1960 if (c->restrict_network_interfaces_is_allow_list != (bool) is_allow_list)
1961 free(set_remove(c->restrict_network_interfaces, *s));
1962 else {
1963 r = set_put_strdup(&c->restrict_network_interfaces, *s);
1964 if (r < 0)
1965 return log_oom();
1966 }
1967 }
1968
1969 joined = strv_join(l, " ");
1970 if (!joined)
1971 return -ENOMEM;
1972
1973 unit_write_settingf(u, flags, name, "%s=%s%s", name, is_allow_list ? "" : "~", joined);
1974 }
1975
1976 return 1;
1977 }
1978
1979 if (streq(name, "BindNetworkInterface")) {
1980 const char *s;
1981
1982 r = sd_bus_message_read(message, "s", &s);
1983 if (r < 0)
1984 return r;
1985
1986 if (!isempty(s) && !ifname_valid_full(s, IFNAME_VALID_ALTERNATIVE))
1987 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface name: %s", s);
1988
1989 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1990 r = free_and_strdup_warn(&c->bind_network_interface, empty_to_null(s));
1991 if (r < 0)
1992 return r;
1993 if (r > 0)
1994 unit_write_settingf(u, flags, name, "BindNetworkInterface=%s", s);
1995 }
1996
1997 return 1;
1998 }
1999
2000 if (streq(name, "NFTSet")) {
2001 int source, nfproto;
2002 const char *table, *set;
2003 bool empty = true;
2004
2005 r = sd_bus_message_enter_container(message, 'a', "(iiss)");
2006 if (r < 0)
2007 return r;
2008
2009 while ((r = sd_bus_message_read(message, "(iiss)", &source, &nfproto, &table, &set)) > 0) {
2010 const char *source_name, *nfproto_name;
2011
2012 if (!IN_SET(source, NFT_SET_SOURCE_CGROUP, NFT_SET_SOURCE_USER, NFT_SET_SOURCE_GROUP))
2013 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid source %d.", source);
2014
2015 source_name = nft_set_source_to_string(source);
2016 assert(source_name);
2017
2018 nfproto_name = nfproto_to_string(nfproto);
2019 if (!nfproto_name)
2020 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid protocol %d.", nfproto);
2021
2022 if (!nft_identifier_valid(table)) {
2023 _cleanup_free_ char *esc = NULL;
2024
2025 esc = cescape(table);
2026 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid NFT table name %s.", strna(esc));
2027 }
2028
2029 if (!nft_identifier_valid(set)) {
2030 _cleanup_free_ char *esc = NULL;
2031
2032 esc = cescape(set);
2033 return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid NFT set name %s.", strna(esc));
2034 }
2035
2036 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
2037 r = nft_set_add(&c->nft_set_context, source, nfproto, table, set);
2038 if (r < 0)
2039 return r;
2040
2041 unit_write_settingf(
2042 u, flags|UNIT_ESCAPE_SPECIFIERS, name,
2043 "%s=%s:%s:%s:%s",
2044 name,
2045 source_name,
2046 nfproto_name,
2047 table,
2048 set);
2049 }
2050
2051 empty = false;
2052 }
2053 if (r < 0)
2054 return r;
2055
2056 r = sd_bus_message_exit_container(message);
2057 if (r < 0)
2058 return r;
2059
2060 if (empty && !UNIT_WRITE_FLAGS_NOOP(flags)) {
2061 nft_set_context_clear(&c->nft_set_context);
2062 unit_write_settingf(u, flags, name, "%s=", name);
2063 }
2064
2065 return 1;
2066 }
2067
2068 if (STR_IN_SET(name,
2069 /* deprecated CGroup v1 properties */
2070 "MemoryLimit",
2071 "MemoryLimitScale",
2072 "CPUShares",
2073 "StartupCPUShares",
2074 "BlockIOAccounting",
2075 "BlockIOWeight",
2076 "StartupBlockIOWeight",
2077 "BlockIODeviceWeight",
2078 "BlockIOReadBandwidth",
2079 "BlockIOWriteBandwidth",
2080 /* see comments in bus_cgroup_vtable */
2081 "CPUAccounting",
2082 "DefaultMemoryMin",
2083 "DefaultMemoryMinScale",
2084 "DefaultMemoryLow",
2085 "DefaultMemoryLowScale",
2086 "DefaultStartupMemoryLow")) {
2087
2088 r = sd_bus_message_skip(message, NULL);
2089 if (r < 0)
2090 return r;
2091
2092 return 1;
2093 }
2094
2095 /* must be last */
2096 if (streq(name, "DisableControllers") || (u->transient && u->load_state == UNIT_STUB))
2097 return bus_cgroup_set_transient_property(u, c, name, message, flags, reterr_error);
2098
2099 return 0;
2100 }