]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | /*** | |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <errno.h> | |
22 | #include <inttypes.h> | |
23 | #include <locale.h> | |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | ||
28 | #include "alloc-util.h" | |
29 | #include "errno-list.h" | |
30 | #include "extract-word.h" | |
31 | #include "locale-util.h" | |
32 | #include "macro.h" | |
33 | #include "parse-util.h" | |
34 | #include "process-util.h" | |
35 | #include "string-util.h" | |
36 | ||
37 | int parse_boolean(const char *v) { | |
38 | assert(v); | |
39 | ||
40 | if (streq(v, "1") || strcaseeq(v, "yes") || strcaseeq(v, "y") || strcaseeq(v, "true") || strcaseeq(v, "t") || strcaseeq(v, "on")) | |
41 | return 1; | |
42 | else if (streq(v, "0") || strcaseeq(v, "no") || strcaseeq(v, "n") || strcaseeq(v, "false") || strcaseeq(v, "f") || strcaseeq(v, "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 | char *x; | |
74 | long l; | |
75 | ||
76 | assert(s); | |
77 | assert(ret); | |
78 | ||
79 | s += strspn(s, WHITESPACE); | |
80 | if (s[0] == '-') | |
81 | return -ERANGE; | |
82 | ||
83 | errno = 0; | |
84 | l = strtol(s, &x, 8); | |
85 | if (errno > 0) | |
86 | return -errno; | |
87 | if (!x || x == s || *x != 0) | |
88 | return -EINVAL; | |
89 | if (l < 0 || l > 07777) | |
90 | return -ERANGE; | |
91 | ||
92 | *ret = (mode_t) l; | |
93 | return 0; | |
94 | } | |
95 | ||
96 | int parse_ifindex(const char *s, int *ret) { | |
97 | int ifi, r; | |
98 | ||
99 | r = safe_atoi(s, &ifi); | |
100 | if (r < 0) | |
101 | return r; | |
102 | if (ifi <= 0) | |
103 | return -EINVAL; | |
104 | ||
105 | *ret = ifi; | |
106 | return 0; | |
107 | } | |
108 | ||
109 | int parse_size(const char *t, uint64_t base, uint64_t *size) { | |
110 | ||
111 | /* Soo, sometimes we want to parse IEC binary suffixes, and | |
112 | * sometimes SI decimal suffixes. This function can parse | |
113 | * both. Which one is the right way depends on the | |
114 | * context. Wikipedia suggests that SI is customary for | |
115 | * hardware metrics and network speeds, while IEC is | |
116 | * customary for most data sizes used by software and volatile | |
117 | * (RAM) memory. Hence be careful which one you pick! | |
118 | * | |
119 | * In either case we use just K, M, G as suffix, and not Ki, | |
120 | * Mi, Gi or so (as IEC would suggest). That's because that's | |
121 | * frickin' ugly. But this means you really need to make sure | |
122 | * to document which base you are parsing when you use this | |
123 | * call. */ | |
124 | ||
125 | struct table { | |
126 | const char *suffix; | |
127 | unsigned long long factor; | |
128 | }; | |
129 | ||
130 | static const struct table iec[] = { | |
131 | { "E", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL*1024ULL }, | |
132 | { "P", 1024ULL*1024ULL*1024ULL*1024ULL*1024ULL }, | |
133 | { "T", 1024ULL*1024ULL*1024ULL*1024ULL }, | |
134 | { "G", 1024ULL*1024ULL*1024ULL }, | |
135 | { "M", 1024ULL*1024ULL }, | |
136 | { "K", 1024ULL }, | |
137 | { "B", 1ULL }, | |
138 | { "", 1ULL }, | |
139 | }; | |
140 | ||
141 | static const struct table si[] = { | |
142 | { "E", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL*1000ULL }, | |
143 | { "P", 1000ULL*1000ULL*1000ULL*1000ULL*1000ULL }, | |
144 | { "T", 1000ULL*1000ULL*1000ULL*1000ULL }, | |
145 | { "G", 1000ULL*1000ULL*1000ULL }, | |
146 | { "M", 1000ULL*1000ULL }, | |
147 | { "K", 1000ULL }, | |
148 | { "B", 1ULL }, | |
149 | { "", 1ULL }, | |
150 | }; | |
151 | ||
152 | const struct table *table; | |
153 | const char *p; | |
154 | unsigned long long r = 0; | |
155 | unsigned n_entries, start_pos = 0; | |
156 | ||
157 | assert(t); | |
158 | assert(IN_SET(base, 1000, 1024)); | |
159 | assert(size); | |
160 | ||
161 | if (base == 1000) { | |
162 | table = si; | |
163 | n_entries = ELEMENTSOF(si); | |
164 | } else { | |
165 | table = iec; | |
166 | n_entries = ELEMENTSOF(iec); | |
167 | } | |
168 | ||
169 | p = t; | |
170 | do { | |
171 | unsigned long long l, tmp; | |
172 | double frac = 0; | |
173 | char *e; | |
174 | unsigned i; | |
175 | ||
176 | p += strspn(p, WHITESPACE); | |
177 | ||
178 | errno = 0; | |
179 | l = strtoull(p, &e, 10); | |
180 | if (errno > 0) | |
181 | return -errno; | |
182 | if (e == p) | |
183 | return -EINVAL; | |
184 | if (*p == '-') | |
185 | return -ERANGE; | |
186 | ||
187 | if (*e == '.') { | |
188 | e++; | |
189 | ||
190 | /* strtoull() itself would accept space/+/- */ | |
191 | if (*e >= '0' && *e <= '9') { | |
192 | unsigned long long l2; | |
193 | char *e2; | |
194 | ||
195 | l2 = strtoull(e, &e2, 10); | |
196 | if (errno > 0) | |
197 | return -errno; | |
198 | ||
199 | /* Ignore failure. E.g. 10.M is valid */ | |
200 | frac = l2; | |
201 | for (; e < e2; e++) | |
202 | frac /= 10; | |
203 | } | |
204 | } | |
205 | ||
206 | e += strspn(e, WHITESPACE); | |
207 | ||
208 | for (i = start_pos; i < n_entries; i++) | |
209 | if (startswith(e, table[i].suffix)) | |
210 | break; | |
211 | ||
212 | if (i >= n_entries) | |
213 | return -EINVAL; | |
214 | ||
215 | if (l + (frac > 0) > ULLONG_MAX / table[i].factor) | |
216 | return -ERANGE; | |
217 | ||
218 | tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor); | |
219 | if (tmp > ULLONG_MAX - r) | |
220 | return -ERANGE; | |
221 | ||
222 | r += tmp; | |
223 | if ((unsigned long long) (uint64_t) r != r) | |
224 | return -ERANGE; | |
225 | ||
226 | p = e + strlen(table[i].suffix); | |
227 | ||
228 | start_pos = i + 1; | |
229 | ||
230 | } while (*p); | |
231 | ||
232 | *size = r; | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | int parse_range(const char *t, unsigned *lower, unsigned *upper) { | |
238 | _cleanup_free_ char *word = NULL; | |
239 | unsigned l, u; | |
240 | int r; | |
241 | ||
242 | assert(lower); | |
243 | assert(upper); | |
244 | ||
245 | /* Extract the lower bound. */ | |
246 | r = extract_first_word(&t, &word, "-", EXTRACT_DONT_COALESCE_SEPARATORS); | |
247 | if (r < 0) | |
248 | return r; | |
249 | if (r == 0) | |
250 | return -EINVAL; | |
251 | ||
252 | r = safe_atou(word, &l); | |
253 | if (r < 0) | |
254 | return r; | |
255 | ||
256 | /* Check for the upper bound and extract it if needed */ | |
257 | if (!t) | |
258 | /* Single number with no dashes. */ | |
259 | u = l; | |
260 | else if (!*t) | |
261 | /* Trailing dash is an error. */ | |
262 | return -EINVAL; | |
263 | else { | |
264 | r = safe_atou(t, &u); | |
265 | if (r < 0) | |
266 | return r; | |
267 | } | |
268 | ||
269 | *lower = l; | |
270 | *upper = u; | |
271 | return 0; | |
272 | } | |
273 | ||
274 | int parse_errno(const char *t) { | |
275 | int r, e; | |
276 | ||
277 | assert(t); | |
278 | ||
279 | r = errno_from_name(t); | |
280 | if (r > 0) | |
281 | return r; | |
282 | ||
283 | r = safe_atoi(t, &e); | |
284 | if (r < 0) | |
285 | return r; | |
286 | ||
287 | /* 0 is also allowed here */ | |
288 | if (!errno_is_valid(e) && e != 0) | |
289 | return -ERANGE; | |
290 | ||
291 | return e; | |
292 | } | |
293 | ||
294 | int parse_syscall_and_errno(const char *in, char **name, int *error) { | |
295 | _cleanup_free_ char *n = NULL; | |
296 | char *p; | |
297 | int e = -1; | |
298 | ||
299 | assert(in); | |
300 | assert(name); | |
301 | assert(error); | |
302 | ||
303 | /* | |
304 | * This parse "syscall:errno" like "uname:EILSEQ", "@sync:255". | |
305 | * If errno is omitted, then error is set to -1. | |
306 | * Empty syscall name is not allowed. | |
307 | * Here, we do not check that the syscall name is valid or not. | |
308 | */ | |
309 | ||
310 | p = strchr(in, ':'); | |
311 | if (p) { | |
312 | e = parse_errno(p + 1); | |
313 | if (e < 0) | |
314 | return e; | |
315 | ||
316 | n = strndup(in, p - in); | |
317 | } else | |
318 | n = strdup(in); | |
319 | ||
320 | if (!n) | |
321 | return -ENOMEM; | |
322 | ||
323 | if (isempty(n)) | |
324 | return -EINVAL; | |
325 | ||
326 | *error = e; | |
327 | *name = n; | |
328 | n = NULL; | |
329 | ||
330 | return 0; | |
331 | } | |
332 | ||
333 | char *format_bytes(char *buf, size_t l, uint64_t t) { | |
334 | unsigned i; | |
335 | ||
336 | /* This only does IEC units so far */ | |
337 | ||
338 | static const struct { | |
339 | const char *suffix; | |
340 | uint64_t factor; | |
341 | } table[] = { | |
342 | { "E", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) }, | |
343 | { "P", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) }, | |
344 | { "T", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) }, | |
345 | { "G", UINT64_C(1024)*UINT64_C(1024)*UINT64_C(1024) }, | |
346 | { "M", UINT64_C(1024)*UINT64_C(1024) }, | |
347 | { "K", UINT64_C(1024) }, | |
348 | }; | |
349 | ||
350 | if (t == (uint64_t) -1) | |
351 | return NULL; | |
352 | ||
353 | for (i = 0; i < ELEMENTSOF(table); i++) { | |
354 | ||
355 | if (t >= table[i].factor) { | |
356 | snprintf(buf, l, | |
357 | "%" PRIu64 ".%" PRIu64 "%s", | |
358 | t / table[i].factor, | |
359 | ((t*UINT64_C(10)) / table[i].factor) % UINT64_C(10), | |
360 | table[i].suffix); | |
361 | ||
362 | goto finish; | |
363 | } | |
364 | } | |
365 | ||
366 | snprintf(buf, l, "%" PRIu64 "B", t); | |
367 | ||
368 | finish: | |
369 | buf[l-1] = 0; | |
370 | return buf; | |
371 | ||
372 | } | |
373 | ||
374 | int safe_atou(const char *s, unsigned *ret_u) { | |
375 | char *x = NULL; | |
376 | unsigned long l; | |
377 | ||
378 | assert(s); | |
379 | assert(ret_u); | |
380 | ||
381 | /* strtoul() is happy to parse negative values, and silently | |
382 | * converts them to unsigned values without generating an | |
383 | * error. We want a clean error, hence let's look for the "-" | |
384 | * prefix on our own, and generate an error. But let's do so | |
385 | * only after strtoul() validated that the string is clean | |
386 | * otherwise, so that we return EINVAL preferably over | |
387 | * ERANGE. */ | |
388 | ||
389 | s += strspn(s, WHITESPACE); | |
390 | ||
391 | errno = 0; | |
392 | l = strtoul(s, &x, 0); | |
393 | if (errno > 0) | |
394 | return -errno; | |
395 | if (!x || x == s || *x != 0) | |
396 | return -EINVAL; | |
397 | if (s[0] == '-') | |
398 | return -ERANGE; | |
399 | if ((unsigned long) (unsigned) l != l) | |
400 | return -ERANGE; | |
401 | ||
402 | *ret_u = (unsigned) l; | |
403 | return 0; | |
404 | } | |
405 | ||
406 | int safe_atoi(const char *s, int *ret_i) { | |
407 | char *x = NULL; | |
408 | long l; | |
409 | ||
410 | assert(s); | |
411 | assert(ret_i); | |
412 | ||
413 | errno = 0; | |
414 | l = strtol(s, &x, 0); | |
415 | if (errno > 0) | |
416 | return -errno; | |
417 | if (!x || x == s || *x != 0) | |
418 | return -EINVAL; | |
419 | if ((long) (int) l != l) | |
420 | return -ERANGE; | |
421 | ||
422 | *ret_i = (int) l; | |
423 | return 0; | |
424 | } | |
425 | ||
426 | int safe_atollu(const char *s, long long unsigned *ret_llu) { | |
427 | char *x = NULL; | |
428 | unsigned long long l; | |
429 | ||
430 | assert(s); | |
431 | assert(ret_llu); | |
432 | ||
433 | s += strspn(s, WHITESPACE); | |
434 | ||
435 | errno = 0; | |
436 | l = strtoull(s, &x, 0); | |
437 | if (errno > 0) | |
438 | return -errno; | |
439 | if (!x || x == s || *x != 0) | |
440 | return -EINVAL; | |
441 | if (*s == '-') | |
442 | return -ERANGE; | |
443 | ||
444 | *ret_llu = l; | |
445 | return 0; | |
446 | } | |
447 | ||
448 | int safe_atolli(const char *s, long long int *ret_lli) { | |
449 | char *x = NULL; | |
450 | long long l; | |
451 | ||
452 | assert(s); | |
453 | assert(ret_lli); | |
454 | ||
455 | errno = 0; | |
456 | l = strtoll(s, &x, 0); | |
457 | if (errno > 0) | |
458 | return -errno; | |
459 | if (!x || x == s || *x != 0) | |
460 | return -EINVAL; | |
461 | ||
462 | *ret_lli = l; | |
463 | return 0; | |
464 | } | |
465 | ||
466 | int safe_atou8(const char *s, uint8_t *ret) { | |
467 | char *x = NULL; | |
468 | unsigned long l; | |
469 | ||
470 | assert(s); | |
471 | assert(ret); | |
472 | ||
473 | s += strspn(s, WHITESPACE); | |
474 | ||
475 | errno = 0; | |
476 | l = strtoul(s, &x, 0); | |
477 | if (errno > 0) | |
478 | return -errno; | |
479 | if (!x || x == s || *x != 0) | |
480 | return -EINVAL; | |
481 | if (s[0] == '-') | |
482 | return -ERANGE; | |
483 | if ((unsigned long) (uint8_t) l != l) | |
484 | return -ERANGE; | |
485 | ||
486 | *ret = (uint8_t) l; | |
487 | return 0; | |
488 | } | |
489 | ||
490 | int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) { | |
491 | char *x = NULL; | |
492 | unsigned long l; | |
493 | ||
494 | assert(s); | |
495 | assert(ret); | |
496 | assert(base <= 16); | |
497 | ||
498 | s += strspn(s, WHITESPACE); | |
499 | ||
500 | errno = 0; | |
501 | l = strtoul(s, &x, base); | |
502 | if (errno > 0) | |
503 | return -errno; | |
504 | if (!x || x == s || *x != 0) | |
505 | return -EINVAL; | |
506 | if (s[0] == '-') | |
507 | return -ERANGE; | |
508 | if ((unsigned long) (uint16_t) l != l) | |
509 | return -ERANGE; | |
510 | ||
511 | *ret = (uint16_t) l; | |
512 | return 0; | |
513 | } | |
514 | ||
515 | int safe_atoi16(const char *s, int16_t *ret) { | |
516 | char *x = NULL; | |
517 | long l; | |
518 | ||
519 | assert(s); | |
520 | assert(ret); | |
521 | ||
522 | errno = 0; | |
523 | l = strtol(s, &x, 0); | |
524 | if (errno > 0) | |
525 | return -errno; | |
526 | if (!x || x == s || *x != 0) | |
527 | return -EINVAL; | |
528 | if ((long) (int16_t) l != l) | |
529 | return -ERANGE; | |
530 | ||
531 | *ret = (int16_t) l; | |
532 | return 0; | |
533 | } | |
534 | ||
535 | int safe_atod(const char *s, double *ret_d) { | |
536 | _cleanup_(freelocalep) locale_t loc = (locale_t) 0; | |
537 | char *x = NULL; | |
538 | double d = 0; | |
539 | ||
540 | assert(s); | |
541 | assert(ret_d); | |
542 | ||
543 | loc = newlocale(LC_NUMERIC_MASK, "C", (locale_t) 0); | |
544 | if (loc == (locale_t) 0) | |
545 | return -errno; | |
546 | ||
547 | errno = 0; | |
548 | d = strtod_l(s, &x, loc); | |
549 | if (errno > 0) | |
550 | return -errno; | |
551 | if (!x || x == s || *x != 0) | |
552 | return -EINVAL; | |
553 | ||
554 | *ret_d = (double) d; | |
555 | return 0; | |
556 | } | |
557 | ||
558 | int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) { | |
559 | size_t i; | |
560 | unsigned val = 0; | |
561 | const char *s; | |
562 | ||
563 | s = *p; | |
564 | ||
565 | /* accept any number of digits, strtoull is limted to 19 */ | |
566 | for (i=0; i < digits; i++,s++) { | |
567 | if (*s < '0' || *s > '9') { | |
568 | if (i == 0) | |
569 | return -EINVAL; | |
570 | ||
571 | /* too few digits, pad with 0 */ | |
572 | for (; i < digits; i++) | |
573 | val *= 10; | |
574 | ||
575 | break; | |
576 | } | |
577 | ||
578 | val *= 10; | |
579 | val += *s - '0'; | |
580 | } | |
581 | ||
582 | /* maybe round up */ | |
583 | if (*s >= '5' && *s <= '9') | |
584 | val++; | |
585 | ||
586 | s += strspn(s, DIGITS); | |
587 | ||
588 | *p = s; | |
589 | *res = val; | |
590 | ||
591 | return 0; | |
592 | } | |
593 | ||
594 | int parse_percent_unbounded(const char *p) { | |
595 | const char *pc, *n; | |
596 | int r, v; | |
597 | ||
598 | pc = endswith(p, "%"); | |
599 | if (!pc) | |
600 | return -EINVAL; | |
601 | ||
602 | n = strndupa(p, pc - p); | |
603 | r = safe_atoi(n, &v); | |
604 | if (r < 0) | |
605 | return r; | |
606 | if (v < 0) | |
607 | return -ERANGE; | |
608 | ||
609 | return v; | |
610 | } | |
611 | ||
612 | int parse_percent(const char *p) { | |
613 | int v; | |
614 | ||
615 | v = parse_percent_unbounded(p); | |
616 | if (v > 100) | |
617 | return -ERANGE; | |
618 | ||
619 | return v; | |
620 | } | |
621 | ||
622 | int parse_nice(const char *p, int *ret) { | |
623 | int n, r; | |
624 | ||
625 | r = safe_atoi(p, &n); | |
626 | if (r < 0) | |
627 | return r; | |
628 | ||
629 | if (!nice_is_valid(n)) | |
630 | return -ERANGE; | |
631 | ||
632 | *ret = n; | |
633 | return 0; | |
634 | } | |
635 | ||
636 | int parse_ip_port(const char *s, uint16_t *ret) { | |
637 | uint16_t l; | |
638 | int r; | |
639 | ||
640 | r = safe_atou16(s, &l); | |
641 | if (r < 0) | |
642 | return r; | |
643 | ||
644 | if (l == 0) | |
645 | return -EINVAL; | |
646 | ||
647 | *ret = (uint16_t) l; | |
648 | ||
649 | return 0; | |
650 | } | |
651 | ||
652 | int parse_dev(const char *s, dev_t *ret) { | |
653 | unsigned x, y; | |
654 | dev_t d; | |
655 | ||
656 | if (sscanf(s, "%u:%u", &x, &y) != 2) | |
657 | return -EINVAL; | |
658 | ||
659 | d = makedev(x, y); | |
660 | if ((unsigned) major(d) != x || (unsigned) minor(d) != y) | |
661 | return -EINVAL; | |
662 | ||
663 | *ret = d; | |
664 | return 0; | |
665 | } |