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"
17 #include "parse-util.h"
18 #include "process-util.h"
19 #include "string-util.h"
21 int parse_boolean(const char *v
) {
24 if (streq(v
, "1") || strcaseeq(v
, "yes") || strcaseeq(v
, "y") || strcaseeq(v
, "true") || strcaseeq(v
, "t") || strcaseeq(v
, "on"))
26 else if (streq(v
, "0") || strcaseeq(v
, "no") || strcaseeq(v
, "n") || strcaseeq(v
, "false") || strcaseeq(v
, "f") || strcaseeq(v
, "off"))
32 int parse_pid(const char *s
, pid_t
* ret_pid
) {
40 r
= safe_atolu(s
, &ul
);
46 if ((unsigned long) pid
!= ul
)
49 if (!pid_is_valid(pid
))
56 int parse_mode(const char *s
, mode_t
*ret
) {
63 s
+= strspn(s
, WHITESPACE
);
71 if (!x
|| x
== s
|| *x
!= 0)
73 if (l
< 0 || l
> 07777)
80 int parse_ifindex(const char *s
, int *ret
) {
83 r
= safe_atoi(s
, &ifi
);
93 int parse_mtu(int family
, const char *s
, uint32_t *ret
) {
98 r
= parse_size(s
, 1024, &u
);
105 if (family
== AF_INET6
)
106 m
= IPV6_MIN_MTU
; /* This is 1280 */
108 m
= IPV4_MIN_MTU
; /* For all other protocols, including 'unspecified' we assume the IPv4 minimal MTU */
117 int parse_size(const char *t
, uint64_t base
, uint64_t *size
) {
119 /* Soo, sometimes we want to parse IEC binary suffixes, and
120 * sometimes SI decimal suffixes. This function can parse
121 * both. Which one is the right way depends on the
122 * context. Wikipedia suggests that SI is customary for
123 * hardware metrics and network speeds, while IEC is
124 * customary for most data sizes used by software and volatile
125 * (RAM) memory. Hence be careful which one you pick!
127 * In either case we use just K, M, G as suffix, and not Ki,
128 * Mi, Gi or so (as IEC would suggest). That's because that's
129 * frickin' ugly. But this means you really need to make sure
130 * to document which base you are parsing when you use this
135 unsigned long long factor
;
138 static const struct table iec
[] = {
139 { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
140 { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
141 { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
142 { "G", 1024ULL*1024ULL*1024ULL },
143 { "M", 1024ULL*1024ULL },
149 static const struct table si
[] = {
150 { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
151 { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
152 { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
153 { "G", 1000ULL*1000ULL*1000ULL },
154 { "M", 1000ULL*1000ULL },
160 const struct table
*table
;
162 unsigned long long r
= 0;
163 unsigned n_entries
, start_pos
= 0;
166 assert(IN_SET(base
, 1000, 1024));
171 n_entries
= ELEMENTSOF(si
);
174 n_entries
= ELEMENTSOF(iec
);
179 unsigned long long l
, tmp
;
184 p
+= strspn(p
, WHITESPACE
);
187 l
= strtoull(p
, &e
, 10);
198 /* strtoull() itself would accept space/+/- */
199 if (*e
>= '0' && *e
<= '9') {
200 unsigned long long l2
;
203 l2
= strtoull(e
, &e2
, 10);
207 /* Ignore failure. E.g. 10.M is valid */
214 e
+= strspn(e
, WHITESPACE
);
216 for (i
= start_pos
; i
< n_entries
; i
++)
217 if (startswith(e
, table
[i
].suffix
))
223 if (l
+ (frac
> 0) > ULLONG_MAX
/ table
[i
].factor
)
226 tmp
= l
* table
[i
].factor
+ (unsigned long long) (frac
* table
[i
].factor
);
227 if (tmp
> ULLONG_MAX
- r
)
231 if ((unsigned long long) (uint64_t) r
!= r
)
234 p
= e
+ strlen(table
[i
].suffix
);
245 int parse_range(const char *t
, unsigned *lower
, unsigned *upper
) {
246 _cleanup_free_
char *word
= NULL
;
253 /* Extract the lower bound. */
254 r
= extract_first_word(&t
, &word
, "-", EXTRACT_DONT_COALESCE_SEPARATORS
);
260 r
= safe_atou(word
, &l
);
264 /* Check for the upper bound and extract it if needed */
266 /* Single number with no dashes. */
269 /* Trailing dash is an error. */
272 r
= safe_atou(t
, &u
);
282 int parse_errno(const char *t
) {
287 r
= errno_from_name(t
);
291 r
= safe_atoi(t
, &e
);
295 /* 0 is also allowed here */
296 if (!errno_is_valid(e
) && e
!= 0)
302 int parse_syscall_and_errno(const char *in
, char **name
, int *error
) {
303 _cleanup_free_
char *n
= NULL
;
312 * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255".
313 * If errno is omitted, then error is set to -1.
314 * Empty syscall name is not allowed.
315 * Here, we do not check that the syscall name is valid or not.
320 e
= parse_errno(p
+ 1);
324 n
= strndup(in
, p
- in
);
340 char *format_bytes(char *buf
, size_t l
, uint64_t t
) {
343 /* This only does IEC units so far */
345 static const struct {
349 { "E", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
350 { "P", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
351 { "T", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
352 { "G", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) },
353 { "M", UINT64_C(1024)*UINT64_C(1024) },
354 { "K", UINT64_C(1024) },
357 if (t
== (uint64_t) -1)
360 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
362 if (t
>= table
[i
].factor
) {
364 "%" PRIu64
".%" PRIu64
"%s",
366 ((t
*UINT64_C(10)) / table
[i
].factor
) % UINT64_C(10),
373 snprintf(buf
, l
, "%" PRIu64
"B", t
);
381 int safe_atou_full(const char *s
, unsigned base
, unsigned *ret_u
) {
389 /* strtoul() is happy to parse negative values, and silently
390 * converts them to unsigned values without generating an
391 * error. We want a clean error, hence let's look for the "-"
392 * prefix on our own, and generate an error. But let's do so
393 * only after strtoul() validated that the string is clean
394 * otherwise, so that we return EINVAL preferably over
397 s
+= strspn(s
, WHITESPACE
);
400 l
= strtoul(s
, &x
, base
);
403 if (!x
|| x
== s
|| *x
!= 0)
407 if ((unsigned long) (unsigned) l
!= l
)
410 *ret_u
= (unsigned) l
;
414 int safe_atoi(const char *s
, int *ret_i
) {
422 l
= strtol(s
, &x
, 0);
425 if (!x
|| x
== s
|| *x
!= 0)
427 if ((long) (int) l
!= l
)
434 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
436 unsigned long long l
;
441 s
+= strspn(s
, WHITESPACE
);
444 l
= strtoull(s
, &x
, 0);
447 if (!x
|| x
== s
|| *x
!= 0)
456 int safe_atolli(const char *s
, long long int *ret_lli
) {
464 l
= strtoll(s
, &x
, 0);
467 if (!x
|| x
== s
|| *x
!= 0)
474 int safe_atou8(const char *s
, uint8_t *ret
) {
481 s
+= strspn(s
, WHITESPACE
);
484 l
= strtoul(s
, &x
, 0);
487 if (!x
|| x
== s
|| *x
!= 0)
491 if ((unsigned long) (uint8_t) l
!= l
)
498 int safe_atou16_full(const char *s
, unsigned base
, uint16_t *ret
) {
506 s
+= strspn(s
, WHITESPACE
);
509 l
= strtoul(s
, &x
, base
);
512 if (!x
|| x
== s
|| *x
!= 0)
516 if ((unsigned long) (uint16_t) l
!= l
)
523 int safe_atoi16(const char *s
, int16_t *ret
) {
531 l
= strtol(s
, &x
, 0);
534 if (!x
|| x
== s
|| *x
!= 0)
536 if ((long) (int16_t) l
!= l
)
543 int safe_atod(const char *s
, double *ret_d
) {
544 _cleanup_(freelocalep
) locale_t loc
= (locale_t
) 0;
551 loc
= newlocale(LC_NUMERIC_MASK
, "C", (locale_t
) 0);
552 if (loc
== (locale_t
) 0)
556 d
= strtod_l(s
, &x
, loc
);
559 if (!x
|| x
== s
|| *x
!= 0)
566 int parse_fractional_part_u(const char **p
, size_t digits
, unsigned *res
) {
573 /* accept any number of digits, strtoull is limited to 19 */
574 for (i
=0; i
< digits
; i
++,s
++) {
575 if (*s
< '0' || *s
> '9') {
579 /* too few digits, pad with 0 */
580 for (; i
< digits
; i
++)
591 if (*s
>= '5' && *s
<= '9')
594 s
+= strspn(s
, DIGITS
);
602 int parse_percent_unbounded(const char *p
) {
606 pc
= endswith(p
, "%");
610 n
= strndupa(p
, pc
- p
);
611 r
= safe_atoi(n
, &v
);
620 int parse_percent(const char *p
) {
623 v
= parse_percent_unbounded(p
);
630 int parse_permille_unbounded(const char *p
) {
631 const char *pc
, *pm
, *dot
, *n
;
634 pm
= endswith(p
, "‰");
636 n
= strndupa(p
, pm
- p
);
637 r
= safe_atoi(n
, &v
);
643 pc
= endswith(p
, "%");
647 dot
= memchr(p
, '.', pc
- p
);
651 if (dot
[1] < '0' || dot
[1] > '9')
654 n
= strndupa(p
, dot
- p
);
657 n
= strndupa(p
, pc
- p
);
659 r
= safe_atoi(n
, &v
);
664 if (v
> (INT_MAX
- q
) / 10)
673 int parse_permille(const char *p
) {
676 v
= parse_permille_unbounded(p
);
683 int parse_nice(const char *p
, int *ret
) {
686 r
= safe_atoi(p
, &n
);
690 if (!nice_is_valid(n
))
697 int parse_ip_port(const char *s
, uint16_t *ret
) {
701 r
= safe_atou16(s
, &l
);
713 int parse_dev(const char *s
, dev_t
*ret
) {
717 if (sscanf(s
, "%u:%u", &x
, &y
) != 2)
721 if ((unsigned) major(d
) != x
|| (unsigned) minor(d
) != y
)
728 int parse_oom_score_adjust(const char *s
, int *ret
) {
734 r
= safe_atoi(s
, &v
);
738 if (v
< OOM_SCORE_ADJ_MIN
|| v
> OOM_SCORE_ADJ_MAX
)