core: split out the helper to serialize/deserialize ratelimits

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-11-17 17:47:37 +01:00
parent ea97ec6cd0
commit 07a6647abb
3 changed files with 34 additions and 23 deletions
+27
View File
@@ -201,6 +201,17 @@ int serialize_pidref(FILE *f, FDSet *fds, const char *key, PidRef *pidref) {
return serialize_item_format(f, key, "@%i", copy);
}
int serialize_ratelimit(FILE *f, const char *key, const RateLimit *rl) {
assert(rl);
return serialize_item_format(f, key,
USEC_FMT " " USEC_FMT " %u %u",
rl->begin,
rl->interval,
rl->num,
rl->burst);
}
int serialize_item_hexmem(FILE *f, const char *key, const void *p, size_t l) {
_cleanup_free_ char *encoded = NULL;
int r;
@@ -486,6 +497,22 @@ int deserialize_pidref(FDSet *fds, const char *value, PidRef *ret) {
return 0;
}
void deserialize_ratelimit(RateLimit *rl, const char *name, const char *value) {
usec_t begin, interval;
unsigned num, burst;
assert(rl);
assert(name);
assert(value);
if (sscanf(value, USEC_FMT " " USEC_FMT " %u %u", &begin, &interval, &num, &burst) != 4)
return log_notice("Failed to parse %s, ignoring: %s", name, value);
/* Preserve the counter only if the configuration didn't change. */
rl->num = (interval == rl->interval && burst == rl->burst) ? num : 0;
rl->begin = begin;
}
int open_serialization_fd(const char *ident) {
int fd;