1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
14 #include <sys/socket.h>
16 #include "alloc-util.h"
17 #include "errno-list.h"
18 #include "extract-word.h"
19 #include "locale-util.h"
22 #include "parse-util.h"
23 #include "process-util.h"
24 #include "string-util.h"
26 int parse_boolean(const char *v
) {
29 if (streq(v
, "1") || strcaseeq(v
, "yes") || strcaseeq(v
, "y") || strcaseeq(v
, "true") || strcaseeq(v
, "t") || strcaseeq(v
, "on"))
31 else if (streq(v
, "0") || strcaseeq(v
, "no") || strcaseeq(v
, "n") || strcaseeq(v
, "false") || strcaseeq(v
, "f") || strcaseeq(v
, "off"))
37 int parse_pid(const char *s
, pid_t
* ret_pid
) {
45 r
= safe_atolu(s
, &ul
);
51 if ((unsigned long) pid
!= ul
)
54 if (!pid_is_valid(pid
))
61 int parse_mode(const char *s
, mode_t
*ret
) {
68 s
+= strspn(s
, WHITESPACE
);
76 if (!x
|| x
== s
|| *x
!= 0)
78 if (l
< 0 || l
> 07777)
85 int parse_ifindex(const char *s
, int *ret
) {
88 r
= safe_atoi(s
, &ifi
);
98 int parse_mtu(int family
, const char *s
, uint32_t *ret
) {
103 r
= parse_size(s
, 1024, &u
);
110 if (family
== AF_INET6
)
111 m
= IPV6_MIN_MTU
; /* This is 1280 */
113 m
= IPV4_MIN_MTU
; /* For all other protocols, including 'unspecified' we assume the IPv4 minimal MTU */
122 int parse_size(const char *t
, uint64_t base
, uint64_t *size
) {
124 /* Soo, sometimes we want to parse IEC binary suffixes, and
125 * sometimes SI decimal suffixes. This function can parse
126 * both. Which one is the right way depends on the
127 * context. Wikipedia suggests that SI is customary for
128 * hardware metrics and network speeds, while IEC is
129 * customary for most data sizes used by software and volatile
130 * (RAM) memory. Hence be careful which one you pick!
132 * In either case we use just K, M, G as suffix, and not Ki,
133 * Mi, Gi or so (as IEC would suggest). That's because that's
134 * frickin' ugly. But this means you really need to make sure
135 * to document which base you are parsing when you use this
140 unsigned long long factor
;
143 static const struct table iec
[] = {
144 { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
145 { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
146 { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
147 { "G", 1024ULL*1024ULL*1024ULL },
148 { "M", 1024ULL*1024ULL },
154 static const struct table si
[] = {
155 { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
156 { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
157 { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
158 { "G", 1000ULL*1000ULL*1000ULL },
159 { "M", 1000ULL*1000ULL },
165 const struct table
*table
;
167 unsigned long long r
= 0;
168 unsigned n_entries
, start_pos
= 0;
171 assert(IN_SET(base
, 1000, 1024));
176 n_entries
= ELEMENTSOF(si
);
179 n_entries
= ELEMENTSOF(iec
);
184 unsigned long long l
, tmp
;
189 p
+= strspn(p
, WHITESPACE
);
192 l
= strtoull(p
, &e
, 10);
203 /* strtoull() itself would accept space/+/- */
204 if (*e
>= '0' && *e
<= '9') {
205 unsigned long long l2
;
208 l2
= strtoull(e
, &e2
, 10);
212 /* Ignore failure. E.g. 10.M is valid */
219 e
+= strspn(e
, WHITESPACE
);
221 for (i
= start_pos
; i
< n_entries
; i
++)
222 if (startswith(e
, table
[i
].suffix
))
228 if (l
+ (frac
> 0) > ULLONG_MAX
/ table
[i
].factor
)
231 tmp
= l
* table
[i
].factor
+ (unsigned long long) (frac
* table
[i
].factor
);
232 if (tmp
> ULLONG_MAX
- r
)
236 if ((unsigned long long) (uint64_t) r
!= r
)
239 p
= e
+ strlen(table
[i
].suffix
);
250 int parse_range(const char *t
, unsigned *lower
, unsigned *upper
) {
251 _cleanup_free_
char *word
= NULL
;
258 /* Extract the lower bound. */
259 r
= extract_first_word(&t
, &word
, "-", EXTRACT_DONT_COALESCE_SEPARATORS
);
265 r
= safe_atou(word
, &l
);
269 /* Check for the upper bound and extract it if needed */
271 /* Single number with no dashes. */
274 /* Trailing dash is an error. */
277 r
= safe_atou(t
, &u
);
287 int parse_errno(const char *t
) {
292 r
= errno_from_name(t
);
296 r
= safe_atoi(t
, &e
);
300 /* 0 is also allowed here */
301 if (!errno_is_valid(e
) && e
!= 0)
307 int parse_syscall_and_errno(const char *in
, char **name
, int *error
) {
308 _cleanup_free_
char *n
= NULL
;
317 * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
318 * If errno is omitted, then error is set to -1.
319 * Empty syscall name is not allowed.
320 * Here, we do not check that the syscall name is valid or not.
325 e
= parse_errno(p
+ 1);
329 n
= strndup(in
, p
- in
);
345 char *format_bytes(char *buf
, size_t l
, uint64_t t
) {
348 /* This only does IEC units so far */
350 static const struct {
354 { "E", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
355 { "P", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
356 { "T", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
357 { "G", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
358 { "M", UINT64_C(1024)*UINT64_C(1024) },
359 { "K", UINT64_C(1024) },
362 if (t
== (uint64_t) -1)
365 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
367 if (t
>= table
[i
].factor
) {
369 "%" PRIu64
".%" PRIu64
"%s",
371 ((t
*UINT64_C(10)) / table
[i
].factor
) % UINT64_C(10),
378 snprintf(buf
, l
, "%" PRIu64
"B", t
);
386 int safe_atou_full(const char *s
, unsigned base
, unsigned *ret_u
) {
394 /* strtoul() is happy to parse negative values, and silently
395 * converts them to unsigned values without generating an
396 * error. We want a clean error, hence let's look for the "-"
397 * prefix on our own, and generate an error. But let's do so
398 * only after strtoul() validated that the string is clean
399 * otherwise, so that we return EINVAL preferably over
402 s
+= strspn(s
, WHITESPACE
);
405 l
= strtoul(s
, &x
, base
);
408 if (!x
|| x
== s
|| *x
!= 0)
412 if ((unsigned long) (unsigned) l
!= l
)
415 *ret_u
= (unsigned) l
;
419 int safe_atoi(const char *s
, int *ret_i
) {
427 l
= strtol(s
, &x
, 0);
430 if (!x
|| x
== s
|| *x
!= 0)
432 if ((long) (int) l
!= l
)
439 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
441 unsigned long long l
;
446 s
+= strspn(s
, WHITESPACE
);
449 l
= strtoull(s
, &x
, 0);
452 if (!x
|| x
== s
|| *x
!= 0)
461 int safe_atolli(const char *s
, long long int *ret_lli
) {
469 l
= strtoll(s
, &x
, 0);
472 if (!x
|| x
== s
|| *x
!= 0)
479 int safe_atou8(const char *s
, uint8_t *ret
) {
486 s
+= strspn(s
, WHITESPACE
);
489 l
= strtoul(s
, &x
, 0);
492 if (!x
|| x
== s
|| *x
!= 0)
496 if ((unsigned long) (uint8_t) l
!= l
)
503 int safe_atou16_full(const char *s
, unsigned base
, uint16_t *ret
) {
511 s
+= strspn(s
, WHITESPACE
);
514 l
= strtoul(s
, &x
, base
);
517 if (!x
|| x
== s
|| *x
!= 0)
521 if ((unsigned long) (uint16_t) l
!= l
)
528 int safe_atoi16(const char *s
, int16_t *ret
) {
536 l
= strtol(s
, &x
, 0);
539 if (!x
|| x
== s
|| *x
!= 0)
541 if ((long) (int16_t) l
!= l
)
548 int safe_atod(const char *s
, double *ret_d
) {
549 _cleanup_(freelocalep
) locale_t loc
= (locale_t
) 0;
556 loc
= newlocale(LC_NUMERIC_MASK
, "C", (locale_t
) 0);
557 if (loc
== (locale_t
) 0)
561 d
= strtod_l(s
, &x
, loc
);
564 if (!x
|| x
== s
|| *x
!= 0)
571 int parse_fractional_part_u(const char **p
, size_t digits
, unsigned *res
) {
578 /* accept any number of digits, strtoull is limted to 19 */
579 for (i
=0; i
< digits
; i
++,s
++) {
580 if (*s
< '0' || *s
> '9') {
584 /* too few digits, pad with 0 */
585 for (; i
< digits
; i
++)
596 if (*s
>= '5' && *s
<= '9')
599 s
+= strspn(s
, DIGITS
);
607 int parse_percent_unbounded(const char *p
) {
611 pc
= endswith(p
, "%");
615 n
= strndupa(p
, pc
- p
);
616 r
= safe_atoi(n
, &v
);
625 int parse_percent(const char *p
) {
628 v
= parse_percent_unbounded(p
);
635 int parse_nice(const char *p
, int *ret
) {
638 r
= safe_atoi(p
, &n
);
642 if (!nice_is_valid(n
))
649 int parse_ip_port(const char *s
, uint16_t *ret
) {
653 r
= safe_atou16(s
, &l
);
665 int parse_dev(const char *s
, dev_t
*ret
) {
669 if (sscanf(s
, "%u:%u", &x
, &y
) != 2)
673 if ((unsigned) major(d
) != x
|| (unsigned) minor(d
) != y
)
680 int parse_oom_score_adjust(const char *s
, int *ret
) {
686 r
= safe_atoi(s
, &v
);
690 if (v
< OOM_SCORE_ADJ_MIN
|| v
> OOM_SCORE_ADJ_MAX
)