1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include <sys/socket.h>
13 #include "alloc-util.h"
14 #include "errno-list.h"
15 #include "extract-word.h"
16 #include "locale-util.h"
18 #include "missing_network.h"
19 #include "parse-util.h"
20 #include "process-util.h"
21 #include "stat-util.h"
22 #include "string-util.h"
24 int parse_boolean(const char *v
) {
28 if (streq(v
, "1") || strcaseeq(v
, "yes") || strcaseeq(v
, "y") || strcaseeq(v
, "true") || strcaseeq(v
, "t") || strcaseeq(v
, "on"))
30 else if (streq(v
, "0") || strcaseeq(v
, "no") || strcaseeq(v
, "n") || strcaseeq(v
, "false") || strcaseeq(v
, "f") || strcaseeq(v
, "off"))
36 int parse_pid(const char *s
, pid_t
* ret_pid
) {
44 r
= safe_atolu(s
, &ul
);
50 if ((unsigned long) pid
!= ul
)
53 if (!pid_is_valid(pid
))
60 int parse_mode(const char *s
, mode_t
*ret
) {
67 s
+= strspn(s
, WHITESPACE
);
75 if (!x
|| x
== s
|| *x
!= 0)
77 if (l
< 0 || l
> 07777)
84 int parse_ifindex(const char *s
, int *ret
) {
90 r
= safe_atoi(s
, &ifi
);
100 int parse_ifindex_or_ifname(const char *s
, int *ret
) {
106 r
= parse_ifindex(s
, ret
);
110 r
= (int) if_nametoindex(s
);
118 int parse_mtu(int family
, const char *s
, uint32_t *ret
) {
123 r
= parse_size(s
, 1024, &u
);
130 if (family
== AF_INET6
)
131 m
= IPV6_MIN_MTU
; /* This is 1280 */
133 m
= IPV4_MIN_MTU
; /* For all other protocols, including 'unspecified' we assume the IPv4 minimal MTU */
142 int parse_size(const char *t
, uint64_t base
, uint64_t *size
) {
144 /* Soo, sometimes we want to parse IEC binary suffixes, and
145 * sometimes SI decimal suffixes. This function can parse
146 * both. Which one is the right way depends on the
147 * context. Wikipedia suggests that SI is customary for
148 * hardware metrics and network speeds, while IEC is
149 * customary for most data sizes used by software and volatile
150 * (RAM) memory. Hence be careful which one you pick!
152 * In either case we use just K, M, G as suffix, and not Ki,
153 * Mi, Gi or so (as IEC would suggest). That's because that's
154 * frickin' ugly. But this means you really need to make sure
155 * to document which base you are parsing when you use this
160 unsigned long long factor
;
163 static const struct table iec
[] = {
164 { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
165 { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
166 { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
167 { "G", 1024ULL*1024ULL*1024ULL },
168 { "M", 1024ULL*1024ULL },
174 static const struct table si
[] = {
175 { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
176 { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
177 { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
178 { "G", 1000ULL*1000ULL*1000ULL },
179 { "M", 1000ULL*1000ULL },
185 const struct table
*table
;
187 unsigned long long r
= 0;
188 unsigned n_entries
, start_pos
= 0;
191 assert(IN_SET(base
, 1000, 1024));
196 n_entries
= ELEMENTSOF(si
);
199 n_entries
= ELEMENTSOF(iec
);
204 unsigned long long l
, tmp
;
209 p
+= strspn(p
, WHITESPACE
);
212 l
= strtoull(p
, &e
, 10);
223 /* strtoull() itself would accept space/+/- */
224 if (*e
>= '0' && *e
<= '9') {
225 unsigned long long l2
;
228 l2
= strtoull(e
, &e2
, 10);
232 /* Ignore failure. E.g. 10.M is valid */
239 e
+= strspn(e
, WHITESPACE
);
241 for (i
= start_pos
; i
< n_entries
; i
++)
242 if (startswith(e
, table
[i
].suffix
))
248 if (l
+ (frac
> 0) > ULLONG_MAX
/ table
[i
].factor
)
251 tmp
= l
* table
[i
].factor
+ (unsigned long long) (frac
* table
[i
].factor
);
252 if (tmp
> ULLONG_MAX
- r
)
256 if ((unsigned long long) (uint64_t) r
!= r
)
259 p
= e
+ strlen(table
[i
].suffix
);
270 int parse_range(const char *t
, unsigned *lower
, unsigned *upper
) {
271 _cleanup_free_
char *word
= NULL
;
278 /* Extract the lower bound. */
279 r
= extract_first_word(&t
, &word
, "-", EXTRACT_DONT_COALESCE_SEPARATORS
);
285 r
= safe_atou(word
, &l
);
289 /* Check for the upper bound and extract it if needed */
291 /* Single number with no dashes. */
294 /* Trailing dash is an error. */
297 r
= safe_atou(t
, &u
);
307 int parse_errno(const char *t
) {
312 r
= errno_from_name(t
);
316 r
= safe_atoi(t
, &e
);
320 /* 0 is also allowed here */
321 if (!errno_is_valid(e
) && e
!= 0)
327 int parse_syscall_and_errno(const char *in
, char **name
, int *error
) {
328 _cleanup_free_
char *n
= NULL
;
337 * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
338 * If errno is omitted, then error is set to -1.
339 * Empty syscall name is not allowed.
340 * Here, we do not check that the syscall name is valid or not.
345 e
= parse_errno(p
+ 1);
349 n
= strndup(in
, p
- in
);
365 int safe_atou_full(const char *s
, unsigned base
, unsigned *ret_u
) {
373 /* strtoul() is happy to parse negative values, and silently
374 * converts them to unsigned values without generating an
375 * error. We want a clean error, hence let's look for the "-"
376 * prefix on our own, and generate an error. But let's do so
377 * only after strtoul() validated that the string is clean
378 * otherwise, so that we return EINVAL preferably over
381 s
+= strspn(s
, WHITESPACE
);
384 l
= strtoul(s
, &x
, base
);
387 if (!x
|| x
== s
|| *x
!= 0)
391 if ((unsigned long) (unsigned) l
!= l
)
394 *ret_u
= (unsigned) l
;
398 int safe_atoi(const char *s
, int *ret_i
) {
406 l
= strtol(s
, &x
, 0);
409 if (!x
|| x
== s
|| *x
!= 0)
411 if ((long) (int) l
!= l
)
418 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
420 unsigned long long l
;
425 s
+= strspn(s
, WHITESPACE
);
428 l
= strtoull(s
, &x
, 0);
431 if (!x
|| x
== s
|| *x
!= 0)
440 int safe_atolli(const char *s
, long long int *ret_lli
) {
448 l
= strtoll(s
, &x
, 0);
451 if (!x
|| x
== s
|| *x
!= 0)
458 int safe_atou8(const char *s
, uint8_t *ret
) {
465 s
+= strspn(s
, WHITESPACE
);
468 l
= strtoul(s
, &x
, 0);
471 if (!x
|| x
== s
|| *x
!= 0)
475 if ((unsigned long) (uint8_t) l
!= l
)
482 int safe_atou16_full(const char *s
, unsigned base
, uint16_t *ret
) {
490 s
+= strspn(s
, WHITESPACE
);
493 l
= strtoul(s
, &x
, base
);
496 if (!x
|| x
== s
|| *x
!= 0)
500 if ((unsigned long) (uint16_t) l
!= l
)
507 int safe_atoi16(const char *s
, int16_t *ret
) {
515 l
= strtol(s
, &x
, 0);
518 if (!x
|| x
== s
|| *x
!= 0)
520 if ((long) (int16_t) l
!= l
)
527 int safe_atod(const char *s
, double *ret_d
) {
528 _cleanup_(freelocalep
) locale_t loc
= (locale_t
) 0;
535 loc
= newlocale(LC_NUMERIC_MASK
, "C", (locale_t
) 0);
536 if (loc
== (locale_t
) 0)
540 d
= strtod_l(s
, &x
, loc
);
543 if (!x
|| x
== s
|| *x
!= 0)
550 int parse_fractional_part_u(const char **p
, size_t digits
, unsigned *res
) {
557 /* accept any number of digits, strtoull is limited to 19 */
558 for (i
=0; i
< digits
; i
++,s
++) {
559 if (*s
< '0' || *s
> '9') {
563 /* too few digits, pad with 0 */
564 for (; i
< digits
; i
++)
575 if (*s
>= '5' && *s
<= '9')
578 s
+= strspn(s
, DIGITS
);
586 int parse_percent_unbounded(const char *p
) {
590 pc
= endswith(p
, "%");
594 n
= strndupa(p
, pc
- p
);
595 r
= safe_atoi(n
, &v
);
604 int parse_percent(const char *p
) {
607 v
= parse_percent_unbounded(p
);
614 int parse_permille_unbounded(const char *p
) {
615 const char *pc
, *pm
, *dot
, *n
;
618 pm
= endswith(p
, "‰");
620 n
= strndupa(p
, pm
- p
);
621 r
= safe_atoi(n
, &v
);
627 pc
= endswith(p
, "%");
631 dot
= memchr(p
, '.', pc
- p
);
635 if (dot
[1] < '0' || dot
[1] > '9')
638 n
= strndupa(p
, dot
- p
);
641 n
= strndupa(p
, pc
- p
);
643 r
= safe_atoi(n
, &v
);
648 if (v
> (INT_MAX
- q
) / 10)
657 int parse_permille(const char *p
) {
660 v
= parse_permille_unbounded(p
);
667 int parse_nice(const char *p
, int *ret
) {
670 r
= safe_atoi(p
, &n
);
674 if (!nice_is_valid(n
))
681 int parse_ip_port(const char *s
, uint16_t *ret
) {
685 r
= safe_atou16(s
, &l
);
697 int parse_ip_port_range(const char *s
, uint16_t *low
, uint16_t *high
) {
701 r
= parse_range(s
, &l
, &h
);
705 if (l
<= 0 || l
> 65535 || h
<= 0 || h
> 65535)
717 int parse_dev(const char *s
, dev_t
*ret
) {
723 n
= strspn(s
, DIGITS
);
729 major
= strndupa(s
, n
);
730 r
= safe_atou(major
, &x
);
734 r
= safe_atou(s
+ n
+ 1, &y
);
738 if (!DEVICE_MAJOR_VALID(x
) || !DEVICE_MINOR_VALID(y
))
741 *ret
= makedev(x
, y
);
745 int parse_oom_score_adjust(const char *s
, int *ret
) {
751 r
= safe_atoi(s
, &v
);
755 if (v
< OOM_SCORE_ADJ_MIN
|| v
> OOM_SCORE_ADJ_MAX
)