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 1001–2000 of 2,316

        return 1;
}
 
int config_parse_iec_size_long(
                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) {
 
        long *sz = ASSERT_PTR(data);
        uint64_t v;
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        r = parse_size(rvalue, 1024, &v);
        if (r >= 0 && v > LONG_MAX)
                r = -ERANGE;
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        *sz = (long) v;
        return 1;
}
 
int config_parse_si_uint64(
                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) {
 
        uint64_t *sz = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        r = parse_size(rvalue, 1000, sz);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        return 1;
}
 
int config_parse_iec_uint64(
                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) {
 
        uint64_t *bytes = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        r = parse_size(rvalue, 1024, bytes);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        return 1;
}
 
int config_parse_iec_uint64_infinity(
                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) {
 
        uint64_t *bytes = ASSERT_PTR(data);
 
        assert(rvalue);
 
        if (streq(rvalue, "infinity")) {
                *bytes = UINT64_MAX;
                return 1;
        }
 
        return config_parse_iec_uint64(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata);
}
 
int config_parse_bool(
                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) {
 
        bool *b = ASSERT_PTR(data);
        bool fatal = ltype;
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(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 = r;
        return 1; /* set */
}
 
int config_parse_uint32_flag(
                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) {
 
        uint32_t *flags = ASSERT_PTR(data);
        int r;
 
        assert(ltype != 0);
 
        r = isempty(rvalue) ? 0 : parse_boolean(rvalue);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        SET_FLAG(*flags, ltype, r);
        return 1;
}
 
int config_parse_uint32_invert_flag(
                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) {
 
        uint32_t *flags = ASSERT_PTR(data);
        int r;
 
        assert(ltype != 0);
 
        r = isempty(rvalue) ? 0 : parse_boolean(rvalue);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        SET_FLAG(*flags, ltype, !r);
        return 1;
}
 
int config_parse_id128(
                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) {
 
        sd_id128_t *result = data;
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        r = id128_from_string_nonzero(rvalue, result);
        if (r == -ENXIO) {
                log_syntax(unit, LOG_WARNING, filename, line, r, "128-bit ID/UUID is all 0, ignoring: %s", rvalue);
                return 0;
        }
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        return 1;
}
 
int config_parse_tristate(
                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) {
 
        int r, *t = ASSERT_PTR(data);
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        /* A tristate is pretty much a boolean, except that it can also take an empty string,
         * indicating "uninitialized", much like NULL is for a pointer type. */
 
        if (isempty(rvalue)) {
                *t = -1;
                return 1;
        }
 
        r = parse_tristate(rvalue, t);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        return 1;
}
 
int config_parse_string(
                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 **s = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *s = mfree(*s);
                return 1;
        }
 
        if (FLAGS_SET(ltype, CONFIG_PARSE_STRING_SAFE) && !string_is_safe(rvalue, STRING_ALLOW_GLOBS)) {
                _cleanup_free_ char *escaped = NULL;
 
                escaped = cescape(rvalue);
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Specified string contains unsafe characters, ignoring: %s", strna(escaped));
                return 0;
        }
 
        if (FLAGS_SET(ltype, CONFIG_PARSE_STRING_ASCII) && !ascii_is_valid(rvalue)) {
                _cleanup_free_ char *escaped = NULL;
 
                escaped = cescape(rvalue);
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Specified string contains invalid ASCII characters, ignoring: %s", strna(escaped));
                return 0;
        }
 
        r = free_and_strdup_warn(s, rvalue);
        if (r < 0)
                return r;
 
        return 1;
}
 
int config_parse_dns_name(
                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 **hostname = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *hostname = mfree(*hostname);
                return 1;
        }
 
        r = dns_name_is_valid(rvalue);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to check validity of DNS domain name '%s', ignoring assignment: %m", rvalue);
                return 0;
        }
        if (r == 0) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Specified invalid DNS domain name, ignoring assignment: %s", rvalue);
                return 0;
        }
 
        r = free_and_strdup_warn(hostname, rvalue);
        if (r < 0)
                return r;
 
        return 1;
}
 
int config_parse_hostname(
                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 **hostname = ASSERT_PTR(data);
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *hostname = mfree(*hostname);
                return 1;
        }
 
        if (!hostname_is_valid(rvalue, 0)) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Specified invalid hostname, ignoring assignment: %s", rvalue);
                return 0;
        }
 
        return config_parse_dns_name(unit, filename, line, section, section_line,
                                     lvalue, ltype, rvalue, data, userdata);
}
 
int config_parse_path(
                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) {
 
        _cleanup_free_ char *n = NULL;
        bool fatal = ltype;
        char **s = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *s = mfree(*s);
                return 1;
        }
 
        n = strdup(rvalue);
        if (!n)
                return log_oom();
 
        r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue);
        if (r < 0)
                return fatal ? -ENOEXEC : 0;
 
        free_and_replace(*s, n);
        return 1;
}
 
int config_parse_strv(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype, /* When true, duplicated entries will be filtered. */
                const char *rvalue,
                void *data,
                void *userdata) {
 
        char ***sv = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *sv = strv_free(*sv);
                return 1;
        }
 
        _cleanup_strv_free_ char **strv = NULL;
        for (const char *p = rvalue;;) {
                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;
 
                r = strv_consume(&strv, word);
                if (r < 0)
                        return log_oom();
        }
 
        r = strv_extend_strv_consume(sv, TAKE_PTR(strv), /* filter_duplicates= */ ltype);
        if (r < 0)
                return log_oom();
 
        return 1;
}
 
int config_parse_warn_compat(
                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) {
 
        Disabled reason = ltype;
 
        switch (reason) {
 
        case DISABLED_CONFIGURATION:
                log_syntax(unit, LOG_DEBUG, filename, line, 0,
                           "Support for option %s= has been disabled at compile time and it is ignored", lvalue);
                break;
 
        case DISABLED_LEGACY:
                log_syntax(unit, LOG_INFO, filename, line, 0,
                           "Support for option %s= has been removed and it is ignored", lvalue);
                break;
 
        case DISABLED_EXPERIMENTAL:
                log_syntax(unit, LOG_INFO, filename, line, 0,
                           "Support for option %s= has not yet been enabled and it is ignored", lvalue);
                break;
        }
 
        return 0;
}
 
int config_parse_log_facility(
                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) {
 
        int *o = data, x;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);
 
        x = log_facility_unshifted_from_string(rvalue);
        if (x < 0)
                return log_syntax_parse_error(unit, filename, line, x, lvalue, rvalue);
 
        *o = (x << 3) | LOG_PRI(*o);
 
        return 1;
}
 
int config_parse_log_level(
                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) {
 
        int *o = data, x;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);
 
        x = log_level_from_string(rvalue);
        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 1;
}
 
int config_parse_signal(
                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) {
 
        int *sig = data, r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(sig);
 
        r = signal_from_string(rvalue);
        if (r <= 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        *sig = r;
        return 1;
}
 
int config_parse_personality(
                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) {
 
        unsigned long *personality = data, p;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(personality);
 
        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 1;
}
 
int config_parse_ifname(
                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 **s = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *s = mfree(*s);
                return 1;
        }
 
        if (!ifname_valid(rvalue)) {
                log_syntax(unit, LOG_WARNING, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
                return 0;
        }
 
        r = free_and_strdup_warn(s, rvalue);
        if (r < 0)
                return r;
 
        return 1;
}
 
int config_parse_ifnames(
                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) {
 
        _cleanup_strv_free_ char **names = NULL;
        char ***s = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *s = strv_free(*s);
                return 1;
        }
 
        for (const char *p = rvalue;;) {
                _cleanup_free_ char *word = 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)
                        break;
 
                if (!ifname_valid_full(word, ltype)) {
                        log_syntax(unit, LOG_WARNING, filename, line, 0,
                                   "Interface name is not valid or too long, ignoring assignment: %s",
                                   word);
                        continue;
                }
 
                r = strv_consume(&names, TAKE_PTR(word));
                if (r < 0)
                        return log_oom();
        }
 
        r = strv_extend_strv(s, names, true);
        if (r < 0)
                return log_oom();
 
        return 1;
}
 
int config_parse_ip_port(
                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) {
 
        uint16_t *s = ASSERT_PTR(data);
        uint16_t port;
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *s = 0;
                return 1;
        }
 
        r = parse_ip_port(rvalue, &port);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        *s = port;
        return 1;
}
 
int config_parse_mtu(
                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) {
 
        uint32_t *mtu = ASSERT_PTR(data);
        int r;
 
        assert(rvalue);
 
        r = parse_mtu(ltype, rvalue, mtu);
        if (r == -ERANGE) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s",
                           (uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX,
                           rvalue);
                return 0;
        }
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        return 1;
}
 
int config_parse_rlimit(
                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 rlimit **rl = data, d = {};
        int r;
 
        assert(rvalue);
        assert(rl);
 
        r = rlimit_parse(ltype, rvalue, &d);
        if (r == -EILSEQ) {
                log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, 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;
        else {
                rl[ltype] = newdup(struct rlimit, &d, 1);
                if (!rl[ltype])
                        return log_oom();
        }
 
        return 1;
}
 
int config_parse_permille(
                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) {
 
        unsigned *permille = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        r = parse_permille(rvalue);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        *permille = (unsigned) r;
        return 1;
}
 
int config_parse_vlanprotocol(
                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) {
 
        int *vlan_protocol = data;
 
        assert(filename);
        assert(lvalue);
 
        if (isempty(rvalue)) {
                *vlan_protocol = -1;
                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
                return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue);
 
        return 1;
}
 
int config_parse_hw_addr(
                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 hw_addr_data *hwaddr = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *hwaddr = HW_ADDR_NULL;
                return 1;
        }
 
        r = parse_hw_addr_full(rvalue, ltype, hwaddr);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        return 1;
}
 
int config_parse_hw_addrs(
                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) {
 
        Set **hwaddrs = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        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 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 1;
 
                n = new(struct hw_addr_data, 1);
                if (!n)
                        return log_oom();
 
                r = parse_hw_addr_full(word, ltype, n);
                if (r < 0) {
                        log_syntax(unit, LOG_WARNING, filename, line, r,
                                   "Not a valid hardware address, ignoring: %s", word);
                        continue;
                }
 
                r = set_ensure_consume(hwaddrs, &hw_addr_hash_ops_free, TAKE_PTR(n));
                if (r < 0)
                        return log_oom();
        }
}
 
int config_parse_ether_addr(
                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) {
 
        _cleanup_free_ struct ether_addr *n = NULL;
        struct ether_addr **hwaddr = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);
        assert(rvalue);
 
        if (isempty(rvalue)) {
                *hwaddr = mfree(*hwaddr);
                return 1;
        }
 
        n = new0(struct ether_addr, 1);
        if (!n)
                return log_oom();
 
        r = parse_ether_addr(rvalue, n);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        free_and_replace(*hwaddr, n);
        return 1;
}
 
int config_parse_ether_addrs(
                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) {
 
        Set **hwaddrs = ASSERT_PTR(data);
        int r;
 
        assert(filename);
        assert(lvalue);