1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include <sys/socket.h>
11 #include "alloc-util.h"
12 #include "errno-list.h"
13 #include "extract-word.h"
14 #include "locale-util.h"
16 #include "missing_network.h"
17 #include "parse-util.h"
18 #include "process-util.h"
19 #include "stat-util.h"
20 #include "string-util.h"
22 int parse_boolean(const char *v
) {
26 if (streq(v
, "1") || strcaseeq(v
, "yes") || strcaseeq(v
, "y") || strcaseeq(v
, "true") || strcaseeq(v
, "t") || strcaseeq(v
, "on"))
28 else if (streq(v
, "0") || strcaseeq(v
, "no") || strcaseeq(v
, "n") || strcaseeq(v
, "false") || strcaseeq(v
, "f") || strcaseeq(v
, "off"))
34 int parse_pid(const char *s
, pid_t
* ret_pid
) {
42 r
= safe_atolu(s
, &ul
);
48 if ((unsigned long) pid
!= ul
)
51 if (!pid_is_valid(pid
))
58 int parse_mode(const char *s
, mode_t
*ret
) {
65 s
+= strspn(s
, WHITESPACE
);
73 if (!x
|| x
== s
|| *x
!= 0)
75 if (l
< 0 || l
> 07777)
82 int parse_ifindex(const char *s
) {
87 r
= safe_atoi(s
, &ifi
);
96 int parse_mtu(int family
, const char *s
, uint32_t *ret
) {
101 r
= parse_size(s
, 1024, &u
);
108 if (family
== AF_INET6
)
109 m
= IPV6_MIN_MTU
; /* This is 1280 */
111 m
= IPV4_MIN_MTU
; /* For all other protocols, including 'unspecified' we assume the IPv4 minimal MTU */
120 int parse_size(const char *t
, uint64_t base
, uint64_t *size
) {
122 /* Soo, sometimes we want to parse IEC binary suffixes, and
123 * sometimes SI decimal suffixes. This function can parse
124 * both. Which one is the right way depends on the
125 * context. Wikipedia suggests that SI is customary for
126 * hardware metrics and network speeds, while IEC is
127 * customary for most data sizes used by software and volatile
128 * (RAM) memory. Hence be careful which one you pick!
130 * In either case we use just K, M, G as suffix, and not Ki,
131 * Mi, Gi or so (as IEC would suggest). That's because that's
132 * frickin' ugly. But this means you really need to make sure
133 * to document which base you are parsing when you use this
138 unsigned long long factor
;
141 static const struct table iec
[] = {
142 { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
143 { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
144 { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
145 { "G", 1024ULL*1024ULL*1024ULL },
146 { "M", 1024ULL*1024ULL },
152 static const struct table si
[] = {
153 { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
154 { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
155 { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
156 { "G", 1000ULL*1000ULL*1000ULL },
157 { "M", 1000ULL*1000ULL },
163 const struct table
*table
;
165 unsigned long long r
= 0;
166 unsigned n_entries
, start_pos
= 0;
169 assert(IN_SET(base
, 1000, 1024));
174 n_entries
= ELEMENTSOF(si
);
177 n_entries
= ELEMENTSOF(iec
);
182 unsigned long long l
, tmp
;
187 p
+= strspn(p
, WHITESPACE
);
190 l
= strtoull(p
, &e
, 10);
201 /* strtoull() itself would accept space/+/- */
202 if (*e
>= '0' && *e
<= '9') {
203 unsigned long long l2
;
206 l2
= strtoull(e
, &e2
, 10);
210 /* Ignore failure. E.g. 10.M is valid */
217 e
+= strspn(e
, WHITESPACE
);
219 for (i
= start_pos
; i
< n_entries
; i
++)
220 if (startswith(e
, table
[i
].suffix
))
226 if (l
+ (frac
> 0) > ULLONG_MAX
/ table
[i
].factor
)
229 tmp
= l
* table
[i
].factor
+ (unsigned long long) (frac
* table
[i
].factor
);
230 if (tmp
> ULLONG_MAX
- r
)
234 if ((unsigned long long) (uint64_t) r
!= r
)
237 p
= e
+ strlen(table
[i
].suffix
);
248 int parse_range(const char *t
, unsigned *lower
, unsigned *upper
) {
249 _cleanup_free_
char *word
= NULL
;
256 /* Extract the lower bound. */
257 r
= extract_first_word(&t
, &word
, "-", EXTRACT_DONT_COALESCE_SEPARATORS
);
263 r
= safe_atou(word
, &l
);
267 /* Check for the upper bound and extract it if needed */
269 /* Single number with no dashes. */
272 /* Trailing dash is an error. */
275 r
= safe_atou(t
, &u
);
285 int parse_errno(const char *t
) {
290 r
= errno_from_name(t
);
294 r
= safe_atoi(t
, &e
);
298 /* 0 is also allowed here */
299 if (!errno_is_valid(e
) && e
!= 0)
305 int parse_syscall_and_errno(const char *in
, char **name
, int *error
) {
306 _cleanup_free_
char *n
= NULL
;
315 * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
316 * If errno is omitted, then error is set to -1.
317 * Empty syscall name is not allowed.
318 * Here, we do not check that the syscall name is valid or not.
323 e
= parse_errno(p
+ 1);
327 n
= strndup(in
, p
- in
);
343 int safe_atou_full(const char *s
, unsigned base
, unsigned *ret_u
) {
350 /* strtoul() is happy to parse negative values, and silently
351 * converts them to unsigned values without generating an
352 * error. We want a clean error, hence let's look for the "-"
353 * prefix on our own, and generate an error. But let's do so
354 * only after strtoul() validated that the string is clean
355 * otherwise, so that we return EINVAL preferably over
358 s
+= strspn(s
, WHITESPACE
);
361 l
= strtoul(s
, &x
, base
);
364 if (!x
|| x
== s
|| *x
!= 0)
368 if ((unsigned long) (unsigned) l
!= l
)
372 *ret_u
= (unsigned) l
;
377 int safe_atoi(const char *s
, int *ret_i
) {
384 l
= strtol(s
, &x
, 0);
387 if (!x
|| x
== s
|| *x
!= 0)
389 if ((long) (int) l
!= l
)
398 int safe_atollu_full(const char *s
, unsigned base
, long long unsigned *ret_llu
) {
400 unsigned long long l
;
404 s
+= strspn(s
, WHITESPACE
);
407 l
= strtoull(s
, &x
, base
);
410 if (!x
|| x
== s
|| *x
!= 0)
421 int safe_atolli(const char *s
, long long int *ret_lli
) {
428 l
= strtoll(s
, &x
, 0);
431 if (!x
|| x
== s
|| *x
!= 0)
440 int safe_atou8(const char *s
, uint8_t *ret
) {
446 s
+= strspn(s
, WHITESPACE
);
449 l
= strtoul(s
, &x
, 0);
452 if (!x
|| x
== s
|| *x
!= 0)
456 if ((unsigned long) (uint8_t) l
!= l
)
464 int safe_atou16_full(const char *s
, unsigned base
, uint16_t *ret
) {
472 s
+= strspn(s
, WHITESPACE
);
475 l
= strtoul(s
, &x
, base
);
478 if (!x
|| x
== s
|| *x
!= 0)
482 if ((unsigned long) (uint16_t) l
!= l
)
489 int safe_atoi16(const char *s
, int16_t *ret
) {
496 l
= strtol(s
, &x
, 0);
499 if (!x
|| x
== s
|| *x
!= 0)
501 if ((long) (int16_t) l
!= l
)
510 int safe_atod(const char *s
, double *ret_d
) {
511 _cleanup_(freelocalep
) locale_t loc
= (locale_t
) 0;
517 loc
= newlocale(LC_NUMERIC_MASK
, "C", (locale_t
) 0);
518 if (loc
== (locale_t
) 0)
522 d
= strtod_l(s
, &x
, loc
);
525 if (!x
|| x
== s
|| *x
!= 0)
534 int parse_fractional_part_u(const char **p
, size_t digits
, unsigned *res
) {
541 /* accept any number of digits, strtoull is limited to 19 */
542 for (i
=0; i
< digits
; i
++,s
++) {
543 if (*s
< '0' || *s
> '9') {
547 /* too few digits, pad with 0 */
548 for (; i
< digits
; i
++)
559 if (*s
>= '5' && *s
<= '9')
562 s
+= strspn(s
, DIGITS
);
570 int parse_percent_unbounded(const char *p
) {
574 pc
= endswith(p
, "%");
578 n
= strndupa(p
, pc
- p
);
579 r
= safe_atoi(n
, &v
);
588 int parse_percent(const char *p
) {
591 v
= parse_percent_unbounded(p
);
598 int parse_permille_unbounded(const char *p
) {
599 const char *pc
, *pm
, *dot
, *n
;
602 pm
= endswith(p
, "‰");
604 n
= strndupa(p
, pm
- p
);
605 r
= safe_atoi(n
, &v
);
611 pc
= endswith(p
, "%");
615 dot
= memchr(p
, '.', pc
- p
);
619 if (dot
[1] < '0' || dot
[1] > '9')
622 n
= strndupa(p
, dot
- p
);
625 n
= strndupa(p
, pc
- p
);
627 r
= safe_atoi(n
, &v
);
632 if (v
> (INT_MAX
- q
) / 10)
641 int parse_permille(const char *p
) {
644 v
= parse_permille_unbounded(p
);
651 int parse_nice(const char *p
, int *ret
) {
654 r
= safe_atoi(p
, &n
);
658 if (!nice_is_valid(n
))
665 int parse_ip_port(const char *s
, uint16_t *ret
) {
669 r
= safe_atou16(s
, &l
);
681 int parse_ip_port_range(const char *s
, uint16_t *low
, uint16_t *high
) {
685 r
= parse_range(s
, &l
, &h
);
689 if (l
<= 0 || l
> 65535 || h
<= 0 || h
> 65535)
701 int parse_ip_prefix_length(const char *s
, int *ret
) {
705 r
= safe_atou(s
, &l
);
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
)