From: Lennart Poettering Date: Thu, 3 May 2018 16:45:39 +0000 (+0200) Subject: rlimit-util: rework rlimit_{from|to}_string() to work without "Limit" prefix X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=54b2f26b4022d7bb19bca1fc0bb192a5f34bcebb;p=elogind.git rlimit-util: rework rlimit_{from|to}_string() to work without "Limit" prefix let's make the call more generic, so that we can also easily use it for parsing "RLIMIT_xyz" style constants. --- diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 9d3bd4a82..4cfe40433 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -1630,36 +1630,40 @@ int bus_property_get_rlimit( void *userdata, sd_bus_error *error) { + const char *is_soft; struct rlimit *rl; uint64_t u; rlim_t x; - const char *is_soft; assert(bus); assert(reply); assert(userdata); is_soft = endswith(property, "Soft"); + rl = *(struct rlimit**) userdata; if (rl) x = is_soft ? rl->rlim_cur : rl->rlim_max; else { struct rlimit buf = {}; + const char *s, *p; int z; - const char *s; + /* Chop off "Soft" suffix */ s = is_soft ? strndupa(property, is_soft - property) : property; - z = rlimit_from_string(strstr(s, "Limit")); + /* Skip over any prefix, such as "Default" */ + assert_se(p = strstr(s, "Limit")); + + z = rlimit_from_string(p + 5); assert(z >= 0); - getrlimit(z, &buf); + (void) getrlimit(z, &buf); x = is_soft ? buf.rlim_cur : buf.rlim_max; } - /* rlim_t might have different sizes, let's map - * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on - * all archs */ + /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all + * archs */ u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x; return sd_bus_message_append(reply, "t", u);