]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <stdio.h> | |
4 | #include <string.h> | |
5 | #include <sys/stat.h> | |
6 | ||
7 | #include "device-nodes.h" | |
8 | #include "path-util.h" | |
9 | #include "string-util.h" | |
10 | #include "utf8.h" | |
11 | ||
12 | int allow_listed_char_for_devnode(char c, const char *additional) { | |
13 | return | |
14 | ascii_isdigit(c) || | |
15 | ascii_isalpha(c) || | |
16 | strchr("#+-.:=@_", c) || | |
17 | (additional && strchr(additional, c)); | |
18 | } | |
19 | ||
20 | int encode_devnode_name(const char *str, char *str_enc, size_t len) { | |
21 | size_t i, j; | |
22 | ||
23 | if (!str || !str_enc) | |
24 | return -EINVAL; | |
25 | ||
26 | for (i = 0, j = 0; str[i] != '\0'; i++) { | |
27 | int seqlen; | |
28 | ||
29 | seqlen = utf8_encoded_valid_unichar(str + i, SIZE_MAX); | |
30 | if (seqlen > 1) { | |
31 | ||
32 | if (len-j < (size_t) seqlen) | |
33 | return -EINVAL; | |
34 | ||
35 | memcpy(&str_enc[j], &str[i], seqlen); | |
36 | j += seqlen; | |
37 | i += (seqlen-1); | |
38 | ||
39 | } else if (str[i] == '\\' || !allow_listed_char_for_devnode(str[i], NULL)) { | |
40 | ||
41 | if (len-j < 4) | |
42 | return -EINVAL; | |
43 | ||
44 | sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]); | |
45 | j += 4; | |
46 | ||
47 | } else { | |
48 | if (len-j < 1) | |
49 | return -EINVAL; | |
50 | ||
51 | str_enc[j] = str[i]; | |
52 | j++; | |
53 | } | |
54 | } | |
55 | ||
56 | if (len-j < 1) | |
57 | return -EINVAL; | |
58 | ||
59 | str_enc[j] = '\0'; | |
60 | return 0; | |
61 | } | |
62 | ||
63 | int devnode_same(const char *a, const char *b) { | |
64 | struct stat sa, sb; | |
65 | ||
66 | assert(a); | |
67 | assert(b); | |
68 | ||
69 | if (!valid_device_node_path(a) || !valid_device_node_path(b)) | |
70 | return -EINVAL; | |
71 | ||
72 | if (stat(a, &sa) < 0) | |
73 | return -errno; | |
74 | if (stat(b, &sb) < 0) | |
75 | return -errno; | |
76 | ||
77 | if (!S_ISBLK(sa.st_mode) && !S_ISCHR(sa.st_mode)) | |
78 | return -ENODEV; | |
79 | if (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)) | |
80 | return -ENODEV; | |
81 | ||
82 | if (((sa.st_mode ^ sb.st_mode) & S_IFMT) != 0) /* both inode same device node type? */ | |
83 | return false; | |
84 | ||
85 | return sa.st_rdev == sb.st_rdev; | |
86 | } |