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_range(const char *t
, unsigned *lower
, unsigned *upper
) {
260 _cleanup_free_
char *word
= NULL
;
267 /* Extract the lower bound. */
268 r
= extract_first_word(&t
, &word
, "-", EXTRACT_DONT_COALESCE_SEPARATORS
);
274 r
= safe_atou(word
, &l
);
278 /* Check for the upper bound and extract it if needed */
280 /* Single number with no dashes. */
283 /* Trailing dash is an error. */
286 r
= safe_atou(t
, &u
);
296 int parse_errno(const char *t
) {
301 r
= errno_from_name(t
);
305 r
= safe_atoi(t
, &e
);
309 /* 0 is also allowed here */
310 if (!errno_is_valid(e
) && e
!= 0)
316 static const char *mangle_base(const char *s
, unsigned *base
) {
322 /* Base already explicitly specified, then don't do anything. */
323 if (SAFE_ATO_MASK_FLAGS(*base
) != 0)
326 /* Support Python 3 style "0b" and 0x" prefixes, because they truly make sense, much more than C's "0" prefix for octal. */
327 k
= STARTSWITH_SET(s
, "0b", "0B");
329 *base
= 2 | (*base
& SAFE_ATO_ALL_FLAGS
);
333 k
= STARTSWITH_SET(s
, "0o", "0O");
335 *base
= 8 | (*base
& SAFE_ATO_ALL_FLAGS
);
342 int safe_atou_full(const char *s
, unsigned base
, unsigned *ret_u
) {
347 assert(SAFE_ATO_MASK_FLAGS(base
) <= 16);
349 /* strtoul() is happy to parse negative values, and silently converts them to unsigned values without
350 * generating an error. We want a clean error, hence let's look for the "-" prefix on our own, and
351 * generate an error. But let's do so only after strtoul() validated that the string is clean
352 * otherwise, so that we return EINVAL preferably over ERANGE. */
354 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_LEADING_WHITESPACE
) &&
355 strchr(WHITESPACE
, s
[0]))
358 s
+= strspn(s
, WHITESPACE
);
360 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_PLUS_MINUS
) &&
361 IN_SET(s
[0], '+', '-'))
362 return -EINVAL
; /* Note that we check the "-" prefix again a second time below, but return a
363 * different error. I.e. if the SAFE_ATO_REFUSE_PLUS_MINUS flag is set we
364 * blanket refuse +/- prefixed integers, while if it is missing we'll just
365 * return ERANGE, because the string actually parses correctly, but doesn't
366 * fit in the return type. */
368 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_LEADING_ZERO
) &&
369 s
[0] == '0' && !streq(s
, "0"))
370 return -EINVAL
; /* This is particularly useful to avoid ambiguities between C's octal
371 * notation and assumed-to-be-decimal integers with a leading zero. */
373 s
= mangle_base(s
, &base
);
376 l
= strtoul(s
, &x
, SAFE_ATO_MASK_FLAGS(base
) /* Let's mask off the flags bits so that only the actual
380 if (!x
|| x
== s
|| *x
!= 0)
382 if (l
!= 0 && s
[0] == '-')
384 if ((unsigned long) (unsigned) l
!= l
)
388 *ret_u
= (unsigned) l
;
393 int safe_atoi(const char *s
, int *ret_i
) {
400 s
+= strspn(s
, WHITESPACE
);
401 s
= mangle_base(s
, &base
);
404 l
= strtol(s
, &x
, base
);
407 if (!x
|| x
== s
|| *x
!= 0)
409 if ((long) (int) l
!= l
)
418 int safe_atollu_full(const char *s
, unsigned base
, unsigned long long *ret_llu
) {
420 unsigned long long l
;
423 assert(SAFE_ATO_MASK_FLAGS(base
) <= 16);
425 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_LEADING_WHITESPACE
) &&
426 strchr(WHITESPACE
, s
[0]))
429 s
+= strspn(s
, WHITESPACE
);
431 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_PLUS_MINUS
) &&
432 IN_SET(s
[0], '+', '-'))
435 if (FLAGS_SET(base
, SAFE_ATO_REFUSE_LEADING_ZERO
) &&
436 s
[0] == '0' && s
[1] != 0)
439 s
= mangle_base(s
, &base
);
442 l
= strtoull(s
, &x
, SAFE_ATO_MASK_FLAGS(base
));
445 if (!x
|| x
== s
|| *x
!= 0)
447 if (l
!= 0 && s
[0] == '-')
456 int safe_atolli(const char *s
, long long int *ret_lli
) {
463 s
+= strspn(s
, WHITESPACE
);
464 s
= mangle_base(s
, &base
);
467 l
= strtoll(s
, &x
, base
);
470 if (!x
|| x
== s
|| *x
!= 0)
479 int safe_atou8_full(const char *s
, unsigned base
, uint8_t *ret
) {
483 r
= safe_atou_full(s
, base
, &u
);
493 int safe_atou16_full(const char *s
, unsigned base
, uint16_t *ret
) {
497 r
= safe_atou_full(s
, base
, &u
);
507 int safe_atoi16(const char *s
, int16_t *ret
) {
514 s
+= strspn(s
, WHITESPACE
);
515 s
= mangle_base(s
, &base
);
518 l
= strtol(s
, &x
, base
);
521 if (!x
|| x
== s
|| *x
!= 0)
523 if ((long) (int16_t) l
!= l
)
532 int safe_atod(const char *s
, double *ret_d
) {
533 _cleanup_(freelocalep
) locale_t loc
= (locale_t
) 0;
539 loc
= newlocale(LC_NUMERIC_MASK
, "C", (locale_t
) 0);
540 if (loc
== (locale_t
) 0)
544 d
= strtod_l(s
, &x
, loc
);
547 if (!x
|| x
== s
|| *x
!= 0)
556 int parse_fractional_part_u(const char **p
, size_t digits
, unsigned *res
) {
562 /* accept any number of digits, strtoull is limited to 19 */
563 for (size_t i
= 0; i
< digits
; i
++,s
++) {
564 if (!ascii_isdigit(*s
)) {
568 /* too few digits, pad with 0 */
569 for (; i
< digits
; i
++)
580 if (*s
>= '5' && *s
<= '9')
583 s
+= strspn(s
, DIGITS
);
591 int parse_nice(const char *p
, int *ret
) {
594 r
= safe_atoi(p
, &n
);
598 if (!nice_is_valid(n
))
605 int parse_ip_port(const char *s
, uint16_t *ret
) {
609 r
= safe_atou16_full(s
, SAFE_ATO_REFUSE_LEADING_WHITESPACE
, &l
);
621 int parse_ip_port_range(const char *s
, uint16_t *low
, uint16_t *high
) {
625 r
= parse_range(s
, &l
, &h
);
629 if (l
<= 0 || l
> 65535 || h
<= 0 || h
> 65535)
641 int parse_ip_prefix_length(const char *s
, int *ret
) {
645 r
= safe_atou(s
, &l
);
657 int parse_oom_score_adjust(const char *s
, int *ret
) {
663 r
= safe_atoi(s
, &v
);
667 if (!oom_score_adjust_is_valid(v
))
674 int store_loadavg_fixed_point(unsigned long i
, unsigned long f
, loadavg_t
*ret
) {
677 if (i
>= (~0UL << LOADAVG_PRECISION_BITS
))
680 i
= i
<< LOADAVG_PRECISION_BITS
;
681 f
= DIV_ROUND_UP((f
<< LOADAVG_PRECISION_BITS
), 100);
683 if (f
>= LOADAVG_FIXED_POINT_1_0
)
690 int parse_loadavg_fixed_point(const char *s
, loadavg_t
*ret
) {
691 const char *d
, *f_str
, *i_str
;
702 i_str
= strndupa_safe(s
, d
- s
);
705 r
= safe_atolu_full(i_str
, 10, &i
);
709 r
= safe_atolu_full(f_str
, 10, &f
);
713 return store_loadavg_fixed_point(i
, f
, ret
);