1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/socket.h>
12 #include "alloc-util.h"
13 #include "errno-list.h"
14 #include "extract-word.h"
15 #include "locale-util.h"
17 #include "missing_network.h"
18 #include "parse-util.h"
19 #include "process-util.h"
20 #include "stat-util.h"
21 #include "string-util.h"
23 int parse_boolean(const char *v
) {
27 if (streq(v
, "1") || strcaseeq(v
, "yes") || strcaseeq(v
, "y") || strcaseeq(v
, "true") || strcaseeq(v
, "t") || strcaseeq(v
, "on"))
29 else if (streq(v
, "0") || strcaseeq(v
, "no") || strcaseeq(v
, "n") || strcaseeq(v
, "false") || strcaseeq(v
, "f") || strcaseeq(v
, "off"))
35 int parse_pid(const char *s
, pid_t
* ret_pid
) {
43 r
= safe_atolu(s
, &ul
);
49 if ((unsigned long) pid
!= ul
)
52 if (!pid_is_valid(pid
))
59 int parse_mode(const char *s
, mode_t
*ret
) {
66 s
+= strspn(s
, WHITESPACE
);
74 if (!x
|| x
== s
|| *x
!= 0)
76 if (l
< 0 || l
> 07777)
83 int parse_ifindex(const char *s
, int *ret
) {
89 r
= safe_atoi(s
, &ifi
);
99 int parse_ifindex_or_ifname(const char *s
, int *ret
) {
105 r
= parse_ifindex(s
, ret
);
109 r
= (int) if_nametoindex(s
);
117 int parse_mtu(int family
, const char *s
, uint32_t *ret
) {
122 r
= parse_size(s
, 1024, &u
);
129 if (family
== AF_INET6
)
130 m
= IPV6_MIN_MTU
; /* This is 1280 */
132 m
= IPV4_MIN_MTU
; /* For all other protocols, including 'unspecified' we assume the IPv4 minimal MTU */
141 int parse_size(const char *t
, uint64_t base
, uint64_t *size
) {
143 /* Soo, sometimes we want to parse IEC binary suffixes, and
144 * sometimes SI decimal suffixes. This function can parse
145 * both. Which one is the right way depends on the
146 * context. Wikipedia suggests that SI is customary for
147 * hardware metrics and network speeds, while IEC is
148 * customary for most data sizes used by software and volatile
149 * (RAM) memory. Hence be careful which one you pick!
151 * In either case we use just K, M, G as suffix, and not Ki,
152 * Mi, Gi or so (as IEC would suggest). That's because that's
153 * frickin' ugly. But this means you really need to make sure
154 * to document which base you are parsing when you use this
159 unsigned long long factor
;
162 static const struct table iec
[] = {
163 { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
164 { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
165 { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
166 { "G", 1024ULL*1024ULL*1024ULL },
167 { "M", 1024ULL*1024ULL },
173 static const struct table si
[] = {
174 { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
175 { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
176 { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
177 { "G", 1000ULL*1000ULL*1000ULL },
178 { "M", 1000ULL*1000ULL },
184 const struct table
*table
;
186 unsigned long long r
= 0;
187 unsigned n_entries
, start_pos
= 0;
190 assert(IN_SET(base
, 1000, 1024));
195 n_entries
= ELEMENTSOF(si
);
198 n_entries
= ELEMENTSOF(iec
);
203 unsigned long long l
, tmp
;
208 p
+= strspn(p
, WHITESPACE
);
211 l
= strtoull(p
, &e
, 10);
222 /* strtoull() itself would accept space/+/- */
223 if (*e
>= '0' && *e
<= '9') {
224 unsigned long long l2
;
227 l2
= strtoull(e
, &e2
, 10);
231 /* Ignore failure. E.g. 10.M is valid */
238 e
+= strspn(e
, WHITESPACE
);
240 for (i
= start_pos
; i
< n_entries
; i
++)
241 if (startswith(e
, table
[i
].suffix
))
247 if (l
+ (frac
> 0) > ULLONG_MAX
/ table
[i
].factor
)
250 tmp
= l
* table
[i
].factor
+ (unsigned long long) (frac
* table
[i
].factor
);
251 if (tmp
> ULLONG_MAX
- r
)
255 if ((unsigned long long) (uint64_t) r
!= r
)
258 p
= e
+ strlen(table
[i
].suffix
);
269 int parse_range(const char *t
, unsigned *lower
, unsigned *upper
) {
270 _cleanup_free_
char *word
= NULL
;
277 /* Extract the lower bound. */
278 r
= extract_first_word(&t
, &word
, "-", EXTRACT_DONT_COALESCE_SEPARATORS
);
284 r
= safe_atou(word
, &l
);
288 /* Check for the upper bound and extract it if needed */
290 /* Single number with no dashes. */
293 /* Trailing dash is an error. */
296 r
= safe_atou(t
, &u
);
306 int parse_errno(const char *t
) {
311 r
= errno_from_name(t
);
315 r
= safe_atoi(t
, &e
);
319 /* 0 is also allowed here */
320 if (!errno_is_valid(e
) && e
!= 0)
326 int parse_syscall_and_errno(const char *in
, char **name
, int *error
) {
327 _cleanup_free_
char *n
= NULL
;
336 * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
337 * If errno is omitted, then error is set to -1.
338 * Empty syscall name is not allowed.
339 * Here, we do not check that the syscall name is valid or not.
344 e
= parse_errno(p
+ 1);
348 n
= strndup(in
, p
- in
);
364 int safe_atou_full(const char *s
, unsigned base
, unsigned *ret_u
) {
372 /* strtoul() is happy to parse negative values, and silently
373 * converts them to unsigned values without generating an
374 * error. We want a clean error, hence let's look for the "-"
375 * prefix on our own, and generate an error. But let's do so
376 * only after strtoul() validated that the string is clean
377 * otherwise, so that we return EINVAL preferably over
380 s
+= strspn(s
, WHITESPACE
);
383 l
= strtoul(s
, &x
, base
);
386 if (!x
|| x
== s
|| *x
!= 0)
390 if ((unsigned long) (unsigned) l
!= l
)
393 *ret_u
= (unsigned) l
;
397 int safe_atoi(const char *s
, int *ret_i
) {
405 l
= strtol(s
, &x
, 0);
408 if (!x
|| x
== s
|| *x
!= 0)
410 if ((long) (int) l
!= l
)
417 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
419 unsigned long long l
;
424 s
+= strspn(s
, WHITESPACE
);
427 l
= strtoull(s
, &x
, 0);
430 if (!x
|| x
== s
|| *x
!= 0)
439 int safe_atolli(const char *s
, long long int *ret_lli
) {
447 l
= strtoll(s
, &x
, 0);
450 if (!x
|| x
== s
|| *x
!= 0)
457 int safe_atou8(const char *s
, uint8_t *ret
) {
464 s
+= strspn(s
, WHITESPACE
);
467 l
= strtoul(s
, &x
, 0);
470 if (!x
|| x
== s
|| *x
!= 0)
474 if ((unsigned long) (uint8_t) l
!= l
)
481 int safe_atou16_full(const char *s
, unsigned base
, uint16_t *ret
) {
489 s
+= strspn(s
, WHITESPACE
);
492 l
= strtoul(s
, &x
, base
);
495 if (!x
|| x
== s
|| *x
!= 0)
499 if ((unsigned long) (uint16_t) l
!= l
)
506 int safe_atoi16(const char *s
, int16_t *ret
) {
514 l
= strtol(s
, &x
, 0);
517 if (!x
|| x
== s
|| *x
!= 0)
519 if ((long) (int16_t) l
!= l
)
526 int safe_atod(const char *s
, double *ret_d
) {
527 _cleanup_(freelocalep
) locale_t loc
= (locale_t
) 0;
534 loc
= newlocale(LC_NUMERIC_MASK
, "C", (locale_t
) 0);
535 if (loc
== (locale_t
) 0)
539 d
= strtod_l(s
, &x
, loc
);
542 if (!x
|| x
== s
|| *x
!= 0)
549 int parse_fractional_part_u(const char **p
, size_t digits
, unsigned *res
) {
556 /* accept any number of digits, strtoull is limited to 19 */
557 for (i
=0; i
< digits
; i
++,s
++) {
558 if (*s
< '0' || *s
> '9') {
562 /* too few digits, pad with 0 */
563 for (; i
< digits
; i
++)
574 if (*s
>= '5' && *s
<= '9')
577 s
+= strspn(s
, DIGITS
);
585 int parse_percent_unbounded(const char *p
) {
589 pc
= endswith(p
, "%");
593 n
= strndupa(p
, pc
- p
);
594 r
= safe_atoi(n
, &v
);
603 int parse_percent(const char *p
) {
606 v
= parse_percent_unbounded(p
);
613 int parse_permille_unbounded(const char *p
) {
614 const char *pc
, *pm
, *dot
, *n
;
617 pm
= endswith(p
, "‰");
619 n
= strndupa(p
, pm
- p
);
620 r
= safe_atoi(n
, &v
);
626 pc
= endswith(p
, "%");
630 dot
= memchr(p
, '.', pc
- p
);
634 if (dot
[1] < '0' || dot
[1] > '9')
637 n
= strndupa(p
, dot
- p
);
640 n
= strndupa(p
, pc
- p
);
642 r
= safe_atoi(n
, &v
);
647 if (v
> (INT_MAX
- q
) / 10)
656 int parse_permille(const char *p
) {
659 v
= parse_permille_unbounded(p
);
666 int parse_nice(const char *p
, int *ret
) {
669 r
= safe_atoi(p
, &n
);
673 if (!nice_is_valid(n
))
680 int parse_ip_port(const char *s
, uint16_t *ret
) {
684 r
= safe_atou16(s
, &l
);
696 int parse_ip_port_range(const char *s
, uint16_t *low
, uint16_t *high
) {
700 r
= parse_range(s
, &l
, &h
);
704 if (l
<= 0 || l
> 65535 || h
<= 0 || h
> 65535)
716 int parse_dev(const char *s
, dev_t
*ret
) {
722 n
= strspn(s
, DIGITS
);
728 major
= strndupa(s
, n
);
729 r
= safe_atou(major
, &x
);
733 r
= safe_atou(s
+ n
+ 1, &y
);
737 if (!DEVICE_MAJOR_VALID(x
) || !DEVICE_MINOR_VALID(y
))
740 *ret
= makedev(x
, y
);
744 int parse_oom_score_adjust(const char *s
, int *ret
) {
750 r
= safe_atoi(s
, &v
);
754 if (v
< OOM_SCORE_ADJ_MIN
|| v
> OOM_SCORE_ADJ_MAX
)