HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/shared/conf-parser.c

70,576 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: a26d2f8dafed50116e75e7f1cf5c8ffb96e95212

Lines 2001–2316 of 2,316

        assert(rvalue);
 
        if (isempty(rvalue)) {
                /* Empty assignment resets the list */
                *hwaddrs = set_free(*hwaddrs);
                return 1;
        }
 
        for (const char *p = rvalue;;) {
                _cleanup_free_ char *word = NULL;
                _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 1;
 
                n = new(struct ether_addr, 1);
                if (!n)
                        return log_oom();
 
                r = parse_ether_addr(word, n);
                if (r < 0) {
                        log_syntax(unit, LOG_WARNING, filename, line, r,
                                   "Not a valid MAC address, ignoring: %s", word);
                        continue;
                }
 
                r = set_ensure_consume(hwaddrs, &ether_addr_hash_ops_free, TAKE_PTR(n));
                if (r < 0)
                        return log_oom();
        }
}
 
int config_parse_in_addr_non_null(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        /* data must be a pointer to struct in_addr or in6_addr, and the type is determined by ltype. */
        struct in_addr *ipv4 = ASSERT_PTR(data);
        struct in6_addr *ipv6 = ASSERT_PTR(data);
        union in_addr_union a;
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(IN_SET(ltype, AF_INET, AF_INET6));
 
        if (isempty(rvalue)) {
                if (ltype == AF_INET)
                        *ipv4 = (struct in_addr) {};
                else
                        *ipv6 = (struct in6_addr) {};
                return 1;
        }
 
        r = in_addr_from_string(ltype, rvalue, &a);
        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,
                           "%s= cannot be the ANY address, ignoring: %s", lvalue, rvalue);
                return 0;
        }
 
        if (ltype == AF_INET)
                *ipv4 = a.in;
        else
                *ipv6 = a.in6;
        return 1;
}
 
int config_parse_in_addr_data(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        struct in_addr_data *p = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
 
        if (isempty(rvalue)) {
                *p = (struct in_addr_data) {};
                return 1;
        }
 
        r = in_addr_from_string_auto(rvalue, &p->family, &p->address);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        return 1;
}
 
int config_parse_in_addr_prefix(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype, /* takes boolean, whether we warn about missing prefixlen */
                const char *rvalue,
                void *data,
                void *userdata) {
 
        struct in_addr_prefix *p = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
 
        if (isempty(rvalue)) {
                *p = (struct in_addr_prefix) {};
                return 1;
        }
 
        r = in_addr_prefix_from_string_auto_full(rvalue, ltype ? PREFIXLEN_REFUSE : PREFIXLEN_FULL, &p->family, &p->address, &p->prefixlen);
        if (r == -ENOANO) {
                r = in_addr_prefix_from_string_auto(rvalue, &p->family, &p->address, &p->prefixlen);
                if (r >= 0)
                        log_syntax(unit, LOG_WARNING, filename, line, r,
                                   "%s=%s is specified without prefix length. Assuming the prefix length is %u. "
                                   "Please specify the prefix length explicitly.", lvalue, rvalue, p->prefixlen);
        }
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        return 1;
}
 
int config_parse_unsigned_bounded(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                const char *rvalue,
                unsigned min,
                unsigned max,
                bool ignoring,
                unsigned *ret) {
 
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(ret);
 
        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.",
                           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);
 
        return 1;  /* Return 1 if something was set */
}
 
int config_parse_calendar(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        CalendarSpec **cr = data;
        _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL;
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);
 
        if (isempty(rvalue)) {
                *cr = calendar_spec_free(*cr);
                return 1;
        }
 
        r = calendar_spec_from_string(rvalue, &c);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        free_and_replace_full(*cr, c, calendar_spec_free);
        return 1;
}
 
DEFINE_CONFIG_PARSE(config_parse_percent, parse_percent);
DEFINE_CONFIG_PARSE(config_parse_permyriad, parse_permyriad);
DEFINE_CONFIG_PARSE_PTR(config_parse_sec_fix_0, parse_sec_fix_0, usec_t);
 
int config_parse_timezone(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        char **tz = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *tz = mfree(*tz);
                return 1;
        }
 
        r = verify_timezone(rvalue, LOG_WARNING);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        r = free_and_strdup_warn(tz, rvalue);
        if (r < 0)
                return r;
 
        return 1;
}
 
int config_parse_ip_protocol(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        uint8_t *proto = ASSERT_PTR(data);
        int r;
 
        r = isempty(rvalue) ? 0 : parse_ip_protocol_full(rvalue, /* relaxed= */ ltype);
        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
                 * reject protocols numbers that don't fit in one byte. */
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Invalid '%s=%s', allowed range is 0..255, ignoring.",
                           lvalue, rvalue);
                return 0;
        }
 
        *proto = r;
        return 1; /* done. */
}
 
int config_parse_loadavg(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        loadavg_t *i = ASSERT_PTR(data);
        int r;
 
        assert(rvalue);
 
        r = parse_permyriad(rvalue);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        r = store_loadavg_fixed_point(r / 100, r % 100, i);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        return 1; /* done. */
}