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