]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/parse-util.c
Merge pull request #26082 from kraxel/uki
[thirdparty/systemd.git] / src / basic / parse-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <inttypes.h>
5 #include <net/if.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/socket.h>
9
10 #include "alloc-util.h"
11 #include "errno-list.h"
12 #include "extract-word.h"
13 #include "locale-util.h"
14 #include "macro.h"
15 #include "missing_network.h"
16 #include "parse-util.h"
17 #include "process-util.h"
18 #include "stat-util.h"
19 #include "string-util.h"
20 #include "strv.h"
21
22 int parse_boolean(const char *v) {
23 if (!v)
24 return -EINVAL;
25
26 if (STRCASE_IN_SET(v,
27 "1",
28 "yes",
29 "y",
30 "true",
31 "t",
32 "on"))
33 return 1;
34
35 if (STRCASE_IN_SET(v,
36 "0",
37 "no",
38 "n",
39 "false",
40 "f",
41 "off"))
42 return 0;
43
44 return -EINVAL;
45 }
46
47 int parse_pid(const char *s, pid_t* ret_pid) {
48 unsigned long ul = 0;
49 pid_t pid;
50 int r;
51
52 assert(s);
53 assert(ret_pid);
54
55 r = safe_atolu(s, &ul);
56 if (r < 0)
57 return r;
58
59 pid = (pid_t) ul;
60
61 if ((unsigned long) pid != ul)
62 return -ERANGE;
63
64 if (!pid_is_valid(pid))
65 return -ERANGE;
66
67 *ret_pid = pid;
68 return 0;
69 }
70
71 int parse_mode(const char *s, mode_t *ret) {
72 unsigned m;
73 int r;
74
75 assert(s);
76
77 r = safe_atou_full(s, 8 |
78 SAFE_ATO_REFUSE_PLUS_MINUS, /* Leading '+' or even '-' char? that's just weird,
79 * refuse. User might have wanted to add mode flags or
80 * so, but this parser doesn't allow that, so let's
81 * better be safe. */
82 &m);
83 if (r < 0)
84 return r;
85 if (m > 07777)
86 return -ERANGE;
87
88 if (ret)
89 *ret = m;
90 return 0;
91 }
92
93 int parse_ifindex(const char *s) {
94 int ifi, r;
95
96 assert(s);
97
98 r = safe_atoi(s, &ifi);
99 if (r < 0)
100 return r;
101 if (ifi <= 0)
102 return -EINVAL;
103
104 return ifi;
105 }
106
107 int parse_mtu(int family, const char *s, uint32_t *ret) {
108 uint64_t u;
109 size_t m;
110 int r;
111
112 r = parse_size(s, 1024, &u);
113 if (r < 0)
114 return r;
115
116 if (u > UINT32_MAX)
117 return -ERANGE;
118
119 if (family == AF_INET6)
120 m = IPV6_MIN_MTU; /* This is 1280 */
121 else
122 m = IPV4_MIN_MTU; /* For all other protocols, including 'unspecified' we assume the IPv4 minimal MTU */
123
124 if (u < m)
125 return -ERANGE;
126
127 *ret = (uint32_t) u;
128 return 0;
129 }
130
131 int parse_size(const char *t, uint64_t base, uint64_t *size) {
132
133 /* Soo, sometimes we want to parse IEC binary suffixes, and
134 * sometimes SI decimal suffixes. This function can parse
135 * both. Which one is the right way depends on the
136 * context. Wikipedia suggests that SI is customary for
137 * hardware metrics and network speeds, while IEC is
138 * customary for most data sizes used by software and volatile
139 * (RAM) memory. Hence be careful which one you pick!
140 *
141 * In either case we use just K, M, G as suffix, and not Ki,
142 * Mi, Gi or so (as IEC would suggest). That's because that's
143 * frickin' ugly. But this means you really need to make sure
144 * to document which base you are parsing when you use this
145 * call. */
146
147 struct table {
148 const char *suffix;
149 unsigned long long factor;
150 };
151
152 static const struct table iec[] = {
153 { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
154 { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL },
155 { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
156 { "G", 1024ULL*1024ULL*1024ULL },
157 { "M", 1024ULL*1024ULL },
158 { "K", 1024ULL },
159 { "B", 1ULL },
160 { "", 1ULL },
161 };
162
163 static const struct table si[] = {
164 { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
165 { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL },
166 { "T", 1000ULL*1000ULL*1000ULL*1000ULL },
167 { "G", 1000ULL*1000ULL*1000ULL },
168 { "M", 1000ULL*1000ULL },
169 { "K", 1000ULL },
170 { "B", 1ULL },
171 { "", 1ULL },
172 };
173
174 const struct table *table;
175 const char *p;
176 unsigned long long r = 0;
177 unsigned n_entries, start_pos = 0;
178
179 assert(t);
180 assert(IN_SET(base, 1000, 1024));
181 assert(size);
182
183 if (base == 1000) {
184 table = si;
185 n_entries = ELEMENTSOF(si);
186 } else {
187 table = iec;
188 n_entries = ELEMENTSOF(iec);
189 }
190
191 p = t;
192 do {
193 unsigned long long l, tmp;
194 double frac = 0;
195 char *e;
196 unsigned i;
197
198 p += strspn(p, WHITESPACE);
199
200 errno = 0;
201 l = strtoull(p, &e, 10);
202 if (errno > 0)
203 return -errno;
204 if (e == p)
205 return -EINVAL;
206 if (*p == '-')
207 return -ERANGE;
208
209 if (*e == '.') {
210 e++;
211
212 /* strtoull() itself would accept space/+/- */
213 if (ascii_isdigit(*e)) {
214 unsigned long long l2;
215 char *e2;
216
217 l2 = strtoull(e, &e2, 10);
218 if (errno > 0)
219 return -errno;
220
221 /* Ignore failure. E.g. 10.M is valid */
222 frac = l2;
223 for (; e < e2; e++)
224 frac /= 10;
225 }
226 }
227
228 e += strspn(e, WHITESPACE);
229
230 for (i = start_pos; i < n_entries; i++)
231 if (startswith(e, table[i].suffix))
232 break;
233
234 if (i >= n_entries)
235 return -EINVAL;
236
237 if (l + (frac > 0) > ULLONG_MAX / table[i].factor)
238 return -ERANGE;
239
240 tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
241 if (tmp > ULLONG_MAX - r)
242 return -ERANGE;
243
244 r += tmp;
245 if ((unsigned long long) (uint64_t) r != r)
246 return -ERANGE;
247
248 p = e + strlen(table[i].suffix);
249
250 start_pos = i + 1;
251
252 } while (*p);
253
254 *size = r;
255
256 return 0;
257 }
258
259 int parse_sector_size(const char *t, uint64_t *ret) {
260 int r;
261
262 assert(t);
263 assert(ret);
264
265 uint64_t ss;
266
267 r = safe_atou64(t, &ss);
268 if (r < 0)
269 return log_error_errno(r, "Failed to parse sector size parameter %s", t);
270 if (ss < 512 || ss > 4096) /* Allow up to 4K due to dm-crypt support and 4K alignment by the homed LUKS backend */
271 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Sector size not between 512 and 4096: %s", t);
272 if (!ISPOWEROF2(ss))
273 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Sector size not power of 2: %s", t);
274
275 *ret = ss;
276 return 0;
277 }
278
279 int parse_range(const char *t, unsigned *lower, unsigned *upper) {
280 _cleanup_free_ char *word = NULL;
281 unsigned l, u;
282 int r;
283
284 assert(lower);
285 assert(upper);
286
287 /* Extract the lower bound. */
288 r = extract_first_word(&t, &word, "-", EXTRACT_DONT_COALESCE_SEPARATORS);
289 if (r < 0)
290 return r;
291 if (r == 0)
292 return -EINVAL;
293
294 r = safe_atou(word, &l);
295 if (r < 0)
296 return r;
297
298 /* Check for the upper bound and extract it if needed */
299 if (!t)
300 /* Single number with no dashes. */
301 u = l;
302 else if (!*t)
303 /* Trailing dash is an error. */
304 return -EINVAL;
305 else {
306 r = safe_atou(t, &u);
307 if (r < 0)
308 return r;
309 }
310
311 *lower = l;
312 *upper = u;
313 return 0;
314 }
315
316 int parse_errno(const char *t) {
317 int r, e;
318
319 assert(t);
320
321 r = errno_from_name(t);
322 if (r > 0)
323 return r;
324
325 r = safe_atoi(t, &e);
326 if (r < 0)
327 return r;
328
329 /* 0 is also allowed here */
330 if (!errno_is_valid(e) && e != 0)
331 return -ERANGE;
332
333 return e;
334 }
335
336 static const char *mangle_base(const char *s, unsigned *base) {
337 const char *k;
338
339 assert(s);
340 assert(base);
341
342 /* Base already explicitly specified, then don't do anything. */
343 if (SAFE_ATO_MASK_FLAGS(*base) != 0)
344 return s;
345
346 /* Support Python 3 style "0b" and 0x" prefixes, because they truly make sense, much more than C's "0" prefix for octal. */
347 k = STARTSWITH_SET(s, "0b", "0B");
348 if (k) {
349 *base = 2 | (*base & SAFE_ATO_ALL_FLAGS);
350 return k;
351 }
352
353 k = STARTSWITH_SET(s, "0o", "0O");
354 if (k) {
355 *base = 8 | (*base & SAFE_ATO_ALL_FLAGS);
356 return k;
357 }
358
359 return s;
360 }
361
362 int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
363 char *x = NULL;
364 unsigned long l;
365
366 assert(s);
367 assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
368
369 /* strtoul() is happy to parse negative values, and silently converts them to unsigned values without
370 * generating an error. We want a clean error, hence let's look for the "-" prefix on our own, and
371 * generate an error. But let's do so only after strtoul() validated that the string is clean
372 * otherwise, so that we return EINVAL preferably over ERANGE. */
373
374 if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
375 strchr(WHITESPACE, s[0]))
376 return -EINVAL;
377
378 s += strspn(s, WHITESPACE);
379
380 if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
381 IN_SET(s[0], '+', '-'))
382 return -EINVAL; /* Note that we check the "-" prefix again a second time below, but return a
383 * different error. I.e. if the SAFE_ATO_REFUSE_PLUS_MINUS flag is set we
384 * blanket refuse +/- prefixed integers, while if it is missing we'll just
385 * return ERANGE, because the string actually parses correctly, but doesn't
386 * fit in the return type. */
387
388 if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
389 s[0] == '0' && !streq(s, "0"))
390 return -EINVAL; /* This is particularly useful to avoid ambiguities between C's octal
391 * notation and assumed-to-be-decimal integers with a leading zero. */
392
393 s = mangle_base(s, &base);
394
395 errno = 0;
396 l = strtoul(s, &x, SAFE_ATO_MASK_FLAGS(base) /* Let's mask off the flags bits so that only the actual
397 * base is left */);
398 if (errno > 0)
399 return -errno;
400 if (!x || x == s || *x != 0)
401 return -EINVAL;
402 if (l != 0 && s[0] == '-')
403 return -ERANGE;
404 if ((unsigned long) (unsigned) l != l)
405 return -ERANGE;
406
407 if (ret_u)
408 *ret_u = (unsigned) l;
409
410 return 0;
411 }
412
413 int safe_atoi(const char *s, int *ret_i) {
414 unsigned base = 0;
415 char *x = NULL;
416 long l;
417
418 assert(s);
419
420 s += strspn(s, WHITESPACE);
421 s = mangle_base(s, &base);
422
423 errno = 0;
424 l = strtol(s, &x, base);
425 if (errno > 0)
426 return -errno;
427 if (!x || x == s || *x != 0)
428 return -EINVAL;
429 if ((long) (int) l != l)
430 return -ERANGE;
431
432 if (ret_i)
433 *ret_i = (int) l;
434
435 return 0;
436 }
437
438 int safe_atollu_full(const char *s, unsigned base, unsigned long long *ret_llu) {
439 char *x = NULL;
440 unsigned long long l;
441
442 assert(s);
443 assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
444
445 if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
446 strchr(WHITESPACE, s[0]))
447 return -EINVAL;
448
449 s += strspn(s, WHITESPACE);
450
451 if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
452 IN_SET(s[0], '+', '-'))
453 return -EINVAL;
454
455 if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
456 s[0] == '0' && s[1] != 0)
457 return -EINVAL;
458
459 s = mangle_base(s, &base);
460
461 errno = 0;
462 l = strtoull(s, &x, SAFE_ATO_MASK_FLAGS(base));
463 if (errno > 0)
464 return -errno;
465 if (!x || x == s || *x != 0)
466 return -EINVAL;
467 if (l != 0 && s[0] == '-')
468 return -ERANGE;
469
470 if (ret_llu)
471 *ret_llu = l;
472
473 return 0;
474 }
475
476 int safe_atolli(const char *s, long long int *ret_lli) {
477 unsigned base = 0;
478 char *x = NULL;
479 long long l;
480
481 assert(s);
482
483 s += strspn(s, WHITESPACE);
484 s = mangle_base(s, &base);
485
486 errno = 0;
487 l = strtoll(s, &x, base);
488 if (errno > 0)
489 return -errno;
490 if (!x || x == s || *x != 0)
491 return -EINVAL;
492
493 if (ret_lli)
494 *ret_lli = l;
495
496 return 0;
497 }
498
499 int safe_atou8_full(const char *s, unsigned base, uint8_t *ret) {
500 unsigned u;
501 int r;
502
503 r = safe_atou_full(s, base, &u);
504 if (r < 0)
505 return r;
506 if (u > UINT8_MAX)
507 return -ERANGE;
508
509 *ret = (uint8_t) u;
510 return 0;
511 }
512
513 int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
514 unsigned u;
515 int r;
516
517 r = safe_atou_full(s, base, &u);
518 if (r < 0)
519 return r;
520 if (u > UINT16_MAX)
521 return -ERANGE;
522
523 *ret = (uint16_t) u;
524 return 0;
525 }
526
527 int safe_atoi16(const char *s, int16_t *ret) {
528 unsigned base = 0;
529 char *x = NULL;
530 long l;
531
532 assert(s);
533
534 s += strspn(s, WHITESPACE);
535 s = mangle_base(s, &base);
536
537 errno = 0;
538 l = strtol(s, &x, base);
539 if (errno > 0)
540 return -errno;
541 if (!x || x == s || *x != 0)
542 return -EINVAL;
543 if ((long) (int16_t) l != l)
544 return -ERANGE;
545
546 if (ret)
547 *ret = (int16_t) l;
548
549 return 0;
550 }
551
552 int safe_atod(const char *s, double *ret_d) {
553 _cleanup_(freelocalep) locale_t loc = (locale_t) 0;
554 char *x = NULL;
555 double d = 0;
556
557 assert(s);
558
559 loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
560 if (loc == (locale_t) 0)
561 return -errno;
562
563 errno = 0;
564 d = strtod_l(s, &x, loc);
565 if (errno > 0)
566 return -errno;
567 if (!x || x == s || *x != 0)
568 return -EINVAL;
569
570 if (ret_d)
571 *ret_d = (double) d;
572
573 return 0;
574 }
575
576 int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) {
577 unsigned val = 0;
578 const char *s;
579
580 s = *p;
581
582 /* accept any number of digits, strtoull is limited to 19 */
583 for (size_t i = 0; i < digits; i++,s++) {
584 if (!ascii_isdigit(*s)) {
585 if (i == 0)
586 return -EINVAL;
587
588 /* too few digits, pad with 0 */
589 for (; i < digits; i++)
590 val *= 10;
591
592 break;
593 }
594
595 val *= 10;
596 val += *s - '0';
597 }
598
599 /* maybe round up */
600 if (*s >= '5' && *s <= '9')
601 val++;
602
603 s += strspn(s, DIGITS);
604
605 *p = s;
606 *res = val;
607
608 return 0;
609 }
610
611 int parse_nice(const char *p, int *ret) {
612 int n, r;
613
614 r = safe_atoi(p, &n);
615 if (r < 0)
616 return r;
617
618 if (!nice_is_valid(n))
619 return -ERANGE;
620
621 *ret = n;
622 return 0;
623 }
624
625 int parse_ip_port(const char *s, uint16_t *ret) {
626 uint16_t l;
627 int r;
628
629 r = safe_atou16_full(s, SAFE_ATO_REFUSE_LEADING_WHITESPACE, &l);
630 if (r < 0)
631 return r;
632
633 if (l == 0)
634 return -EINVAL;
635
636 *ret = (uint16_t) l;
637
638 return 0;
639 }
640
641 int parse_ip_port_range(const char *s, uint16_t *low, uint16_t *high) {
642 unsigned l, h;
643 int r;
644
645 r = parse_range(s, &l, &h);
646 if (r < 0)
647 return r;
648
649 if (l <= 0 || l > 65535 || h <= 0 || h > 65535)
650 return -EINVAL;
651
652 if (h < l)
653 return -EINVAL;
654
655 *low = l;
656 *high = h;
657
658 return 0;
659 }
660
661 int parse_ip_prefix_length(const char *s, int *ret) {
662 unsigned l;
663 int r;
664
665 r = safe_atou(s, &l);
666 if (r < 0)
667 return r;
668
669 if (l > 128)
670 return -ERANGE;
671
672 *ret = (int) l;
673
674 return 0;
675 }
676
677 int parse_oom_score_adjust(const char *s, int *ret) {
678 int r, v;
679
680 assert(s);
681 assert(ret);
682
683 r = safe_atoi(s, &v);
684 if (r < 0)
685 return r;
686
687 if (!oom_score_adjust_is_valid(v))
688 return -ERANGE;
689
690 *ret = v;
691 return 0;
692 }
693
694 int store_loadavg_fixed_point(unsigned long i, unsigned long f, loadavg_t *ret) {
695 assert(ret);
696
697 if (i >= (~0UL << LOADAVG_PRECISION_BITS))
698 return -ERANGE;
699
700 i = i << LOADAVG_PRECISION_BITS;
701 f = DIV_ROUND_UP((f << LOADAVG_PRECISION_BITS), 100);
702
703 if (f >= LOADAVG_FIXED_POINT_1_0)
704 return -ERANGE;
705
706 *ret = i | f;
707 return 0;
708 }
709
710 int parse_loadavg_fixed_point(const char *s, loadavg_t *ret) {
711 const char *d, *f_str, *i_str;
712 unsigned long i, f;
713 int r;
714
715 assert(s);
716 assert(ret);
717
718 d = strchr(s, '.');
719 if (!d)
720 return -EINVAL;
721
722 i_str = strndupa_safe(s, d - s);
723 f_str = d + 1;
724
725 r = safe_atolu_full(i_str, 10, &i);
726 if (r < 0)
727 return r;
728
729 r = safe_atolu_full(f_str, 10, &f);
730 if (r < 0)
731 return r;
732
733 return store_loadavg_fixed_point(i, f, ret);
734 }