sysusers: convert to conf_file_read()

I was annoyed that systemd-sysusers doesn't print any info when it opens a
config file. Its read_config_file() started out the same as the one in tmpfiles,
and then they diverged. The one in tmpfiles has that logging, hence the rework
to use it here too and get better logging. The two programs should provide
similar functionality, so using a common helper will make it easier to extend
them in tandem later.

No functional change apart from the log info.

The userdata argument (Context) is moved to the last position as requested in
the review.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2024-02-09 17:57:41 +01:00
parent f6a1346e4e
commit 15d660fb41
2 changed files with 18 additions and 60 deletions
+4 -5
View File
@@ -439,12 +439,11 @@ int conf_file_read(
if (IN_SET(line[0], 0, '#'))
continue;
k = parse_line(fn, v, line, &invalid_line, userdata);
if (k < 0 && invalid_line) {
k = parse_line(fn, v, line, invalid_config ? &invalid_line : NULL, userdata);
if (k < 0 && invalid_line)
/* Allow reporting with a special code if the caller requested this. */
if (invalid_config)
*invalid_config = true;
} else
*invalid_config = true;
else
/* The first error, if any, becomes our return value. */
RET_GATHER(r, k);
}
+14 -55
View File
@@ -1680,11 +1680,13 @@ static int item_equivalent(Item *a, Item *b) {
}
static int parse_line(
Context *c,
const char *fname,
unsigned line,
const char *buffer) {
const char *buffer,
bool *invalid_config,
void *context) {
Context *c = ASSERT_PTR(context);
_cleanup_free_ char *action = NULL,
*name = NULL, *resolved_name = NULL,
*id = NULL, *resolved_id = NULL,
@@ -1697,10 +1699,10 @@ static int parse_line(
int r;
const char *p;
assert(c);
assert(fname);
assert(line >= 1);
assert(buffer);
assert(!invalid_config); /* We don't support invalid_config yet. */
/* Parse columns */
p = buffer;
@@ -1969,57 +1971,14 @@ static int parse_line(
}
static int read_config_file(Context *c, const char *fn, bool ignore_enoent) {
_cleanup_fclose_ FILE *rf = NULL;
_cleanup_free_ char *pp = NULL;
FILE *f = NULL;
unsigned v = 0;
int r = 0;
assert(c);
assert(fn);
if (streq(fn, "-"))
f = stdin;
else {
r = search_and_fopen(fn, "re", arg_root, (const char**) CONF_PATHS_STRV("sysusers.d"), &rf, &pp);
if (r < 0) {
if (ignore_enoent && r == -ENOENT)
return 0;
return log_error_errno(r, "Failed to open '%s', ignoring: %m", fn);
}
f = rf;
fn = pp;
}
for (;;) {
_cleanup_free_ char *line = NULL;
int k;
k = read_stripped_line(f, LONG_LINE_MAX, &line);
if (k < 0)
return log_error_errno(k, "Failed to read '%s': %m", fn);
if (k == 0)
break;
v++;
if (IN_SET(line[0], 0, '#'))
continue;
k = parse_line(c, fn, v, line);
if (k < 0 && r == 0)
r = k;
}
if (ferror(f)) {
log_error_errno(errno, "Failed to read from file %s: %m", fn);
if (r == 0)
r = -EIO;
}
return r;
return conf_file_read(
arg_root,
(const char**) CONF_PATHS_STRV("sysusers.d"),
ASSERT_PTR(fn),
parse_line,
ASSERT_PTR(c),
ignore_enoent,
/* invalid_config= */ NULL);
}
static int cat_config(void) {
@@ -2193,7 +2152,7 @@ static int parse_arguments(Context *c, char **args) {
STRV_FOREACH(arg, args) {
if (arg_inline)
/* Use (argument):n, where n==1 for the first positional arg */
r = parse_line(c, "(argument)", pos, *arg);
r = parse_line("(argument)", pos, *arg, /* invalid_config= */ NULL, c);
else
r = read_config_file(c, *arg, /* ignore_enoent= */ false);
if (r < 0)