1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include <sys/socket.h>
10 #include "alloc-util.h"
11 #include "errno-list.h"
12 #include "extract-word.h"
13 #include "locale-util.h"
15 #include "missing_network.h"
16 #include "parse-util.h"
17 #include "process-util.h"
18 #include "stat-util.h"
19 #include "string-util.h"
22 int parse_boolean(const char *v
) {
47 int parse_pid(const char *s
, pid_t
* ret_pid
) {
55 r
= safe_atolu(s
, &ul
);
61 if ((unsigned long) pid
!= ul
)
64 if (!pid_is_valid(pid
))
71 int parse_mode(const char *s
, mode_t
*ret
) {
77 r
= safe_atou_full(s
, 8 |
78 SAFE_ATO_REFUSE_PLUS_MINUS
, /* Leading '+' or even '-' char? that's just weird,
79 * refuse. User might have wanted to add mode flags or
80 * so, but this parser doesn't allow that, so let's
93 int parse_ifindex(const char *s
) {
98 r
= safe_atoi(s
, &ifi
);
107 int parse_mtu(int family
, const char *s
, uint32_t *ret
) {
112 r
= parse_size(s
, 1024, &u
);
119 if (family
== AF_INET6
)
120 m
= IPV6_MIN_MTU
; /* This is 1280 */
122 m
= IPV4_MIN_MTU
; /* For all other protocols, including 'unspecified' we assume the IPv4 minimal MTU */
131 int parse_size(const char *t
, uint64_t base
, uint64_t *size
) {
133 /* Soo, sometimes we want to parse IEC binary suffixes, and
134 * sometimes SI decimal suffixes. This function can parse
135 * both. Which one is the right way depends on the
136 * context. Wikipedia suggests that SI is customary for
137 * hardware metrics and network speeds, while IEC is
138 * customary for most data sizes used by software and volatile
139 * (RAM) memory. Hence be careful which one you pick!
141 * In either case we use just K, M, G as suffix, and not Ki,
142 * Mi, Gi or so (as IEC would suggest). That's because that's
143 * frickin' ugly. But this means you really need to make sure
144 * to document which base you are parsing when you use this
149 unsigned long long factor
;
152 static const struct table iec
[] = {
153 { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
154 { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
155 { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
156 { "G", 1024ULL*1024ULL*1024ULL },
157 { "M", 1024ULL*1024ULL },
163 static const struct table si
[] = {
164 { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
165 { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
166 { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
167 { "G", 1000ULL*1000ULL*1000ULL },
168 { "M", 1000ULL*1000ULL },
174 const struct table
*table
;
176 unsigned long long r
= 0;
177 unsigned n_entries
, start_pos
= 0;
180 assert(IN_SET(base
, 1000, 1024));
185 n_entries
= ELEMENTSOF(si
);
188 n_entries
= ELEMENTSOF(iec
);
193 unsigned long long l
, tmp
;
198 p
+= strspn(p
, WHITESPACE
);
201 l
= strtoull(p
, &e
, 10);
212 /* strtoull() itself would accept space/+/- */
213 if (ascii_isdigit(*e
)) {
214 unsigned long long l2
;
217 l2
= strtoull(e
, &e2
, 10);
221 /* Ignore failure. E.g. 10.M is valid */
228 e
+= strspn(e
, WHITESPACE
);
230 for (i
= start_pos
; i
< n_entries
; i
++)
231 if (startswith(e
, table
[i
].suffix
))
237 if (l
+ (frac
> 0) > ULLONG_MAX
/ table
[i
].factor
)
240 tmp
= l
* table
[i
].factor
+ (unsigned long long) (frac
* table
[i
].factor
);
241 if (tmp
> ULLONG_MAX
- r
)
245 if ((unsigned long long) (uint64_t) r
!= r
)
248 p
= e
+ strlen(table
[i
].suffix
);
259 int parse_sector_size(const char *t
, uint64_t *ret
) {
267 r
= safe_atou64(t
, &ss
);
269 return log_error_errno(r
, "Failed to parse sector size parameter %s", t
);
270 if (ss
< 512 || ss
> 4096) /* Allow up to 4K due to dm-crypt support and 4K alignment by the homed LUKS backend */
271 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Sector size not between 512 and 4096: %s", t
);
273 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Sector size not power of 2: %s", t
);
279 int parse_range(const char *t
, unsigned *lower
, unsigned *upper
) {
280 _cleanup_free_
char *word
= NULL
;
287 /* Extract the lower bound. */
288 r
= extract_first_word(&t
, &word
, "-", EXTRACT_DONT_COALESCE_SEPARATORS
);
294 r
= safe_atou(word
, &l
);
298 /* Check for the upper bound and extract it if needed */
300 /* Single number with no dashes. */
303 /* Trailing dash is an error. */
306 r
= safe_atou(t
, &u
);
316 int parse_errno(const char *t
) {
321 r
= errno_from_name(t
);
325 r
= safe_atoi(t
, &e
);
329 /* 0 is also allowed here */
330 if (!errno_is_valid(e
) && e
!= 0)
336 static const char *mangle_base(const char *s
, unsigned *base
) {
342 /* Base already explicitly specified, then don't do anything. */
343 if (SAFE_ATO_MASK_FLAGS(*base
) != 0)
346 /* Support Python 3 style "0b" and 0x" prefixes, because they truly make sense, much more than C's "0" prefix for octal. */
347 k
= STARTSWITH_SET(s
, "0b", "0B");
349 *base
= 2 | (*base
& SAFE_ATO_ALL_FLAGS
);
353 k
= STARTSWITH_SET(s
, "0o", "0O");
355 *base
= 8 | (*base
& SAFE_ATO_ALL_FLAGS
);
362 int safe_atou_full(const char *s
, unsigned base
, unsigned *ret_u
) {
367 assert(SAFE_ATO_MASK_FLAGS(base
) <= 16);
369 /* strtoul() is happy to parse negative values, and silently converts them to unsigned values without
370 * generating an error. We want a clean error, hence let's look for the "-" prefix on our own, and
371 * generate an error. But let's do so only after strtoul() validated that the string is clean
372 * otherwise, so that we return EINVAL preferably over ERANGE. */
374 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_LEADING_WHITESPACE
) &&
375 strchr(WHITESPACE
, s
[0]))
378 s
+= strspn(s
, WHITESPACE
);
380 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_PLUS_MINUS
) &&
381 IN_SET(s
[0], '+', '-'))
382 return -EINVAL
; /* Note that we check the "-" prefix again a second time below, but return a
383 * different error. I.e. if the SAFE_ATO_REFUSE_PLUS_MINUS flag is set we
384 * blanket refuse +/- prefixed integers, while if it is missing we'll just
385 * return ERANGE, because the string actually parses correctly, but doesn't
386 * fit in the return type. */
388 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_LEADING_ZERO
) &&
389 s
[0] == '0' && !streq(s
, "0"))
390 return -EINVAL
; /* This is particularly useful to avoid ambiguities between C's octal
391 * notation and assumed-to-be-decimal integers with a leading zero. */
393 s
= mangle_base(s
, &base
);
396 l
= strtoul(s
, &x
, SAFE_ATO_MASK_FLAGS(base
) /* Let's mask off the flags bits so that only the actual
400 if (!x
|| x
== s
|| *x
!= 0)
402 if (l
!= 0 && s
[0] == '-')
404 if ((unsigned long) (unsigned) l
!= l
)
408 *ret_u
= (unsigned) l
;
413 int safe_atoi(const char *s
, int *ret_i
) {
420 s
+= strspn(s
, WHITESPACE
);
421 s
= mangle_base(s
, &base
);
424 l
= strtol(s
, &x
, base
);
427 if (!x
|| x
== s
|| *x
!= 0)
429 if ((long) (int) l
!= l
)
438 int safe_atollu_full(const char *s
, unsigned base
, unsigned long long *ret_llu
) {
440 unsigned long long l
;
443 assert(SAFE_ATO_MASK_FLAGS(base
) <= 16);
445 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_LEADING_WHITESPACE
) &&
446 strchr(WHITESPACE
, s
[0]))
449 s
+= strspn(s
, WHITESPACE
);
451 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_PLUS_MINUS
) &&
452 IN_SET(s
[0], '+', '-'))
455 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_LEADING_ZERO
) &&
456 s
[0] == '0' && s
[1] != 0)
459 s
= mangle_base(s
, &base
);
462 l
= strtoull(s
, &x
, SAFE_ATO_MASK_FLAGS(base
));
465 if (!x
|| x
== s
|| *x
!= 0)
467 if (l
!= 0 && s
[0] == '-')
476 int safe_atolli(const char *s
, long long int *ret_lli
) {
483 s
+= strspn(s
, WHITESPACE
);
484 s
= mangle_base(s
, &base
);
487 l
= strtoll(s
, &x
, base
);
490 if (!x
|| x
== s
|| *x
!= 0)
499 int safe_atou8_full(const char *s
, unsigned base
, uint8_t *ret
) {
503 r
= safe_atou_full(s
, base
, &u
);
513 int safe_atou16_full(const char *s
, unsigned base
, uint16_t *ret
) {
517 r
= safe_atou_full(s
, base
, &u
);
527 int safe_atoi16(const char *s
, int16_t *ret
) {
534 s
+= strspn(s
, WHITESPACE
);
535 s
= mangle_base(s
, &base
);
538 l
= strtol(s
, &x
, base
);
541 if (!x
|| x
== s
|| *x
!= 0)
543 if ((long) (int16_t) l
!= l
)
552 int safe_atod(const char *s
, double *ret_d
) {
553 _cleanup_(freelocalep
) locale_t loc
= (locale_t
) 0;
559 loc
= newlocale(LC_NUMERIC_MASK
, "C", (locale_t
) 0);
560 if (loc
== (locale_t
) 0)
564 d
= strtod_l(s
, &x
, loc
);
567 if (!x
|| x
== s
|| *x
!= 0)
576 int parse_fractional_part_u(const char **p
, size_t digits
, unsigned *res
) {
582 /* accept any number of digits, strtoull is limited to 19 */
583 for (size_t i
= 0; i
< digits
; i
++,s
++) {
584 if (!ascii_isdigit(*s
)) {
588 /* too few digits, pad with 0 */
589 for (; i
< digits
; i
++)
600 if (*s
>= '5' && *s
<= '9')
603 s
+= strspn(s
, DIGITS
);
611 int parse_nice(const char *p
, int *ret
) {
614 r
= safe_atoi(p
, &n
);
618 if (!nice_is_valid(n
))
625 int parse_ip_port(const char *s
, uint16_t *ret
) {
629 r
= safe_atou16_full(s
, SAFE_ATO_REFUSE_LEADING_WHITESPACE
, &l
);
641 int parse_ip_port_range(const char *s
, uint16_t *low
, uint16_t *high
) {
645 r
= parse_range(s
, &l
, &h
);
649 if (l
<= 0 || l
> 65535 || h
<= 0 || h
> 65535)
661 int parse_ip_prefix_length(const char *s
, int *ret
) {
665 r
= safe_atou(s
, &l
);
677 int parse_oom_score_adjust(const char *s
, int *ret
) {
683 r
= safe_atoi(s
, &v
);
687 if (!oom_score_adjust_is_valid(v
))
694 int store_loadavg_fixed_point(unsigned long i
, unsigned long f
, loadavg_t
*ret
) {
697 if (i
>= (~0UL << LOADAVG_PRECISION_BITS
))
700 i
= i
<< LOADAVG_PRECISION_BITS
;
701 f
= DIV_ROUND_UP((f
<< LOADAVG_PRECISION_BITS
), 100);
703 if (f
>= LOADAVG_FIXED_POINT_1_0
)
710 int parse_loadavg_fixed_point(const char *s
, loadavg_t
*ret
) {
711 const char *d
, *f_str
, *i_str
;
722 i_str
= strndupa_safe(s
, d
- s
);
725 r
= safe_atolu_full(i_str
, 10, &i
);
729 r
= safe_atolu_full(f_str
, 10, &f
);
733 return store_loadavg_fixed_point(i
, f
, ret
);