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_ifindex_or_ifname(const char *s
) {
101 r
= parse_ifindex(s
);
106 r
= (int) if_nametoindex(s
);
113 int parse_mtu(int family
, const char *s
, uint32_t *ret
) {
118 r
= parse_size(s
, 1024, &u
);
125 if (family
== AF_INET6
)
126 m
= IPV6_MIN_MTU
; /* This is 1280 */
128 m
= IPV4_MIN_MTU
; /* For all other protocols, including 'unspecified' we assume the IPv4 minimal MTU */
137 int parse_size(const char *t
, uint64_t base
, uint64_t *size
) {
139 /* Soo, sometimes we want to parse IEC binary suffixes, and
140 * sometimes SI decimal suffixes. This function can parse
141 * both. Which one is the right way depends on the
142 * context. Wikipedia suggests that SI is customary for
143 * hardware metrics and network speeds, while IEC is
144 * customary for most data sizes used by software and volatile
145 * (RAM) memory. Hence be careful which one you pick!
147 * In either case we use just K, M, G as suffix, and not Ki,
148 * Mi, Gi or so (as IEC would suggest). That's because that's
149 * frickin' ugly. But this means you really need to make sure
150 * to document which base you are parsing when you use this
155 unsigned long long factor
;
158 static const struct table iec
[] = {
159 { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
160 { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
161 { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
162 { "G", 1024ULL*1024ULL*1024ULL },
163 { "M", 1024ULL*1024ULL },
169 static const struct table si
[] = {
170 { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
171 { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
172 { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
173 { "G", 1000ULL*1000ULL*1000ULL },
174 { "M", 1000ULL*1000ULL },
180 const struct table
*table
;
182 unsigned long long r
= 0;
183 unsigned n_entries
, start_pos
= 0;
186 assert(IN_SET(base
, 1000, 1024));
191 n_entries
= ELEMENTSOF(si
);
194 n_entries
= ELEMENTSOF(iec
);
199 unsigned long long l
, tmp
;
204 p
+= strspn(p
, WHITESPACE
);
207 l
= strtoull(p
, &e
, 10);
218 /* strtoull() itself would accept space/+/- */
219 if (*e
>= '0' && *e
<= '9') {
220 unsigned long long l2
;
223 l2
= strtoull(e
, &e2
, 10);
227 /* Ignore failure. E.g. 10.M is valid */
234 e
+= strspn(e
, WHITESPACE
);
236 for (i
= start_pos
; i
< n_entries
; i
++)
237 if (startswith(e
, table
[i
].suffix
))
243 if (l
+ (frac
> 0) > ULLONG_MAX
/ table
[i
].factor
)
246 tmp
= l
* table
[i
].factor
+ (unsigned long long) (frac
* table
[i
].factor
);
247 if (tmp
> ULLONG_MAX
- r
)
251 if ((unsigned long long) (uint64_t) r
!= r
)
254 p
= e
+ strlen(table
[i
].suffix
);
265 int parse_range(const char *t
, unsigned *lower
, unsigned *upper
) {
266 _cleanup_free_
char *word
= NULL
;
273 /* Extract the lower bound. */
274 r
= extract_first_word(&t
, &word
, "-", EXTRACT_DONT_COALESCE_SEPARATORS
);
280 r
= safe_atou(word
, &l
);
284 /* Check for the upper bound and extract it if needed */
286 /* Single number with no dashes. */
289 /* Trailing dash is an error. */
292 r
= safe_atou(t
, &u
);
302 int parse_errno(const char *t
) {
307 r
= errno_from_name(t
);
311 r
= safe_atoi(t
, &e
);
315 /* 0 is also allowed here */
316 if (!errno_is_valid(e
) && e
!= 0)
322 int parse_syscall_and_errno(const char *in
, char **name
, int *error
) {
323 _cleanup_free_
char *n
= NULL
;
332 * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
333 * If errno is omitted, then error is set to -1.
334 * Empty syscall name is not allowed.
335 * Here, we do not check that the syscall name is valid or not.
340 e
= parse_errno(p
+ 1);
344 n
= strndup(in
, p
- in
);
360 int safe_atou_full(const char *s
, unsigned base
, unsigned *ret_u
) {
367 /* strtoul() is happy to parse negative values, and silently
368 * converts them to unsigned values without generating an
369 * error. We want a clean error, hence let's look for the "-"
370 * prefix on our own, and generate an error. But let's do so
371 * only after strtoul() validated that the string is clean
372 * otherwise, so that we return EINVAL preferably over
375 s
+= strspn(s
, WHITESPACE
);
378 l
= strtoul(s
, &x
, base
);
381 if (!x
|| x
== s
|| *x
!= 0)
385 if ((unsigned long) (unsigned) l
!= l
)
389 *ret_u
= (unsigned) l
;
394 int safe_atoi(const char *s
, int *ret_i
) {
401 l
= strtol(s
, &x
, 0);
404 if (!x
|| x
== s
|| *x
!= 0)
406 if ((long) (int) l
!= l
)
415 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
417 unsigned long long l
;
421 s
+= strspn(s
, WHITESPACE
);
424 l
= strtoull(s
, &x
, 0);
427 if (!x
|| x
== s
|| *x
!= 0)
438 int safe_atolli(const char *s
, long long int *ret_lli
) {
445 l
= strtoll(s
, &x
, 0);
448 if (!x
|| x
== s
|| *x
!= 0)
457 int safe_atou8(const char *s
, uint8_t *ret
) {
463 s
+= strspn(s
, WHITESPACE
);
466 l
= strtoul(s
, &x
, 0);
469 if (!x
|| x
== s
|| *x
!= 0)
473 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
) {
513 l
= strtol(s
, &x
, 0);
516 if (!x
|| x
== s
|| *x
!= 0)
518 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;
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)
551 int parse_fractional_part_u(const char **p
, size_t digits
, unsigned *res
) {
558 /* accept any number of digits, strtoull is limited to 19 */
559 for (i
=0; i
< digits
; i
++,s
++) {
560 if (*s
< '0' || *s
> '9') {
564 /* too few digits, pad with 0 */
565 for (; i
< digits
; i
++)
576 if (*s
>= '5' && *s
<= '9')
579 s
+= strspn(s
, DIGITS
);
587 int parse_percent_unbounded(const char *p
) {
591 pc
= endswith(p
, "%");
595 n
= strndupa(p
, pc
- p
);
596 r
= safe_atoi(n
, &v
);
605 int parse_percent(const char *p
) {
608 v
= parse_percent_unbounded(p
);
615 int parse_permille_unbounded(const char *p
) {
616 const char *pc
, *pm
, *dot
, *n
;
619 pm
= endswith(p
, "‰");
621 n
= strndupa(p
, pm
- p
);
622 r
= safe_atoi(n
, &v
);
628 pc
= endswith(p
, "%");
632 dot
= memchr(p
, '.', pc
- p
);
636 if (dot
[1] < '0' || dot
[1] > '9')
639 n
= strndupa(p
, dot
- p
);
642 n
= strndupa(p
, pc
- p
);
644 r
= safe_atoi(n
, &v
);
649 if (v
> (INT_MAX
- q
) / 10)
658 int parse_permille(const char *p
) {
661 v
= parse_permille_unbounded(p
);
668 int parse_nice(const char *p
, int *ret
) {
671 r
= safe_atoi(p
, &n
);
675 if (!nice_is_valid(n
))
682 int parse_ip_port(const char *s
, uint16_t *ret
) {
686 r
= safe_atou16(s
, &l
);
698 int parse_ip_port_range(const char *s
, uint16_t *low
, uint16_t *high
) {
702 r
= parse_range(s
, &l
, &h
);
706 if (l
<= 0 || l
> 65535 || h
<= 0 || h
> 65535)
718 int parse_dev(const char *s
, dev_t
*ret
) {
724 n
= strspn(s
, DIGITS
);
730 major
= strndupa(s
, n
);
731 r
= safe_atou(major
, &x
);
735 r
= safe_atou(s
+ n
+ 1, &y
);
739 if (!DEVICE_MAJOR_VALID(x
) || !DEVICE_MINOR_VALID(y
))
742 *ret
= makedev(x
, y
);
746 int parse_oom_score_adjust(const char *s
, int *ret
) {
752 r
= safe_atoi(s
, &v
);
756 if (v
< OOM_SCORE_ADJ_MIN
|| v
> OOM_SCORE_ADJ_MAX
)