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