diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-09-08 17:02:11 +0200 |
---|---|---|
committer | Luca Boccassi <luca.boccassi@gmail.com> | 2023-09-08 21:59:44 +0200 |
commit | 0df7d525763ce76269c414d9c0a9ee9978edf299 (patch) | |
tree | 1f75d063077ba0dca01a637404b017d921cb9254 /src | |
parent | conf-parser: parse main config first only if not symlinked to/as drop-in (diff) | |
download | systemd-0df7d525763ce76269c414d9c0a9ee9978edf299.tar.xz systemd-0df7d525763ce76269c414d9c0a9ee9978edf299.zip |
serialize: add common deserialize_read_line() helper and use it everywhere
This introduces a common helper replacing three very similar uses.
Prompted by review of: #27890
Diffstat (limited to 'src')
-rw-r--r-- | src/core/job.c | 20 | ||||
-rw-r--r-- | src/core/manager-serialize.c | 14 | ||||
-rw-r--r-- | src/core/unit-serialize.c | 14 | ||||
-rw-r--r-- | src/shared/serialize.c | 28 | ||||
-rw-r--r-- | src/shared/serialize.h | 2 |
5 files changed, 48 insertions, 30 deletions
diff --git a/src/core/job.c b/src/core/job.c index acf8a78f41..e7d1f65dbc 100644 --- a/src/core/job.c +++ b/src/core/job.c @@ -1233,21 +1233,15 @@ int job_deserialize(Job *j, FILE *f) { assert(f); for (;;) { - _cleanup_free_ char *line = NULL; - char *l, *v; + _cleanup_free_ char *l = NULL; size_t k; + char *v; - r = read_line(f, LONG_LINE_MAX, &line); + r = deserialize_read_line(f, &l); if (r < 0) - return log_error_errno(r, "Failed to read serialization line: %m"); - if (r == 0) - return 0; - - l = strstrip(line); - - /* End marker */ - if (isempty(l)) - return 0; + return r; + if (r == 0) /* eof or end marker */ + break; k = strcspn(l, "="); @@ -1326,6 +1320,8 @@ int job_deserialize(Job *j, FILE *f) { } else log_debug("Unknown job serialization key: %s", l); } + + return 0; } int job_coldplug(Job *j) { diff --git a/src/core/manager-serialize.c b/src/core/manager-serialize.c index f4ad9b0642..71234ae936 100644 --- a/src/core/manager-serialize.c +++ b/src/core/manager-serialize.c @@ -338,17 +338,13 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { _cleanup_(manager_reloading_stopp) _unused_ Manager *reloading = manager_reloading_start(m); for (;;) { - _cleanup_free_ char *line = NULL; - const char *val, *l; + _cleanup_free_ char *l = NULL; + const char *val; - r = read_line(f, LONG_LINE_MAX, &line); + r = deserialize_read_line(f, &l); if (r < 0) - return log_error_errno(r, "Failed to read serialization line: %m"); - if (r == 0) - break; - - l = strstrip(line); - if (isempty(l)) /* end marker */ + return r; + if (r == 0) /* eof or end marker */ break; if ((val = startswith(l, "current-job-id="))) { diff --git a/src/core/unit-serialize.c b/src/core/unit-serialize.c index cb209c5bde..f0030d3211 100644 --- a/src/core/unit-serialize.c +++ b/src/core/unit-serialize.c @@ -272,19 +272,15 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { assert(fds); for (;;) { - _cleanup_free_ char *line = NULL; - char *l, *v; + _cleanup_free_ char *l = NULL; ssize_t m; size_t k; + char *v; - r = read_line(f, LONG_LINE_MAX, &line); + r = deserialize_read_line(f, &l); if (r < 0) - return log_error_errno(r, "Failed to read serialization line: %m"); - if (r == 0) /* eof */ - break; - - l = strstrip(line); - if (isempty(l)) /* End marker */ + return r; + if (r == 0) /* eof or end marker */ break; k = strcspn(l, "="); diff --git a/src/shared/serialize.c b/src/shared/serialize.c index 5396062a8b..669071dbd8 100644 --- a/src/shared/serialize.c +++ b/src/shared/serialize.c @@ -131,6 +131,34 @@ int serialize_strv(FILE *f, const char *key, char **l) { return ret; } +int deserialize_read_line(FILE *f, char **ret) { + _cleanup_free_ char *line = NULL; + int r; + + assert(f); + assert(ret); + + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return log_error_errno(r, "Failed to read serialization line: %m"); + if (r == 0) { /* eof */ + *ret = NULL; + return 0; + } + + const char *l = strstrip(line); + if (isempty(l)) { /* End marker */ + *ret = NULL; + return 0; + } + + if (free_and_strdup(&line, l) < 0) + return log_oom(); + + *ret = TAKE_PTR(line); + return 1; +} + int deserialize_strv(char ***l, const char *value) { ssize_t unescaped_len; char *unescaped; diff --git a/src/shared/serialize.h b/src/shared/serialize.h index 8b6e0999f1..fa1731acb5 100644 --- a/src/shared/serialize.h +++ b/src/shared/serialize.h @@ -23,6 +23,8 @@ static inline int serialize_bool_elide(FILE *f, const char *key, bool b) { return b ? serialize_item(f, key, yes_no(b)) : 0; } +int deserialize_read_line(FILE *f, char **ret); + int deserialize_usec(const char *value, usec_t *timestamp); int deserialize_dual_timestamp(const char *value, dual_timestamp *t); int deserialize_environment(const char *value, char ***environment); |