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 /// Additional includes needed by elogind
22 #include "musl_missing.h"
24 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 #if 0 /// UNNEEDED by elogind
249 int parse_range(const char *t, unsigned *lower, unsigned *upper) {
250 _cleanup_free_ char *word = NULL;
257 /* Extract the lower bound. */
258 r = extract_first_word(&t, &word, "-", EXTRACT_DONT_COALESCE_SEPARATORS);
264 r = safe_atou(word, &l);
268 /* Check for the upper bound and extract it if needed */
270 /* Single number with no dashes. */
273 /* Trailing dash is an error. */
276 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_permille_unbounded(const char *p) {
636 const char *pc, *pm, *dot, *n;
639 pm = endswith(p, "‰");
641 n = strndupa(p, pm - p);
642 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);
665 if (v > (INT_MAX - q) / 10)
677 int parse_permille(const char *p) {
680 v = parse_permille_unbounded(p);
687 #if 0 /// UNNEEDED by elogind
688 int parse_nice(const char *p, int *ret) {
691 r = safe_atoi(p, &n);
695 if (!nice_is_valid(n))
702 int parse_ip_port(const char *s, uint16_t *ret) {
706 r = safe_atou16(s, &l);
719 int parse_dev(const char *s, dev_t *ret) {
723 if (sscanf(s, "%u:%u", &x, &y) != 2)
727 if ((unsigned) major(d) != x || (unsigned) minor(d) != y)
734 int parse_oom_score_adjust(const char *s, int *ret) {
740 r = safe_atoi(s, &v);
744 if (v < OOM_SCORE_ADJ_MIN || v > OOM_SCORE_ADJ_MAX)