conf-parser: use log_syntax_parse_error() and friends more

This also makes all conf parsers defined in conf-parser.c return 1
on success, 0 on non-critical error.
Also, use free_and_strdup_warn() where applicable.
This commit is contained in:
Yu Watanabe
2024-09-02 06:17:05 +09:00
parent 3c8dc3a3e0
commit 6f12cb91bc
2 changed files with 137 additions and 202 deletions
+126 -191
View File
@@ -892,13 +892,11 @@ int config_parse_iec_size(
r = parse_size(rvalue, 1024, &v);
if (r >= 0 && (uint64_t) (size_t) v != v)
r = -ERANGE;
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
*sz = (size_t) v;
return 0;
return 1;
}
int config_parse_si_uint64(
@@ -922,9 +920,9 @@ int config_parse_si_uint64(
r = parse_size(rvalue, 1000, sz);
if (r < 0)
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue);
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
return 0;
return 1;
}
int config_parse_iec_uint64(
@@ -948,9 +946,9 @@ int config_parse_iec_uint64(
r = parse_size(rvalue, 1024, bytes);
if (r < 0)
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse size value '%s', ignoring: %m", rvalue);
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
return 0;
return 1;
}
int config_parse_iec_uint64_infinity(
@@ -971,7 +969,7 @@ int config_parse_iec_uint64_infinity(
if (streq(rvalue, "infinity")) {
*bytes = UINT64_MAX;
return 0;
return 1;
}
return config_parse_iec_uint64(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
@@ -989,23 +987,21 @@ int config_parse_bool(
void *data,
void *userdata) {
int k;
bool *b = ASSERT_PTR(data);
bool fatal = ltype;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
k = parse_boolean(rvalue);
if (k < 0) {
log_syntax(unit, fatal ? LOG_ERR : LOG_WARNING, filename, line, k,
"Failed to parse boolean value%s: %s",
fatal ? "" : ", ignoring", rvalue);
r = parse_boolean(rvalue);
if (r < 0) {
log_syntax_parse_error_full(unit, filename, line, r, fatal, lvalue, rvalue);
return fatal ? -ENOEXEC : 0;
}
*b = k;
*b = r;
return 1; /* set */
}
@@ -1027,12 +1023,8 @@ int config_parse_uint32_flag(
assert(ltype != 0);
r = isempty(rvalue) ? 0 : parse_boolean(rvalue);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse %s=%s. Ignoring assignment: %m",
lvalue, rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
SET_FLAG(*flags, ltype, r);
return 1;
@@ -1058,12 +1050,14 @@ int config_parse_id128(
assert(rvalue);
r = id128_from_string_nonzero(rvalue, result);
if (r == -ENXIO)
if (r == -ENXIO) {
log_syntax(unit, LOG_WARNING, filename, line, r, "128-bit ID/UUID is all 0, ignoring: %s", rvalue);
else if (r < 0)
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse 128-bit ID/UUID, ignoring: %s", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
return 0;
return 1;
}
int config_parse_tristate(
@@ -1093,11 +1087,8 @@ int config_parse_tristate(
}
r = parse_tristate(rvalue, t);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse boolean value for %s=, ignoring: %s", lvalue, rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
return 1;
}
@@ -1144,7 +1135,7 @@ int config_parse_string(
return 0;
}
r = free_and_strdup_warn(s, empty_to_null(rvalue));
r = free_and_strdup_warn(s, rvalue);
if (r < 0)
return r;
@@ -1172,7 +1163,7 @@ int config_parse_dns_name(
if (isempty(rvalue)) {
*hostname = mfree(*hostname);
return 0;
return 1;
}
r = dns_name_is_valid(rvalue);
@@ -1187,7 +1178,11 @@ int config_parse_dns_name(
return 0;
}
return free_and_strdup_warn(hostname, rvalue);
r = free_and_strdup_warn(hostname, rvalue);
if (r < 0)
return r;
return 1;
}
int config_parse_hostname(
@@ -1210,7 +1205,7 @@ int config_parse_hostname(
if (isempty(rvalue)) {
*hostname = mfree(*hostname);
return 0;
return 1;
}
if (!hostname_is_valid(rvalue, 0)) {
@@ -1244,8 +1239,10 @@ int config_parse_path(
assert(lvalue);
assert(rvalue);
if (isempty(rvalue))
goto finalize;
if (isempty(rvalue)) {
*s = mfree(*s);
return 1;
}
n = strdup(rvalue);
if (!n)
@@ -1255,8 +1252,8 @@ int config_parse_path(
if (r < 0)
return fatal ? -ENOEXEC : 0;
finalize:
return free_and_replace(*s, n);
free_and_replace(*s, n);
return 1;
}
int config_parse_strv(
@@ -1280,7 +1277,7 @@ int config_parse_strv(
if (isempty(rvalue)) {
*sv = strv_free(*sv);
return 0;
return 1;
}
_cleanup_strv_free_ char **strv = NULL;
@@ -1288,14 +1285,10 @@ int config_parse_strv(
char *word;
r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE);
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
if (r == 0)
break;
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
return 0;
}
r = strv_consume(&strv, word);
if (r < 0)
@@ -1306,7 +1299,7 @@ int config_parse_strv(
if (r < 0)
return log_oom();
return 0;
return 1;
}
int config_parse_warn_compat(
@@ -1364,14 +1357,12 @@ int config_parse_log_facility(
assert(data);
x = log_facility_unshifted_from_string(rvalue);
if (x < 0) {
log_syntax(unit, LOG_WARNING, filename, line, x, "Failed to parse log facility, ignoring: %s", rvalue);
return 0;
}
if (x < 0)
return log_syntax_parse_error(unit, filename, line, x, lvalue, rvalue);
*o = (x << 3) | LOG_PRI(*o);
return 0;
return 1;
}
int config_parse_log_level(
@@ -1394,17 +1385,15 @@ int config_parse_log_level(
assert(data);
x = log_level_from_string(rvalue);
if (x < 0) {
log_syntax(unit, LOG_WARNING, filename, line, x, "Failed to parse log level, ignoring: %s", rvalue);
return 0;
}
if (x < 0)
return log_syntax_parse_error(unit, filename, line, x, lvalue, rvalue);
if (*o < 0) /* if it wasn't initialized so far, assume zero facility */
*o = x;
else
*o = (*o & LOG_FACMASK) | x;
return 0;
return 1;
}
int config_parse_signal(
@@ -1427,13 +1416,11 @@ int config_parse_signal(
assert(sig);
r = signal_from_string(rvalue);
if (r <= 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse signal name, ignoring: %s", rvalue);
return 0;
}
if (r <= 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
*sig = r;
return 0;
return 1;
}
int config_parse_personality(
@@ -1455,18 +1442,17 @@ int config_parse_personality(
assert(rvalue);
assert(personality);
if (isempty(rvalue))
p = PERSONALITY_INVALID;
else {
p = personality_from_string(rvalue);
if (p == PERSONALITY_INVALID) {
log_syntax(unit, LOG_WARNING, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
return 0;
}
if (isempty(rvalue)) {
*personality = PERSONALITY_INVALID;
return 1;
}
p = personality_from_string(rvalue);
if (p == PERSONALITY_INVALID)
return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue);
*personality = p;
return 0;
return 1;
}
int config_parse_ifname(
@@ -1498,9 +1484,9 @@ int config_parse_ifname(
return 0;
}
r = free_and_strdup(s, rvalue);
r = free_and_strdup_warn(s, rvalue);
if (r < 0)
return log_oom();
return r;
return 1;
}
@@ -1527,21 +1513,15 @@ int config_parse_ifnames(
if (isempty(rvalue)) {
*s = strv_free(*s);
return 0;
return 1;
}
for (const char *p = rvalue;;) {
_cleanup_free_ char *word = NULL;
r = extract_first_word(&p, &word, NULL, 0);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to extract interface name, ignoring assignment: %s",
rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
if (r == 0)
break;
@@ -1561,7 +1541,7 @@ int config_parse_ifnames(
if (r < 0)
return log_oom();
return 0;
return 1;
}
int config_parse_ip_port(
@@ -1586,18 +1566,15 @@ int config_parse_ip_port(
if (isempty(rvalue)) {
*s = 0;
return 0;
return 1;
}
r = parse_ip_port(rvalue, &port);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse port '%s'.", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
*s = port;
return 0;
return 1;
}
int config_parse_mtu(
@@ -1625,11 +1602,8 @@ int config_parse_mtu(
rvalue);
return 0;
}
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse MTU value '%s', ignoring: %m", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
return 1;
}
@@ -1657,10 +1631,8 @@ int config_parse_rlimit(
log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue);
return 0;
}
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse resource value, ignoring: %s", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
if (rl[ltype])
*rl[ltype] = d;
@@ -1670,7 +1642,7 @@ int config_parse_rlimit(
return log_oom();
}
return 0;
return 1;
}
int config_parse_permille(
@@ -1693,15 +1665,11 @@ int config_parse_permille(
assert(rvalue);
r = parse_permille(rvalue);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse permille value, ignoring: %s", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
*permille = (unsigned) r;
return 0;
return 1;
}
int config_parse_vlanprotocol(
@@ -1723,20 +1691,17 @@ int config_parse_vlanprotocol(
if (isempty(rvalue)) {
*vlan_protocol = -1;
return 0;
return 1;
}
if (STR_IN_SET(rvalue, "802.1ad", "802.1AD"))
*vlan_protocol = ETH_P_8021AD;
else if (STR_IN_SET(rvalue, "802.1q", "802.1Q"))
*vlan_protocol = ETH_P_8021Q;
else {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Failed to parse VLAN protocol value, ignoring: %s", rvalue);
return 0;
}
else
return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue);
return 0;
return 1;
}
int config_parse_hw_addr(
@@ -1760,18 +1725,15 @@ int config_parse_hw_addr(
if (isempty(rvalue)) {
*hwaddr = HW_ADDR_NULL;
return 0;
return 1;
}
r = parse_hw_addr_full(rvalue, ltype, &a);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Not a valid hardware address, ignoring assignment: %s", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
*hwaddr = a;
return 0;
return 1;
}
int config_parse_hw_addrs(
@@ -1796,7 +1758,7 @@ int config_parse_hw_addrs(
if (isempty(rvalue)) {
/* Empty assignment resets the list */
*hwaddrs = set_free(*hwaddrs);
return 0;
return 1;
}
for (const char *p = rvalue;;) {
@@ -1804,15 +1766,10 @@ int config_parse_hw_addrs(
_cleanup_free_ struct hw_addr_data *n = NULL;
r = extract_first_word(&p, &word, NULL, 0);
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
if (r == 0)
return 0;
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid syntax, ignoring: %s", rvalue);
return 0;
}
return 1;
n = new(struct hw_addr_data, 1);
if (!n)
@@ -1853,7 +1810,7 @@ int config_parse_ether_addr(
if (isempty(rvalue)) {
*hwaddr = mfree(*hwaddr);
return 0;
return 1;
}
n = new0(struct ether_addr, 1);
@@ -1861,15 +1818,11 @@ int config_parse_ether_addr(
return log_oom();
r = parse_ether_addr(rvalue, n);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Not a valid MAC address, ignoring assignment: %s", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
free_and_replace(*hwaddr, n);
return 0;
return 1;
}
int config_parse_ether_addrs(
@@ -1894,7 +1847,7 @@ int config_parse_ether_addrs(
if (isempty(rvalue)) {
/* Empty assignment resets the list */
*hwaddrs = set_free(*hwaddrs);
return 0;
return 1;
}
for (const char *p = rvalue;;) {
@@ -1902,15 +1855,10 @@ int config_parse_ether_addrs(
_cleanup_free_ struct ether_addr *n = NULL;
r = extract_first_word(&p, &word, NULL, 0);
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
if (r == 0)
return 0;
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid syntax, ignoring: %s", rvalue);
return 0;
}
return 1;
n = new(struct ether_addr, 1);
if (!n)
@@ -1957,15 +1905,12 @@ int config_parse_in_addr_non_null(
*ipv4 = (struct in_addr) {};
else
*ipv6 = (struct in6_addr) {};
return 0;
return 1;
}
r = in_addr_from_string(ltype, rvalue, &a);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
if (!in_addr_is_set(ltype, &a)) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
@@ -1977,7 +1922,7 @@ int config_parse_in_addr_non_null(
*ipv4 = a.in;
else
*ipv6 = a.in6;
return 0;
return 1;
}
int config_parse_unsigned_bounded(
@@ -1986,8 +1931,8 @@ int config_parse_unsigned_bounded(
unsigned line,
const char *section,
unsigned section_line,
const char *name,
const char *value,
const char *lvalue,
const char *rvalue,
unsigned min,
unsigned max,
bool ignoring,
@@ -1996,26 +1941,21 @@ int config_parse_unsigned_bounded(
int r;
assert(filename);
assert(name);
assert(value);
assert(lvalue);
assert(rvalue);
assert(ret);
r = safe_atou_bounded(value, min, max, ret);
if (r == -ERANGE)
r = safe_atou_bounded(rvalue, min, max, ret);
if (r == -ERANGE) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Invalid '%s=%s', allowed range is %u..%u%s.",
name, value, min, max, ignoring ? ", ignoring" : "");
else if (r < 0)
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=%s'%s: %m",
name, value, ignoring ? ", ignoring" : "");
lvalue, rvalue, min, max, ignoring ? ", ignoring" : "");
return ignoring ? 0 : r;
}
if (r < 0)
return log_syntax_parse_error_full(unit, filename, line, r, /* critical = */ !ignoring, lvalue, rvalue);
if (r >= 0)
return 1; /* Return 1 if something was set */
else if (ignoring)
return 0;
else
return r;
return 1; /* Return 1 if something was set */
}
int config_parse_calendar(
@@ -2041,17 +1981,15 @@ int config_parse_calendar(
if (isempty(rvalue)) {
*cr = calendar_spec_free(*cr);
return 0;
return 1;
}
r = calendar_spec_from_string(rvalue, &c);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse calendar specification, ignoring: %s", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
free_and_replace_full(*cr, c, calendar_spec_free);
return 0;
return 1;
}
DEFINE_CONFIG_PARSE(config_parse_percent, parse_percent);
@@ -2079,17 +2017,18 @@ int config_parse_timezone(
if (isempty(rvalue)) {
*tz = mfree(*tz);
return 0;
return 1;
}
r = verify_timezone(rvalue, LOG_WARNING);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Timezone is not valid, ignoring assignment: %s", rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
return free_and_strdup_warn(tz, rvalue);
r = free_and_strdup_warn(tz, rvalue);
if (r < 0)
return r;
return 1;
}
int config_parse_ip_protocol(
@@ -2108,12 +2047,8 @@ int config_parse_ip_protocol(
int r;
r = isempty(rvalue) ? 0 : parse_ip_protocol_full(rvalue, /* relaxed= */ ltype);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to parse '%s=%s', ignoring: %m",
lvalue, rvalue);
return 0;
}
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
if (r > UINT8_MAX) {
/* linux/fib_rules.h and linux/fou.h define the netlink field as one byte, so we need to
+11 -11
View File
@@ -444,8 +444,8 @@ int config_parse_unsigned_bounded(
unsigned line,
const char *section,
unsigned section_line,
const char *name,
const char *value,
const char *lvalue,
const char *rvalue,
unsigned min,
unsigned max,
bool ignoring,
@@ -457,8 +457,8 @@ static inline int config_parse_uint32_bounded(
unsigned line,
const char *section,
unsigned section_line,
const char *name,
const char *value,
const char *lvalue,
const char *rvalue,
uint32_t min,
uint32_t max,
bool ignoring,
@@ -468,7 +468,7 @@ static inline int config_parse_uint32_bounded(
int r;
r = config_parse_unsigned_bounded(
unit, filename, line, section, section_line, name, value,
unit, filename, line, section, section_line, lvalue, rvalue,
min, max, ignoring,
&t);
if (r <= 0)
@@ -484,8 +484,8 @@ static inline int config_parse_uint16_bounded(
unsigned line,
const char *section,
unsigned section_line,
const char *name,
const char *value,
const char *lvalue,
const char *rvalue,
uint16_t min,
uint16_t max,
bool ignoring,
@@ -495,7 +495,7 @@ static inline int config_parse_uint16_bounded(
int r;
r = config_parse_unsigned_bounded(
unit, filename, line, section, section_line, name, value,
unit, filename, line, section, section_line, lvalue, rvalue,
min, max, ignoring,
&t);
if (r <= 0)
@@ -511,8 +511,8 @@ static inline int config_parse_uint8_bounded(
unsigned line,
const char *section,
unsigned section_line,
const char *name,
const char *value,
const char *lvalue,
const char *rvalue,
uint8_t min,
uint8_t max,
bool ignoring,
@@ -522,7 +522,7 @@ static inline int config_parse_uint8_bounded(
int r;
r = config_parse_unsigned_bounded(
unit, filename, line, section, section_line, name, value,
unit, filename, line, section, section_line, lvalue, rvalue,
min, max, ignoring,
&t);
if (r <= 0)