]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/parse-util.c
fd-util: introduce parse_fd()
[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
54 r = safe_atolu(s, &ul);
55 if (r < 0)
56 return r;
57
58 pid = (pid_t) ul;
59
60 if ((unsigned long) pid != ul)
61 return -ERANGE;
62
63 if (!pid_is_valid(pid))
64 return -ERANGE;
65
66 if (ret_pid)
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 int parse_fd(const char *t) {
337 int r, fd;
338
339 assert(t);
340
341 r = safe_atoi(t, &fd);
342 if (r < 0)
343 return r;
344
345 if (fd < 0)
346 return -ERANGE;
347
348 return fd;
349 }
350
351 static const char *mangle_base(const char *s, unsigned *base) {
352 const char *k;
353
354 assert(s);
355 assert(base);
356
357 /* Base already explicitly specified, then don't do anything. */
358 if (SAFE_ATO_MASK_FLAGS(*base) != 0)
359 return s;
360
361 /* Support Python 3 style "0b" and 0x" prefixes, because they truly make sense, much more than C's "0" prefix for octal. */
362 k = STARTSWITH_SET(s, "0b", "0B");
363 if (k) {
364 *base = 2 | (*base & SAFE_ATO_ALL_FLAGS);
365 return k;
366 }
367
368 k = STARTSWITH_SET(s, "0o", "0O");
369 if (k) {
370 *base = 8 | (*base & SAFE_ATO_ALL_FLAGS);
371 return k;
372 }
373
374 return s;
375 }
376
377 int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
378 char *x = NULL;
379 unsigned long l;
380
381 assert(s);
382 assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
383
384 /* strtoul() is happy to parse negative values, and silently converts them to unsigned values without
385 * generating an error. We want a clean error, hence let's look for the "-" prefix on our own, and
386 * generate an error. But let's do so only after strtoul() validated that the string is clean
387 * otherwise, so that we return EINVAL preferably over ERANGE. */
388
389 if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
390 strchr(WHITESPACE, s[0]))
391 return -EINVAL;
392
393 s += strspn(s, WHITESPACE);
394
395 if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
396 IN_SET(s[0], '+', '-'))
397 return -EINVAL; /* Note that we check the "-" prefix again a second time below, but return a
398 * different error. I.e. if the SAFE_ATO_REFUSE_PLUS_MINUS flag is set we
399 * blanket refuse +/- prefixed integers, while if it is missing we'll just
400 * return ERANGE, because the string actually parses correctly, but doesn't
401 * fit in the return type. */
402
403 if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
404 s[0] == '0' && !streq(s, "0"))
405 return -EINVAL; /* This is particularly useful to avoid ambiguities between C's octal
406 * notation and assumed-to-be-decimal integers with a leading zero. */
407
408 s = mangle_base(s, &base);
409
410 errno = 0;
411 l = strtoul(s, &x, SAFE_ATO_MASK_FLAGS(base) /* Let's mask off the flags bits so that only the actual
412 * base is left */);
413 if (errno > 0)
414 return -errno;
415 if (!x || x == s || *x != 0)
416 return -EINVAL;
417 if (l != 0 && s[0] == '-')
418 return -ERANGE;
419 if ((unsigned long) (unsigned) l != l)
420 return -ERANGE;
421
422 if (ret_u)
423 *ret_u = (unsigned) l;
424
425 return 0;
426 }
427
428 int safe_atoi(const char *s, int *ret_i) {
429 unsigned base = 0;
430 char *x = NULL;
431 long l;
432
433 assert(s);
434
435 s += strspn(s, WHITESPACE);
436 s = mangle_base(s, &base);
437
438 errno = 0;
439 l = strtol(s, &x, base);
440 if (errno > 0)
441 return -errno;
442 if (!x || x == s || *x != 0)
443 return -EINVAL;
444 if ((long) (int) l != l)
445 return -ERANGE;
446
447 if (ret_i)
448 *ret_i = (int) l;
449
450 return 0;
451 }
452
453 int safe_atollu_full(const char *s, unsigned base, unsigned long long *ret_llu) {
454 char *x = NULL;
455 unsigned long long l;
456
457 assert(s);
458 assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
459
460 if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
461 strchr(WHITESPACE, s[0]))
462 return -EINVAL;
463
464 s += strspn(s, WHITESPACE);
465
466 if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
467 IN_SET(s[0], '+', '-'))
468 return -EINVAL;
469
470 if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
471 s[0] == '0' && s[1] != 0)
472 return -EINVAL;
473
474 s = mangle_base(s, &base);
475
476 errno = 0;
477 l = strtoull(s, &x, SAFE_ATO_MASK_FLAGS(base));
478 if (errno > 0)
479 return -errno;
480 if (!x || x == s || *x != 0)
481 return -EINVAL;
482 if (l != 0 && s[0] == '-')
483 return -ERANGE;
484
485 if (ret_llu)
486 *ret_llu = l;
487
488 return 0;
489 }
490
491 int safe_atolli(const char *s, long long int *ret_lli) {
492 unsigned base = 0;
493 char *x = NULL;
494 long long l;
495
496 assert(s);
497
498 s += strspn(s, WHITESPACE);
499 s = mangle_base(s, &base);
500
501 errno = 0;
502 l = strtoll(s, &x, base);
503 if (errno > 0)
504 return -errno;
505 if (!x || x == s || *x != 0)
506 return -EINVAL;
507
508 if (ret_lli)
509 *ret_lli = l;
510
511 return 0;
512 }
513
514 int safe_atou8_full(const char *s, unsigned base, uint8_t *ret) {
515 unsigned u;
516 int r;
517
518 r = safe_atou_full(s, base, &u);
519 if (r < 0)
520 return r;
521 if (u > UINT8_MAX)
522 return -ERANGE;
523
524 *ret = (uint8_t) u;
525 return 0;
526 }
527
528 int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
529 unsigned u;
530 int r;
531
532 r = safe_atou_full(s, base, &u);
533 if (r < 0)
534 return r;
535 if (u > UINT16_MAX)
536 return -ERANGE;
537
538 *ret = (uint16_t) u;
539 return 0;
540 }
541
542 int safe_atoi16(const char *s, int16_t *ret) {
543 unsigned base = 0;
544 char *x = NULL;
545 long l;
546
547 assert(s);
548
549 s += strspn(s, WHITESPACE);
550 s = mangle_base(s, &base);
551
552 errno = 0;
553 l = strtol(s, &x, base);
554 if (errno > 0)
555 return -errno;
556 if (!x || x == s || *x != 0)
557 return -EINVAL;
558 if ((long) (int16_t) l != l)
559 return -ERANGE;
560
561 if (ret)
562 *ret = (int16_t) l;
563
564 return 0;
565 }
566
567 int safe_atod(const char *s, double *ret_d) {
568 _cleanup_(freelocalep) locale_t loc = (locale_t) 0;
569 char *x = NULL;
570 double d = 0;
571
572 assert(s);
573
574 loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0);
575 if (loc == (locale_t) 0)
576 return -errno;
577
578 errno = 0;
579 d = strtod_l(s, &x, loc);
580 if (errno > 0)
581 return -errno;
582 if (!x || x == s || *x != 0)
583 return -EINVAL;
584
585 if (ret_d)
586 *ret_d = (double) d;
587
588 return 0;
589 }
590
591 int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) {
592 unsigned val = 0;
593 const char *s;
594
595 s = *p;
596
597 /* accept any number of digits, strtoull is limited to 19 */
598 for (size_t i = 0; i < digits; i++,s++) {
599 if (!ascii_isdigit(*s)) {
600 if (i == 0)
601 return -EINVAL;
602
603 /* too few digits, pad with 0 */
604 for (; i < digits; i++)
605 val *= 10;
606
607 break;
608 }
609
610 val *= 10;
611 val += *s - '0';
612 }
613
614 /* maybe round up */
615 if (*s >= '5' && *s <= '9')
616 val++;
617
618 s += strspn(s, DIGITS);
619
620 *p = s;
621 *res = val;
622
623 return 0;
624 }
625
626 int parse_nice(const char *p, int *ret) {
627 int n, r;
628
629 r = safe_atoi(p, &n);
630 if (r < 0)
631 return r;
632
633 if (!nice_is_valid(n))
634 return -ERANGE;
635
636 *ret = n;
637 return 0;
638 }
639
640 int parse_ip_port(const char *s, uint16_t *ret) {
641 uint16_t l;
642 int r;
643
644 r = safe_atou16_full(s, SAFE_ATO_REFUSE_LEADING_WHITESPACE, &l);
645 if (r < 0)
646 return r;
647
648 if (l == 0)
649 return -EINVAL;
650
651 *ret = (uint16_t) l;
652
653 return 0;
654 }
655
656 int parse_ip_port_range(const char *s, uint16_t *low, uint16_t *high) {
657 unsigned l, h;
658 int r;
659
660 r = parse_range(s, &l, &h);
661 if (r < 0)
662 return r;
663
664 if (l <= 0 || l > 65535 || h <= 0 || h > 65535)
665 return -EINVAL;
666
667 if (h < l)
668 return -EINVAL;
669
670 *low = l;
671 *high = h;
672
673 return 0;
674 }
675
676 int parse_ip_prefix_length(const char *s, int *ret) {
677 unsigned l;
678 int r;
679
680 r = safe_atou(s, &l);
681 if (r < 0)
682 return r;
683
684 if (l > 128)
685 return -ERANGE;
686
687 *ret = (int) l;
688
689 return 0;
690 }
691
692 int parse_oom_score_adjust(const char *s, int *ret) {
693 int r, v;
694
695 assert(s);
696 assert(ret);
697
698 r = safe_atoi(s, &v);
699 if (r < 0)
700 return r;
701
702 if (!oom_score_adjust_is_valid(v))
703 return -ERANGE;
704
705 *ret = v;
706 return 0;
707 }
708
709 int store_loadavg_fixed_point(unsigned long i, unsigned long f, loadavg_t *ret) {
710 assert(ret);
711
712 if (i >= (~0UL << LOADAVG_PRECISION_BITS))
713 return -ERANGE;
714
715 i = i << LOADAVG_PRECISION_BITS;
716 f = DIV_ROUND_UP((f << LOADAVG_PRECISION_BITS), 100);
717
718 if (f >= LOADAVG_FIXED_POINT_1_0)
719 return -ERANGE;
720
721 *ret = i | f;
722 return 0;
723 }
724
725 int parse_loadavg_fixed_point(const char *s, loadavg_t *ret) {
726 const char *d, *f_str, *i_str;
727 unsigned long i, f;
728 int r;
729
730 assert(s);
731 assert(ret);
732
733 d = strchr(s, '.');
734 if (!d)
735 return -EINVAL;
736
737 i_str = strndupa_safe(s, d - s);
738 f_str = d + 1;
739
740 r = safe_atolu_full(i_str, 10, &i);
741 if (r < 0)
742 return r;
743
744 r = safe_atolu_full(f_str, 10, &f);
745 if (r < 0)
746 return r;
747
748 return store_loadavg_fixed_point(i, f, ret);
749 }