1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-26 14:04:03 +03:00

tree-wide: port various parsers over to read_stripped_line()

This commit is contained in:
Lennart Poettering 2023-10-16 18:01:00 +02:00
parent c56cb33f09
commit 0ff6ff2b29
26 changed files with 127 additions and 194 deletions

View File

@ -473,16 +473,14 @@ static int get_process_id(pid_t pid, const char *field, uid_t *ret) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *l; char *l;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) if (r == 0)
break; break;
l = strstrip(line); l = startswith(line, field);
if (l) {
if (startswith(l, field)) {
l += strlen(field);
l += strspn(l, WHITESPACE); l += strspn(l, WHITESPACE);
l[strcspn(l, WHITESPACE)] = 0; l[strcspn(l, WHITESPACE)] = 0;

View File

@ -85,22 +85,20 @@ static int apply_file(const char *filename, bool ignore_enoent) {
log_debug("Applying %s%s", pp, special_glyph(SPECIAL_GLYPH_ELLIPSIS)); log_debug("Applying %s%s", pp, special_glyph(SPECIAL_GLYPH_ELLIPSIS));
for (unsigned line = 1;; line++) { for (unsigned line = 1;; line++) {
_cleanup_free_ char *text = NULL; _cleanup_free_ char *text = NULL;
char *p;
int k; int k;
k = read_line(f, LONG_LINE_MAX, &text); k = read_stripped_line(f, LONG_LINE_MAX, &text);
if (k < 0) if (k < 0)
return log_error_errno(k, "Failed to read file '%s': %m", pp); return log_error_errno(k, "Failed to read file '%s': %m", pp);
if (k == 0) if (k == 0)
break; break;
p = strstrip(text); if (isempty(text))
if (isempty(p))
continue; continue;
if (strchr(COMMENTS, p[0])) if (strchr(COMMENTS, text[0]))
continue; continue;
RET_GATHER(r, apply_rule(filename, line, p)); RET_GATHER(r, apply_rule(filename, line, text));
} }
return r; return r;

View File

@ -298,15 +298,14 @@ static int process(
uint64_t k, *q; uint64_t k, *q;
char *l; char *l;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) if (r == 0)
break; break;
/* Trim and skip the device */ /* Skip the device */
l = strstrip(line); l = line + strcspn(line, WHITESPACE);
l += strcspn(l, WHITESPACE);
l += strspn(l, WHITESPACE); l += strspn(l, WHITESPACE);
if (all_unified) { if (all_unified) {

View File

@ -221,21 +221,19 @@ static int manager_deserialize_one_unit(Manager *m, const char *name, FILE *f, F
} }
static int manager_deserialize_units(Manager *m, FILE *f, FDSet *fds) { static int manager_deserialize_units(Manager *m, FILE *f, FDSet *fds) {
const char *unit_name;
int r; int r;
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
/* Start marker */ /* Start marker */
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to read serialization line: %m"); return log_error_errno(r, "Failed to read serialization line: %m");
if (r == 0) if (r == 0)
break; break;
unit_name = strstrip(line); r = manager_deserialize_one_unit(m, line, f, fds);
r = manager_deserialize_one_unit(m, unit_name, f, fds);
if (r == -ENOMEM) if (r == -ENOMEM)
return r; return r;
if (r < 0) { if (r < 0) {

View File

@ -554,24 +554,22 @@ int unit_deserialize_state(Unit *u, FILE *f, FDSet *fds) {
int unit_deserialize_state_skip(FILE *f) { int unit_deserialize_state_skip(FILE *f) {
int r; int r;
assert(f); assert(f);
/* Skip serialized data for this unit. We don't know what it is. */ /* Skip serialized data for this unit. We don't know what it is. */
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *l;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to read serialization line: %m"); return log_error_errno(r, "Failed to read serialization line: %m");
if (r == 0) if (r == 0)
return 0; return 0;
l = strstrip(line);
/* End marker */ /* End marker */
if (isempty(l)) if (isempty(line))
return 1; return 1;
} }
} }

View File

@ -809,10 +809,10 @@ static int add_crypttab_devices(void) {
_cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL, _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL,
*keyfile = NULL, *keydev = NULL, *headerdev = NULL, *filtered_header = NULL; *keyfile = NULL, *keydev = NULL, *headerdev = NULL, *filtered_header = NULL;
crypto_device *d = NULL; crypto_device *d = NULL;
char *l, *uuid; char *uuid;
int k; int k;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to read %s: %m", arg_crypttab); return log_error_errno(r, "Failed to read %s: %m", arg_crypttab);
if (r == 0) if (r == 0)
@ -820,11 +820,10 @@ static int add_crypttab_devices(void) {
crypttab_line++; crypttab_line++;
l = strstrip(line); if (IN_SET(line[0], 0, '#'))
if (IN_SET(l[0], 0, '#'))
continue; continue;
k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyspec, &options); k = sscanf(line, "%ms %ms %ms %ms", &name, &device, &keyspec, &options);
if (k < 2 || k > 4) { if (k < 2 || k > 4) {
log_error("Failed to parse %s:%u, ignoring.", arg_crypttab, crypttab_line); log_error("Failed to parse %s:%u, ignoring.", arg_crypttab, crypttab_line);
continue; continue;

View File

@ -168,9 +168,8 @@ static int add_credential_gettys(void) {
for (;;) { for (;;) {
_cleanup_free_ char *tty = NULL; _cleanup_free_ char *tty = NULL;
char *s;
r = read_line(f, PATH_MAX, &tty); r = read_stripped_line(f, PATH_MAX, &tty);
if (r == 0) if (r == 0)
break; break;
if (r < 0) { if (r < 0) {
@ -178,11 +177,10 @@ static int add_credential_gettys(void) {
break; break;
} }
s = strstrip(tty); if (startswith(tty, "#"))
if (startswith(s, "#"))
continue; continue;
r = t->func(s); r = t->func(tty);
if (r < 0) if (r < 0)
return r; return r;
} }

View File

@ -132,9 +132,8 @@ static int add_integritytab_devices(void) {
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL, *name = NULL, *device_id = NULL, *device_path = NULL, *key_file = NULL, *options = NULL; _cleanup_free_ char *line = NULL, *name = NULL, *device_id = NULL, *device_path = NULL, *key_file = NULL, *options = NULL;
char *l;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to read %s: %m", arg_integritytab); return log_error_errno(r, "Failed to read %s: %m", arg_integritytab);
if (r == 0) if (r == 0)
@ -142,17 +141,13 @@ static int add_integritytab_devices(void) {
integritytab_line++; integritytab_line++;
l = strstrip(line); if (IN_SET(line[0], 0, '#'))
if (!l)
continue;
if (IN_SET(l[0], 0, '#'))
continue; continue;
/* The key file and the options are optional */ /* The key file and the options are optional */
r = sscanf(l, "%ms %ms %ms %ms", &name, &device_id, &key_file, &options); r = sscanf(line, "%ms %ms %ms %ms", &name, &device_id, &key_file, &options);
if (!IN_SET(r, 2, 3, 4)) { if (!IN_SET(r, 2, 3, 4)) {
log_error("Failed to parse %s:%u, ignoring.", l, integritytab_line); log_error("Failed to parse %s:%u, ignoring.", arg_integritytab, integritytab_line);
continue; continue;
} }

View File

@ -70,7 +70,6 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) {
_cleanup_free_ char *b = NULL; _cleanup_free_ char *b = NULL;
_cleanup_free_ const char *fn = NULL; _cleanup_free_ const char *fn = NULL;
const char *c = NULL; const char *c = NULL;
size_t n;
int r; int r;
assert(field); assert(field);
@ -93,26 +92,21 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) {
return -errno; return -errno;
} }
/* This is an awful parse, but it follows closely what /* This is an awful parse, but it follows closely what xdg-user-dirs does upstream */
* xdg-user-dirs does upstream */
n = strlen(field);
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *l, *p, *e; char *p, *e;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) if (r == 0)
break; break;
l = strstrip(line); p = startswith(line, field);
if (!p)
if (!strneq(l, field, n))
continue; continue;
p = l + n;
p += strspn(p, WHITESPACE); p += strspn(p, WHITESPACE);
if (*p != '=') if (*p != '=')

View File

@ -314,27 +314,25 @@ static int list_x11_keymaps(int argc, char **argv, void *userdata) {
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *l, *w; char *w;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to read keyboard mapping list: %m"); return log_error_errno(r, "Failed to read keyboard mapping list: %m");
if (r == 0) if (r == 0)
break; break;
l = strstrip(line); if (isempty(line))
if (isempty(l))
continue; continue;
if (l[0] == '!') { if (line[0] == '!') {
if (startswith(l, "! model")) if (startswith(line, "! model"))
state = MODELS; state = MODELS;
else if (startswith(l, "! layout")) else if (startswith(line, "! layout"))
state = LAYOUTS; state = LAYOUTS;
else if (startswith(l, "! variant")) else if (startswith(line, "! variant"))
state = VARIANTS; state = VARIANTS;
else if (startswith(l, "! option")) else if (startswith(line, "! option"))
state = OPTIONS; state = OPTIONS;
else else
state = NONE; state = NONE;
@ -345,7 +343,7 @@ static int list_x11_keymaps(int argc, char **argv, void *userdata) {
if (state != look_for) if (state != look_for)
continue; continue;
w = l + strcspn(l, WHITESPACE); w = line + strcspn(line, WHITESPACE);
if (argc > 1) { if (argc > 1) {
char *e; char *e;
@ -368,8 +366,7 @@ static int list_x11_keymaps(int argc, char **argv, void *userdata) {
} else } else
*w = 0; *w = 0;
r = strv_extend(&list, l); if (strv_consume(&list, TAKE_PTR(line)) < 0)
if (r < 0)
return log_oom(); return log_oom();
} }

View File

@ -434,22 +434,20 @@ int x11_read_data(Context *c, sd_bus_message *m) {
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *l;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) if (r == 0)
break; break;
l = strstrip(line); if (IN_SET(line[0], 0, '#'))
if (IN_SET(l[0], 0, '#'))
continue; continue;
if (in_section && first_word(l, "Option")) { if (in_section && first_word(line, "Option")) {
_cleanup_strv_free_ char **a = NULL; _cleanup_strv_free_ char **a = NULL;
r = strv_split_full(&a, l, WHITESPACE, EXTRACT_UNQUOTE); r = strv_split_full(&a, line, WHITESPACE, EXTRACT_UNQUOTE);
if (r < 0) if (r < 0)
return r; return r;
@ -469,17 +467,17 @@ int x11_read_data(Context *c, sd_bus_message *m) {
free_and_replace(*p, a[2]); free_and_replace(*p, a[2]);
} }
} else if (!in_section && first_word(l, "Section")) { } else if (!in_section && first_word(line, "Section")) {
_cleanup_strv_free_ char **a = NULL; _cleanup_strv_free_ char **a = NULL;
r = strv_split_full(&a, l, WHITESPACE, EXTRACT_UNQUOTE); r = strv_split_full(&a, line, WHITESPACE, EXTRACT_UNQUOTE);
if (r < 0) if (r < 0)
return -ENOMEM; return -ENOMEM;
if (strv_length(a) == 2 && streq(a[1], "InputClass")) if (strv_length(a) == 2 && streq(a[1], "InputClass"))
in_section = true; in_section = true;
} else if (in_section && first_word(l, "EndSection")) } else if (in_section && first_word(line, "EndSection"))
in_section = false; in_section = false;
} }
@ -618,10 +616,9 @@ static int read_next_mapping(
_cleanup_strv_free_ char **b = NULL; _cleanup_strv_free_ char **b = NULL;
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
size_t length; size_t length;
const char *l;
int r; int r;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) if (r == 0)
@ -629,11 +626,10 @@ static int read_next_mapping(
(*n)++; (*n)++;
l = strstrip(line); if (IN_SET(line[0], 0, '#'))
if (IN_SET(l[0], 0, '#'))
continue; continue;
r = strv_split_full(&b, l, WHITESPACE, EXTRACT_UNQUOTE); r = strv_split_full(&b, line, WHITESPACE, EXTRACT_UNQUOTE);
if (r < 0) if (r < 0)
return r; return r;
@ -1008,16 +1004,14 @@ static int locale_gen_locale_supported(const char *locale_entry) {
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *l;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return log_debug_errno(r, "Failed to read /usr/share/i18n/SUPPORTED: %m"); return log_debug_errno(r, "Failed to read /usr/share/i18n/SUPPORTED: %m");
if (r == 0) if (r == 0)
return 0; return 0;
l = strstrip(line); if (strcaseeq_ptr(line, locale_entry))
if (strcaseeq_ptr(l, locale_entry))
return 1; return 1;
} }
} }

View File

@ -79,22 +79,20 @@ static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent
log_debug("apply: %s", pp); log_debug("apply: %s", pp);
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *l;
int k; int k;
k = read_line(f, LONG_LINE_MAX, &line); k = read_stripped_line(f, LONG_LINE_MAX, &line);
if (k < 0) if (k < 0)
return log_error_errno(k, "Failed to read file '%s': %m", pp); return log_error_errno(k, "Failed to read file '%s': %m", pp);
if (k == 0) if (k == 0)
break; break;
l = strstrip(line); if (isempty(line))
if (isempty(l))
continue; continue;
if (strchr(COMMENTS, *l)) if (strchr(COMMENTS, *line))
continue; continue;
k = module_load_and_warn(ctx, l, true); k = module_load_and_warn(ctx, line, true);
if (k == -ENOENT) if (k == -ENOENT)
continue; continue;
RET_GATHER(r, k); RET_GATHER(r, k);

View File

@ -325,19 +325,17 @@ static int dhcp4_server_set_dns_from_resolve_conf(Link *link) {
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
const char *a; const char *a;
char *l;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to read " PRIVATE_UPLINK_RESOLV_CONF ": %m"); return log_error_errno(r, "Failed to read " PRIVATE_UPLINK_RESOLV_CONF ": %m");
if (r == 0) if (r == 0)
break; break;
l = strstrip(line); if (IN_SET(*line, '#', ';', 0))
if (IN_SET(*l, '#', ';', 0))
continue; continue;
a = first_word(l, "nameserver"); a = first_word(line, "nameserver");
if (!a) if (!a)
continue; continue;

View File

@ -226,9 +226,9 @@ int resolvconf_parse_argv(int argc, char *argv[]) {
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
const char *a, *l; const char *a;
r = read_line(stdin, LONG_LINE_MAX, &line); r = read_stripped_line(stdin, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to read from stdin: %m"); return log_error_errno(r, "Failed to read from stdin: %m");
if (r == 0) if (r == 0)
@ -236,25 +236,24 @@ int resolvconf_parse_argv(int argc, char *argv[]) {
n++; n++;
l = strstrip(line); if (IN_SET(*line, '#', ';', 0))
if (IN_SET(*l, '#', ';', 0))
continue; continue;
a = first_word(l, "nameserver"); a = first_word(line, "nameserver");
if (a) { if (a) {
(void) parse_nameserver(a); (void) parse_nameserver(a);
continue; continue;
} }
a = first_word(l, "domain"); a = first_word(line, "domain");
if (!a) if (!a)
a = first_word(l, "search"); a = first_word(line, "search");
if (a) { if (a) {
(void) parse_search_domain(a); (void) parse_search_domain(a);
continue; continue;
} }
log_syntax(NULL, LOG_DEBUG, "stdin", n, 0, "Ignoring resolv.conf line: %s", l); log_syntax(NULL, LOG_DEBUG, "stdin", n, 0, "Ignoring resolv.conf line: %s", line);
} }
if (type == TYPE_EXCLUSIVE) { if (type == TYPE_EXCLUSIVE) {

View File

@ -441,9 +441,8 @@ static int dns_trust_anchor_load_files(
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *l;
r = read_line(g, LONG_LINE_MAX, &line); r = read_stripped_line(g, LONG_LINE_MAX, &line);
if (r < 0) { if (r < 0) {
log_warning_errno(r, "Failed to read '%s', ignoring: %m", *f); log_warning_errno(r, "Failed to read '%s', ignoring: %m", *f);
break; break;
@ -453,14 +452,13 @@ static int dns_trust_anchor_load_files(
n++; n++;
l = strstrip(line); if (isempty(line))
if (isempty(l))
continue; continue;
if (*l == ';') if (*line == ';')
continue; continue;
(void) loader(d, *f, n, l); (void) loader(d, *f, n, line);
} }
} }

View File

@ -122,9 +122,8 @@ int manager_read_resolv_conf(Manager *m) {
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
const char *a; const char *a;
char *l;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) { if (r < 0) {
log_error_errno(r, "Failed to read /etc/resolv.conf: %m"); log_error_errno(r, "Failed to read /etc/resolv.conf: %m");
goto clear; goto clear;
@ -134,11 +133,10 @@ int manager_read_resolv_conf(Manager *m) {
n++; n++;
l = strstrip(line); if (IN_SET(*line, '#', ';', 0))
if (IN_SET(*l, '#', ';', 0))
continue; continue;
a = first_word(l, "nameserver"); a = first_word(line, "nameserver");
if (a) { if (a) {
r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a); r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a);
if (r < 0) if (r < 0)
@ -147,9 +145,9 @@ int manager_read_resolv_conf(Manager *m) {
continue; continue;
} }
a = first_word(l, "domain"); a = first_word(line, "domain");
if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */ if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */
a = first_word(l, "search"); a = first_word(line, "search");
if (a) { if (a) {
r = manager_parse_search_domains_and_warn(m, a); r = manager_parse_search_domains_and_warn(m, a);
if (r < 0) if (r < 0)
@ -158,7 +156,7 @@ int manager_read_resolv_conf(Manager *m) {
continue; continue;
} }
log_syntax(NULL, LOG_DEBUG, "/etc/resolv.conf", n, 0, "Ignoring resolv.conf line: %s", l); log_syntax(NULL, LOG_DEBUG, "/etc/resolv.conf", n, 0, "Ignoring resolv.conf line: %s", line);
} }
m->resolv_conf_stat = st; m->resolv_conf_stat = st;

View File

@ -325,9 +325,8 @@ static int boot_entry_load_type1(
for (;;) { for (;;) {
_cleanup_free_ char *buf = NULL, *field = NULL; _cleanup_free_ char *buf = NULL, *field = NULL;
const char *p;
r = read_line(f, LONG_LINE_MAX, &buf); r = read_stripped_line(f, LONG_LINE_MAX, &buf);
if (r == 0) if (r == 0)
break; break;
if (r == -ENOBUFS) if (r == -ENOBUFS)
@ -337,10 +336,10 @@ static int boot_entry_load_type1(
line++; line++;
p = strstrip(buf); if (IN_SET(buf[0], '#', '\0'))
if (IN_SET(p[0], '#', '\0'))
continue; continue;
const char *p = buf;
r = extract_first_word(&p, &field, NULL, 0); r = extract_first_word(&p, &field, NULL, 0);
if (r < 0) { if (r < 0) {
log_syntax(NULL, LOG_WARNING, tmp.path, line, r, "Failed to parse, ignoring line: %m"); log_syntax(NULL, LOG_WARNING, tmp.path, line, r, "Failed to parse, ignoring line: %m");
@ -450,9 +449,8 @@ int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path) {
for (;;) { for (;;) {
_cleanup_free_ char *buf = NULL, *field = NULL; _cleanup_free_ char *buf = NULL, *field = NULL;
const char *p;
r = read_line(file, LONG_LINE_MAX, &buf); r = read_stripped_line(file, LONG_LINE_MAX, &buf);
if (r == 0) if (r == 0)
break; break;
if (r == -ENOBUFS) if (r == -ENOBUFS)
@ -462,10 +460,10 @@ int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path) {
line++; line++;
p = strstrip(buf); if (IN_SET(buf[0], '#', '\0'))
if (IN_SET(p[0], '#', '\0'))
continue; continue;
const char *p = buf;
r = extract_first_word(&p, &field, NULL, 0); r = extract_first_word(&p, &field, NULL, 0);
if (r < 0) { if (r < 0) {
log_syntax(NULL, LOG_WARNING, path, line, r, "Failed to parse, ignoring line: %m"); log_syntax(NULL, LOG_WARNING, path, line, r, "Failed to parse, ignoring line: %m");

View File

@ -79,32 +79,24 @@ int read_etc_hostname_stream(FILE *f, char **ret) {
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *p;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) /* EOF without any hostname? the file is empty, let's treat that exactly like no file at all: ENOENT */ if (r == 0) /* EOF without any hostname? the file is empty, let's treat that exactly like no file at all: ENOENT */
return -ENOENT; return -ENOENT;
p = strstrip(line);
/* File may have empty lines or comments, ignore them */ /* File may have empty lines or comments, ignore them */
if (!IN_SET(*p, '\0', '#')) { if (IN_SET(line[0], '\0', '#'))
char *copy; continue;
hostname_cleanup(p); /* normalize the hostname */ hostname_cleanup(line); /* normalize the hostname */
if (!hostname_is_valid(p, VALID_HOSTNAME_TRAILING_DOT)) /* check that the hostname we return is valid */ if (!hostname_is_valid(line, VALID_HOSTNAME_TRAILING_DOT)) /* check that the hostname we return is valid */
return -EBADMSG; return -EBADMSG;
copy = strdup(p); *ret = TAKE_PTR(line);
if (!copy) return 0;
return -ENOMEM;
*ret = copy;
return 0;
}
} }
} }

View File

@ -3246,23 +3246,21 @@ static int read_presets(RuntimeScope scope, const char *root_dir, UnitFilePreset
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
_cleanup_(unit_file_preset_rule_done) UnitFilePresetRule rule = {}; _cleanup_(unit_file_preset_rule_done) UnitFilePresetRule rule = {};
const char *parameter; const char *parameter;
char *l;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) if (r == 0)
break; break;
l = strstrip(line);
n++; n++;
if (isempty(l)) if (isempty(line))
continue; continue;
if (strchr(COMMENTS, *l)) if (strchr(COMMENTS, line[0]))
continue; continue;
parameter = first_word(l, "enable"); parameter = first_word(line, "enable");
if (parameter) { if (parameter) {
char *unit_name; char *unit_name;
char **instances = NULL; char **instances = NULL;
@ -3281,7 +3279,7 @@ static int read_presets(RuntimeScope scope, const char *root_dir, UnitFilePreset
}; };
} }
parameter = first_word(l, "disable"); parameter = first_word(line, "disable");
if (parameter) { if (parameter) {
char *pattern; char *pattern;
@ -3295,7 +3293,7 @@ static int read_presets(RuntimeScope scope, const char *root_dir, UnitFilePreset
}; };
} }
parameter = first_word(l, "ignore"); parameter = first_word(line, "ignore");
if (parameter) { if (parameter) {
char *pattern; char *pattern;

View File

@ -268,7 +268,7 @@ int deserialize_read_line(FILE *f, char **ret) {
assert(f); assert(f);
assert(ret); assert(ret);
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to read serialization line: %m"); return log_error_errno(r, "Failed to read serialization line: %m");
if (r == 0) { /* eof */ if (r == 0) { /* eof */
@ -276,15 +276,11 @@ int deserialize_read_line(FILE *f, char **ret) {
return 0; return 0;
} }
const char *l = strstrip(line); if (isempty(line)) { /* End marker */
if (isempty(l)) { /* End marker */
*ret = NULL; *ret = NULL;
return 0; return 0;
} }
if (free_and_strdup(&line, l) < 0)
return log_oom();
*ret = TAKE_PTR(line); *ret = TAKE_PTR(line);
return 1; return 1;
} }

View File

@ -238,10 +238,10 @@ static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ig
_cleanup_free_ char *l = NULL; _cleanup_free_ char *l = NULL;
bool ignore_failure = false; bool ignore_failure = false;
Option *existing; Option *existing;
char *p, *value; char *value;
int k; int k;
k = read_line(f, LONG_LINE_MAX, &l); k = read_stripped_line(f, LONG_LINE_MAX, &l);
if (k == 0) if (k == 0)
break; break;
if (k < 0) if (k < 0)
@ -249,13 +249,12 @@ static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ig
c++; c++;
p = strstrip(l); if (isempty(l))
if (isempty(p))
continue; continue;
if (strchr(COMMENTS "\n", *p)) if (strchr(COMMENTS, l[0]))
continue; continue;
char *p = l;
value = strchr(p, '='); value = strchr(p, '=');
if (value) { if (value) {
if (p[0] == '-') { if (p[0] == '-') {

View File

@ -2005,10 +2005,9 @@ static int read_config_file(Context *c, const char *fn, bool ignore_enoent) {
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
char *l;
int k; int k;
k = read_line(f, LONG_LINE_MAX, &line); k = read_stripped_line(f, LONG_LINE_MAX, &line);
if (k < 0) if (k < 0)
return log_error_errno(k, "Failed to read '%s': %m", fn); return log_error_errno(k, "Failed to read '%s': %m", fn);
if (k == 0) if (k == 0)
@ -2016,11 +2015,10 @@ static int read_config_file(Context *c, const char *fn, bool ignore_enoent) {
v++; v++;
l = strstrip(line); if (IN_SET(line[0], 0, '#'))
if (IN_SET(*l, 0, '#'))
continue; continue;
k = parse_line(c, fn, v, l); k = parse_line(c, fn, v, line);
if (k < 0 && r == 0) if (k < 0 && r == 0)
r = k; r = k;
} }

View File

@ -450,9 +450,8 @@ static int load_sysv(SysvStub *s) {
for (;;) { for (;;) {
_cleanup_free_ char *l = NULL; _cleanup_free_ char *l = NULL;
char *t;
r = read_line(f, LONG_LINE_MAX, &l); r = read_stripped_line(f, LONG_LINE_MAX, &l);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to read configuration file '%s': %m", s->path); return log_error_errno(r, "Failed to read configuration file '%s': %m", s->path);
if (r == 0) if (r == 0)
@ -460,17 +459,16 @@ static int load_sysv(SysvStub *s) {
line++; line++;
t = strstrip(l); if (l[0] != '#') {
if (*t != '#') {
/* Try to figure out whether this init script supports /* Try to figure out whether this init script supports
* the reload operation. This heuristic looks for * the reload operation. This heuristic looks for
* "Usage" lines which include the reload option. */ * "Usage" lines which include the reload option. */
if (state == USAGE_CONTINUATION || if (state == USAGE_CONTINUATION ||
(state == NORMAL && strcasestr(t, "usage"))) { (state == NORMAL && strcasestr(l, "usage"))) {
if (usage_contains_reload(t)) { if (usage_contains_reload(l)) {
supports_reload = true; supports_reload = true;
state = NORMAL; state = NORMAL;
} else if (t[strlen(t)-1] == '\\') } else if (endswith(l, "\\"))
state = USAGE_CONTINUATION; state = USAGE_CONTINUATION;
else else
state = NORMAL; state = NORMAL;
@ -479,18 +477,18 @@ static int load_sysv(SysvStub *s) {
continue; continue;
} }
if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) { if (state == NORMAL && streq(l, "### BEGIN INIT INFO")) {
state = LSB; state = LSB;
s->has_lsb = true; s->has_lsb = true;
continue; continue;
} }
if (IN_SET(state, LSB_DESCRIPTION, LSB) && streq(t, "### END INIT INFO")) { if (IN_SET(state, LSB_DESCRIPTION, LSB) && streq(l, "### END INIT INFO")) {
state = NORMAL; state = NORMAL;
continue; continue;
} }
t++; char *t = l + 1;
t += strspn(t, WHITESPACE); t += strspn(t, WHITESPACE);
if (state == NORMAL) { if (state == NORMAL) {

View File

@ -211,9 +211,8 @@ static int context_parse_ntp_services_from_disk(Context *c) {
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
const char *word;
r = read_line(file, LINE_MAX, &line); r = read_stripped_line(file, LINE_MAX, &line);
if (r < 0) { if (r < 0) {
log_error_errno(r, "Failed to read %s, ignoring: %m", *f); log_error_errno(r, "Failed to read %s, ignoring: %m", *f);
continue; continue;
@ -221,13 +220,12 @@ static int context_parse_ntp_services_from_disk(Context *c) {
if (r == 0) if (r == 0)
break; break;
word = strstrip(line); if (isempty(line) || startswith(line, "#"))
if (isempty(word) || startswith(word, "#"))
continue; continue;
r = context_add_ntp_service(c, word, *f); r = context_add_ntp_service(c, line, *f);
if (r < 0) if (r < 0)
log_warning_errno(r, "Failed to add NTP service \"%s\", ignoring: %m", word); log_warning_errno(r, "Failed to add NTP service \"%s\", ignoring: %m", line);
} }
} }

View File

@ -4203,10 +4203,9 @@ static int read_config_file(
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
bool invalid_line = false; bool invalid_line = false;
char *l;
int k; int k;
k = read_line(f, LONG_LINE_MAX, &line); k = read_stripped_line(f, LONG_LINE_MAX, &line);
if (k < 0) if (k < 0)
return log_error_errno(k, "Failed to read '%s': %m", fn); return log_error_errno(k, "Failed to read '%s': %m", fn);
if (k == 0) if (k == 0)
@ -4214,11 +4213,10 @@ static int read_config_file(
v++; v++;
l = strstrip(line); if (IN_SET(line[0], 0, '#'))
if (IN_SET(*l, 0, '#'))
continue; continue;
k = parse_line(c, fn, v, l, &invalid_line, &uid_cache, &gid_cache); k = parse_line(c, fn, v, line, &invalid_line, &uid_cache, &gid_cache);
if (k < 0) { if (k < 0) {
if (invalid_line) if (invalid_line)
/* Allow reporting with a special code if the caller requested this */ /* Allow reporting with a special code if the caller requested this */

View File

@ -453,9 +453,9 @@ static int add_veritytab_devices(void) {
for (;;) { for (;;) {
_cleanup_free_ char *line = NULL, *name = NULL, *data_device = NULL, *hash_device = NULL, _cleanup_free_ char *line = NULL, *name = NULL, *data_device = NULL, *hash_device = NULL,
*roothash = NULL, *options = NULL; *roothash = NULL, *options = NULL;
char *l, *data_uuid, *hash_uuid; char *data_uuid, *hash_uuid;
r = read_line(f, LONG_LINE_MAX, &line); r = read_stripped_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to read %s: %m", arg_veritytab); return log_error_errno(r, "Failed to read %s: %m", arg_veritytab);
if (r == 0) if (r == 0)
@ -463,11 +463,10 @@ static int add_veritytab_devices(void) {
veritytab_line++; veritytab_line++;
l = strstrip(line); if (IN_SET(line[0], 0, '#'))
if (IN_SET(l[0], 0, '#'))
continue; continue;
r = sscanf(l, "%ms %ms %ms %ms %ms", &name, &data_device, &hash_device, &roothash, &options); r = sscanf(line, "%ms %ms %ms %ms %ms", &name, &data_device, &hash_device, &roothash, &options);
if (!IN_SET(r, 4, 5)) { if (!IN_SET(r, 4, 5)) {
log_error("Failed to parse %s:%u, ignoring.", arg_veritytab, veritytab_line); log_error("Failed to parse %s:%u, ignoring.", arg_veritytab, veritytab_line);
continue; continue;