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"
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
) {
86 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 char *format_bytes(char *buf
, size_t l
, uint64_t t
) {
346 /* This only does IEC units so far */
348 static const struct {
352 { "E", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
353 { "P", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
354 { "T", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
355 { "G", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
356 { "M", UINT64_C(1024)*UINT64_C(1024) },
357 { "K", UINT64_C(1024) },
360 if (t
== (uint64_t) -1)
363 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
365 if (t
>= table
[i
].factor
) {
367 "%" PRIu64
".%" PRIu64
"%s",
369 ((t
*UINT64_C(10)) / table
[i
].factor
) % UINT64_C(10),
376 snprintf(buf
, l
, "%" PRIu64
"B", t
);
384 int safe_atou_full(const char *s
, unsigned base
, unsigned *ret_u
) {
392 /* strtoul() is happy to parse negative values, and silently
393 * converts them to unsigned values without generating an
394 * error. We want a clean error, hence let's look for the "-"
395 * prefix on our own, and generate an error. But let's do so
396 * only after strtoul() validated that the string is clean
397 * otherwise, so that we return EINVAL preferably over
400 s
+= strspn(s
, WHITESPACE
);
403 l
= strtoul(s
, &x
, base
);
406 if (!x
|| x
== s
|| *x
!= 0)
410 if ((unsigned long) (unsigned) l
!= l
)
413 *ret_u
= (unsigned) l
;
417 int safe_atoi(const char *s
, int *ret_i
) {
425 l
= strtol(s
, &x
, 0);
428 if (!x
|| x
== s
|| *x
!= 0)
430 if ((long) (int) l
!= l
)
437 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
439 unsigned long long l
;
444 s
+= strspn(s
, WHITESPACE
);
447 l
= strtoull(s
, &x
, 0);
450 if (!x
|| x
== s
|| *x
!= 0)
459 int safe_atolli(const char *s
, long long int *ret_lli
) {
467 l
= strtoll(s
, &x
, 0);
470 if (!x
|| x
== s
|| *x
!= 0)
477 int safe_atou8(const char *s
, uint8_t *ret
) {
484 s
+= strspn(s
, WHITESPACE
);
487 l
= strtoul(s
, &x
, 0);
490 if (!x
|| x
== s
|| *x
!= 0)
494 if ((unsigned long) (uint8_t) l
!= l
)
501 int safe_atou16_full(const char *s
, unsigned base
, uint16_t *ret
) {
509 s
+= strspn(s
, WHITESPACE
);
512 l
= strtoul(s
, &x
, base
);
515 if (!x
|| x
== s
|| *x
!= 0)
519 if ((unsigned long) (uint16_t) l
!= l
)
526 int safe_atoi16(const char *s
, int16_t *ret
) {
534 l
= strtol(s
, &x
, 0);
537 if (!x
|| x
== s
|| *x
!= 0)
539 if ((long) (int16_t) l
!= l
)
546 int safe_atod(const char *s
, double *ret_d
) {
547 _cleanup_(freelocalep
) locale_t loc
= (locale_t
) 0;
554 loc
= newlocale(LC_NUMERIC_MASK
, "C", (locale_t
) 0);
555 if (loc
== (locale_t
) 0)
559 d
= strtod_l(s
, &x
, loc
);
562 if (!x
|| x
== s
|| *x
!= 0)
569 int parse_fractional_part_u(const char **p
, size_t digits
, unsigned *res
) {
576 /* accept any number of digits, strtoull is limited to 19 */
577 for (i
=0; i
< digits
; i
++,s
++) {
578 if (*s
< '0' || *s
> '9') {
582 /* too few digits, pad with 0 */
583 for (; i
< digits
; i
++)
594 if (*s
>= '5' && *s
<= '9')
597 s
+= strspn(s
, DIGITS
);
605 int parse_percent_unbounded(const char *p
) {
609 pc
= endswith(p
, "%");
613 n
= strndupa(p
, pc
- p
);
614 r
= safe_atoi(n
, &v
);
623 int parse_percent(const char *p
) {
626 v
= parse_percent_unbounded(p
);
633 int parse_permille_unbounded(const char *p
) {
634 const char *pc
, *pm
, *dot
, *n
;
637 pm
= endswith(p
, "‰");
639 n
= strndupa(p
, pm
- p
);
640 r
= safe_atoi(n
, &v
);
646 pc
= endswith(p
, "%");
650 dot
= memchr(p
, '.', pc
- p
);
654 if (dot
[1] < '0' || dot
[1] > '9')
657 n
= strndupa(p
, dot
- p
);
660 n
= strndupa(p
, pc
- p
);
662 r
= safe_atoi(n
, &v
);
667 if (v
> (INT_MAX
- q
) / 10)
676 int parse_permille(const char *p
) {
679 v
= parse_permille_unbounded(p
);
686 int parse_nice(const char *p
, int *ret
) {
689 r
= safe_atoi(p
, &n
);
693 if (!nice_is_valid(n
))
700 int parse_ip_port(const char *s
, uint16_t *ret
) {
704 r
= safe_atou16(s
, &l
);
716 int parse_ip_port_range(const char *s
, uint16_t *low
, uint16_t *high
) {
720 r
= parse_range(s
, &l
, &h
);
724 if (l
<= 0 || l
> 65535 || h
<= 0 || h
> 65535)
736 int parse_dev(const char *s
, dev_t
*ret
) {
742 n
= strspn(s
, DIGITS
);
748 major
= strndupa(s
, n
);
749 r
= safe_atou(major
, &x
);
753 r
= safe_atou(s
+ n
+ 1, &y
);
757 if (!DEVICE_MAJOR_VALID(x
) || !DEVICE_MINOR_VALID(y
))
760 *ret
= makedev(x
, y
);
764 int parse_oom_score_adjust(const char *s
, int *ret
) {
770 r
= safe_atoi(s
, &v
);
774 if (v
< OOM_SCORE_ADJ_MIN
|| v
> OOM_SCORE_ADJ_MAX
)