]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <inttypes.h> | |
5 | #include <linux/oom.h> | |
6 | #include <net/if.h> | |
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | #include <sys/socket.h> | |
10 | ||
11 | #include "alloc-util.h" | |
12 | #include "errno-list.h" | |
13 | #include "extract-word.h" | |
14 | #include "locale-util.h" | |
15 | #include "macro.h" | |
16 | #include "missing_network.h" | |
17 | #include "parse-util.h" | |
18 | #include "process-util.h" | |
19 | #include "stat-util.h" | |
20 | #include "string-util.h" | |
21 | #include "strv.h" | |
22 | ||
23 | int parse_boolean(const char *v) { | |
24 | if (!v) | |
25 | return -EINVAL; | |
26 | ||
27 | if (STRCASE_IN_SET(v, | |
28 | "1", | |
29 | "yes", | |
30 | "y", | |
31 | "true", | |
32 | "t", | |
33 | "on")) | |
34 | return 1; | |
35 | ||
36 | if (STRCASE_IN_SET(v, | |
37 | "0", | |
38 | "no", | |
39 | "n", | |
40 | "false", | |
41 | "f", | |
42 | "off")) | |
43 | return 0; | |
44 | ||
45 | return -EINVAL; | |
46 | } | |
47 | ||
48 | int 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 | ||
65 | if (!pid_is_valid(pid)) | |
66 | return -ERANGE; | |
67 | ||
68 | *ret_pid = pid; | |
69 | return 0; | |
70 | } | |
71 | ||
72 | int parse_mode(const char *s, mode_t *ret) { | |
73 | unsigned m; | |
74 | int r; | |
75 | ||
76 | assert(s); | |
77 | ||
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) | |
87 | return -ERANGE; | |
88 | ||
89 | if (ret) | |
90 | *ret = m; | |
91 | return 0; | |
92 | } | |
93 | ||
94 | int parse_ifindex(const char *s) { | |
95 | int ifi, r; | |
96 | ||
97 | assert(s); | |
98 | ||
99 | r = safe_atoi(s, &ifi); | |
100 | if (r < 0) | |
101 | return r; | |
102 | if (ifi <= 0) | |
103 | return -EINVAL; | |
104 | ||
105 | return ifi; | |
106 | } | |
107 | ||
108 | int 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 | ||
132 | int 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); | |
181 | assert(IN_SET(base, 1000, 1024)); | |
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); | |
200 | ||
201 | errno = 0; | |
202 | l = strtoull(p, &e, 10); | |
203 | if (errno > 0) | |
204 | return -errno; | |
205 | if (e == p) | |
206 | return -EINVAL; | |
207 | if (*p == '-') | |
208 | return -ERANGE; | |
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); | |
219 | if (errno > 0) | |
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 | ||
260 | int 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 | ||
297 | int 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 | ||
310 | /* 0 is also allowed here */ | |
311 | if (!errno_is_valid(e) && e != 0) | |
312 | return -ERANGE; | |
313 | ||
314 | return e; | |
315 | } | |
316 | ||
317 | int 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; | |
350 | *name = TAKE_PTR(n); | |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
355 | static 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 | ||
381 | int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) { | |
382 | char *x = NULL; | |
383 | unsigned long l; | |
384 | ||
385 | assert(s); | |
386 | assert(SAFE_ATO_MASK_FLAGS(base) <= 16); | |
387 | ||
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; | |
396 | ||
397 | s += strspn(s, WHITESPACE); | |
398 | ||
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 | ||
412 | s = mangle_base(s, &base); | |
413 | ||
414 | errno = 0; | |
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 */); | |
417 | if (errno > 0) | |
418 | return -errno; | |
419 | if (!x || x == s || *x != 0) | |
420 | return -EINVAL; | |
421 | if (l != 0 && s[0] == '-') | |
422 | return -ERANGE; | |
423 | if ((unsigned long) (unsigned) l != l) | |
424 | return -ERANGE; | |
425 | ||
426 | if (ret_u) | |
427 | *ret_u = (unsigned) l; | |
428 | ||
429 | return 0; | |
430 | } | |
431 | ||
432 | int safe_atoi(const char *s, int *ret_i) { | |
433 | unsigned base = 0; | |
434 | char *x = NULL; | |
435 | long l; | |
436 | ||
437 | assert(s); | |
438 | ||
439 | s += strspn(s, WHITESPACE); | |
440 | s = mangle_base(s, &base); | |
441 | ||
442 | errno = 0; | |
443 | l = strtol(s, &x, base); | |
444 | if (errno > 0) | |
445 | return -errno; | |
446 | if (!x || x == s || *x != 0) | |
447 | return -EINVAL; | |
448 | if ((long) (int) l != l) | |
449 | return -ERANGE; | |
450 | ||
451 | if (ret_i) | |
452 | *ret_i = (int) l; | |
453 | ||
454 | return 0; | |
455 | } | |
456 | ||
457 | int safe_atollu_full(const char *s, unsigned base, long long unsigned *ret_llu) { | |
458 | char *x = NULL; | |
459 | unsigned long long l; | |
460 | ||
461 | assert(s); | |
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; | |
467 | ||
468 | s += strspn(s, WHITESPACE); | |
469 | ||
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 | ||
478 | s = mangle_base(s, &base); | |
479 | ||
480 | errno = 0; | |
481 | l = strtoull(s, &x, SAFE_ATO_MASK_FLAGS(base)); | |
482 | if (errno > 0) | |
483 | return -errno; | |
484 | if (!x || x == s || *x != 0) | |
485 | return -EINVAL; | |
486 | if (l != 0 && s[0] == '-') | |
487 | return -ERANGE; | |
488 | ||
489 | if (ret_llu) | |
490 | *ret_llu = l; | |
491 | ||
492 | return 0; | |
493 | } | |
494 | ||
495 | int safe_atolli(const char *s, long long int *ret_lli) { | |
496 | unsigned base = 0; | |
497 | char *x = NULL; | |
498 | long long l; | |
499 | ||
500 | assert(s); | |
501 | ||
502 | s += strspn(s, WHITESPACE); | |
503 | s = mangle_base(s, &base); | |
504 | ||
505 | errno = 0; | |
506 | l = strtoll(s, &x, base); | |
507 | if (errno > 0) | |
508 | return -errno; | |
509 | if (!x || x == s || *x != 0) | |
510 | return -EINVAL; | |
511 | ||
512 | if (ret_lli) | |
513 | *ret_lli = l; | |
514 | ||
515 | return 0; | |
516 | } | |
517 | ||
518 | int safe_atou8(const char *s, uint8_t *ret) { | |
519 | unsigned base = 0; | |
520 | unsigned long l; | |
521 | char *x = NULL; | |
522 | ||
523 | assert(s); | |
524 | ||
525 | s += strspn(s, WHITESPACE); | |
526 | s = mangle_base(s, &base); | |
527 | ||
528 | errno = 0; | |
529 | l = strtoul(s, &x, base); | |
530 | if (errno > 0) | |
531 | return -errno; | |
532 | if (!x || x == s || *x != 0) | |
533 | return -EINVAL; | |
534 | if (l != 0 && s[0] == '-') | |
535 | return -ERANGE; | |
536 | if ((unsigned long) (uint8_t) l != l) | |
537 | return -ERANGE; | |
538 | ||
539 | if (ret) | |
540 | *ret = (uint8_t) l; | |
541 | return 0; | |
542 | } | |
543 | ||
544 | int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) { | |
545 | char *x = NULL; | |
546 | unsigned long l; | |
547 | ||
548 | assert(s); | |
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; | |
554 | ||
555 | s += strspn(s, WHITESPACE); | |
556 | ||
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 | ||
565 | s = mangle_base(s, &base); | |
566 | ||
567 | errno = 0; | |
568 | l = strtoul(s, &x, SAFE_ATO_MASK_FLAGS(base)); | |
569 | if (errno > 0) | |
570 | return -errno; | |
571 | if (!x || x == s || *x != 0) | |
572 | return -EINVAL; | |
573 | if (l != 0 && s[0] == '-') | |
574 | return -ERANGE; | |
575 | if ((unsigned long) (uint16_t) l != l) | |
576 | return -ERANGE; | |
577 | ||
578 | if (ret) | |
579 | *ret = (uint16_t) l; | |
580 | ||
581 | return 0; | |
582 | } | |
583 | ||
584 | int safe_atoi16(const char *s, int16_t *ret) { | |
585 | unsigned base = 0; | |
586 | char *x = NULL; | |
587 | long l; | |
588 | ||
589 | assert(s); | |
590 | ||
591 | s += strspn(s, WHITESPACE); | |
592 | s = mangle_base(s, &base); | |
593 | ||
594 | errno = 0; | |
595 | l = strtol(s, &x, base); | |
596 | if (errno > 0) | |
597 | return -errno; | |
598 | if (!x || x == s || *x != 0) | |
599 | return -EINVAL; | |
600 | if ((long) (int16_t) l != l) | |
601 | return -ERANGE; | |
602 | ||
603 | if (ret) | |
604 | *ret = (int16_t) l; | |
605 | ||
606 | return 0; | |
607 | } | |
608 | ||
609 | int safe_atod(const char *s, double *ret_d) { | |
610 | _cleanup_(freelocalep) locale_t loc = (locale_t) 0; | |
611 | char *x = NULL; | |
612 | double d = 0; | |
613 | ||
614 | assert(s); | |
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); | |
622 | if (errno > 0) | |
623 | return -errno; | |
624 | if (!x || x == s || *x != 0) | |
625 | return -EINVAL; | |
626 | ||
627 | if (ret_d) | |
628 | *ret_d = (double) d; | |
629 | ||
630 | return 0; | |
631 | } | |
632 | ||
633 | int 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 | ||
640 | /* accept any number of digits, strtoull is limited to 19 */ | |
641 | for (i=0; i < digits; i++,s++) { | |
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 | } | |
668 | ||
669 | int parse_percent_unbounded(const char *p) { | |
670 | const char *pc, *n; | |
671 | int r, v; | |
672 | ||
673 | pc = endswith(p, "%"); | |
674 | if (!pc) | |
675 | return -EINVAL; | |
676 | ||
677 | n = strndupa(p, pc - p); | |
678 | r = safe_atoi(n, &v); | |
679 | if (r < 0) | |
680 | return r; | |
681 | if (v < 0) | |
682 | return -ERANGE; | |
683 | ||
684 | return v; | |
685 | } | |
686 | ||
687 | int parse_percent(const char *p) { | |
688 | int v; | |
689 | ||
690 | v = parse_percent_unbounded(p); | |
691 | if (v > 100) | |
692 | return -ERANGE; | |
693 | ||
694 | return v; | |
695 | } | |
696 | ||
697 | int 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; | |
707 | if (v < 0) | |
708 | return -ERANGE; | |
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; | |
729 | if (v < 0) | |
730 | return -ERANGE; | |
731 | if (v > (INT_MAX - q) / 10) | |
732 | return -ERANGE; | |
733 | ||
734 | v = v * 10 + q; | |
735 | } | |
736 | ||
737 | return v; | |
738 | } | |
739 | ||
740 | int 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 | ||
750 | int 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 | } | |
763 | ||
764 | int 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 | } | |
779 | ||
780 | int 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 | ||
800 | int 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 | ||
816 | int parse_dev(const char *s, dev_t *ret) { | |
817 | const char *major; | |
818 | unsigned x, y; | |
819 | size_t n; | |
820 | int r; | |
821 | ||
822 | n = strspn(s, DIGITS); | |
823 | if (n == 0) | |
824 | return -EINVAL; | |
825 | if (s[n] != ':') | |
826 | return -EINVAL; | |
827 | ||
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); | |
841 | return 0; | |
842 | } | |
843 | ||
844 | int 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 | } |