From 94abee6063bcc01474a358b106d9916075f0fd0b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 18 Jun 2025 17:06:12 +0200 Subject: [PATCH] shared/bus-unit-util: define helper function for rlimits Strictly speaking, we call startswith twice now. This duplication shall be eliminated later. --- src/shared/bus-unit-util.c | 52 ++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c index 59b5fe93530..0255f4fb02a 100644 --- a/src/shared/bus-unit-util.c +++ b/src/shared/bus-unit-util.c @@ -1266,6 +1266,31 @@ static int bus_append_standard_input_data(sd_bus_message *m, const char *field, return bus_append_byte_array(m, field, decoded, sz); } +static int bus_append_resource_limit(sd_bus_message *m, const char *field, const char *eq) { + const char *suffix = ASSERT_PTR(startswith(field, "Limit")); + int rl, r; + + rl = rlimit_from_string(suffix); + if (rl < 0) + return log_error_errno(rl, "Unknown setting '%s'.", field); + + struct rlimit l; + r = rlimit_parse(rl, eq, &l); + if (r < 0) + return log_error_errno(r, "Failed to parse resource limit: %s", eq); + + r = sd_bus_message_append(m, "(sv)", field, "t", (uint64_t) l.rlim_max); + if (r < 0) + return bus_log_create_error(r); + + const char *sn = strjoina(field, "Soft"); + r = sd_bus_message_append(m, "(sv)", sn, "t", (uint64_t) l.rlim_cur); + if (r < 0) + return bus_log_create_error(r); + + return 1; +} + static int bus_append_cgroup_property(sd_bus_message *m, const char *field, const char *eq) { if (STR_IN_SET(field, "DevicePolicy", "Slice", @@ -1393,7 +1418,6 @@ static int bus_append_automount_property(sd_bus_message *m, const char *field, c } static int bus_append_execute_property(sd_bus_message *m, const char *field, const char *eq) { - const char *suffix; int r; if (STR_IN_SET(field, "User", @@ -1562,30 +1586,8 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con if (streq(field, "StandardInputData")) return bus_append_standard_input_data(m, field, eq); - if ((suffix = startswith(field, "Limit"))) { - int rl; - - rl = rlimit_from_string(suffix); - if (rl >= 0) { - const char *sn; - struct rlimit l; - - r = rlimit_parse(rl, eq, &l); - if (r < 0) - return log_error_errno(r, "Failed to parse resource limit: %s", eq); - - r = sd_bus_message_append(m, "(sv)", field, "t", (uint64_t) l.rlim_max); - if (r < 0) - return bus_log_create_error(r); - - sn = strjoina(field, "Soft"); - r = sd_bus_message_append(m, "(sv)", sn, "t", (uint64_t) l.rlim_cur); - if (r < 0) - return bus_log_create_error(r); - - return 1; - } - } + if (startswith(field, "Limit")) + return bus_append_resource_limit(m, field, eq); if (STR_IN_SET(field, "AppArmorProfile", "SmackProcessLabel")) { -- 2.47.3