]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-util.c
Merge pull request #5191 from keszybz/tweaks
[thirdparty/systemd.git] / src / libudev / libudev-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <ctype.h>
21 #include <errno.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "libudev.h"
28
29 #include "MurmurHash2.h"
30 #include "device-nodes.h"
31 #include "libudev-private.h"
32 #include "syslog-util.h"
33 #include "utf8.h"
34
35 /**
36 * SECTION:libudev-util
37 * @short_description: utils
38 *
39 * Utilities useful when dealing with devices and device node names.
40 */
41
42 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
43 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
44 char *result, size_t maxsize, int read_value)
45 {
46 char temp[UTIL_PATH_SIZE];
47 char *subsys;
48 char *sysname;
49 struct udev_device *dev;
50 char *attr;
51
52 if (string[0] != '[')
53 return -1;
54
55 strscpy(temp, sizeof(temp), string);
56
57 subsys = &temp[1];
58
59 sysname = strchr(subsys, '/');
60 if (sysname == NULL)
61 return -1;
62 sysname[0] = '\0';
63 sysname = &sysname[1];
64
65 attr = strchr(sysname, ']');
66 if (attr == NULL)
67 return -1;
68 attr[0] = '\0';
69 attr = &attr[1];
70 if (attr[0] == '/')
71 attr = &attr[1];
72 if (attr[0] == '\0')
73 attr = NULL;
74
75 if (read_value && attr == NULL)
76 return -1;
77
78 dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
79 if (dev == NULL)
80 return -1;
81
82 if (read_value) {
83 const char *val;
84
85 val = udev_device_get_sysattr_value(dev, attr);
86 if (val != NULL)
87 strscpy(result, maxsize, val);
88 else
89 result[0] = '\0';
90 log_debug("value '[%s/%s]%s' is '%s'", subsys, sysname, attr, result);
91 } else {
92 size_t l;
93 char *s;
94
95 s = result;
96 l = strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL);
97 if (attr != NULL)
98 strpcpyl(&s, l, "/", attr, NULL);
99 log_debug("path '[%s/%s]%s' is '%s'", subsys, sysname, attr, result);
100 }
101 udev_device_unref(dev);
102 return 0;
103 }
104
105 int util_log_priority(const char *priority)
106 {
107 char *endptr;
108 int prio;
109
110 prio = strtoul(priority, &endptr, 10);
111 if (endptr[0] == '\0' || isspace(endptr[0])) {
112 if (prio >= 0 && prio <= 7)
113 return prio;
114 else
115 return -ERANGE;
116 }
117
118 return log_level_from_string(priority);
119 }
120
121 size_t util_path_encode(const char *src, char *dest, size_t size)
122 {
123 size_t i, j;
124
125 for (i = 0, j = 0; src[i] != '\0'; i++) {
126 if (src[i] == '/') {
127 if (j+4 >= size) {
128 j = 0;
129 break;
130 }
131 memcpy(&dest[j], "\\x2f", 4);
132 j += 4;
133 } else if (src[i] == '\\') {
134 if (j+4 >= size) {
135 j = 0;
136 break;
137 }
138 memcpy(&dest[j], "\\x5c", 4);
139 j += 4;
140 } else {
141 if (j+1 >= size) {
142 j = 0;
143 break;
144 }
145 dest[j] = src[i];
146 j++;
147 }
148 }
149 dest[j] = '\0';
150 return j;
151 }
152
153 void util_remove_trailing_chars(char *path, char c)
154 {
155 size_t len;
156
157 if (path == NULL)
158 return;
159 len = strlen(path);
160 while (len > 0 && path[len-1] == c)
161 path[--len] = '\0';
162 }
163
164 /*
165 * Copy from 'str' to 'to', while removing all leading and trailing whitespace,
166 * and replacing each run of consecutive whitespace with a single underscore.
167 * The chars from 'str' are copied up to the \0 at the end of the string, or
168 * at most 'len' chars. This appends \0 to 'to', at the end of the copied
169 * characters.
170 *
171 * If 'len' chars are copied into 'to', the final \0 is placed at len+1
172 * (i.e. 'to[len] = \0'), so the 'to' buffer must have at least len+1
173 * chars available.
174 *
175 * Note this may be called with 'str' == 'to', i.e. to replace whitespace
176 * in-place in a buffer. This function can handle that situation.
177 */
178 int util_replace_whitespace(const char *str, char *to, size_t len)
179 {
180 size_t i, j;
181
182 /* strip trailing whitespace */
183 len = strnlen(str, len);
184 while (len && isspace(str[len-1]))
185 len--;
186
187 /* strip leading whitespace */
188 i = 0;
189 while ((i < len) && isspace(str[i]))
190 i++;
191
192 j = 0;
193 while (i < len) {
194 /* substitute multiple whitespace with a single '_' */
195 if (isspace(str[i])) {
196 while (isspace(str[i]))
197 i++;
198 to[j++] = '_';
199 }
200 to[j++] = str[i++];
201 }
202 to[j] = '\0';
203 return j;
204 }
205
206 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
207 int util_replace_chars(char *str, const char *white)
208 {
209 size_t i = 0;
210 int replaced = 0;
211
212 while (str[i] != '\0') {
213 int len;
214
215 if (whitelisted_char_for_devnode(str[i], white)) {
216 i++;
217 continue;
218 }
219
220 /* accept hex encoding */
221 if (str[i] == '\\' && str[i+1] == 'x') {
222 i += 2;
223 continue;
224 }
225
226 /* accept valid utf8 */
227 len = utf8_encoded_valid_unichar(&str[i]);
228 if (len > 1) {
229 i += len;
230 continue;
231 }
232
233 /* if space is allowed, replace whitespace with ordinary space */
234 if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) {
235 str[i] = ' ';
236 i++;
237 replaced++;
238 continue;
239 }
240
241 /* everything else is replaced with '_' */
242 str[i] = '_';
243 i++;
244 replaced++;
245 }
246 return replaced;
247 }
248
249 /**
250 * udev_util_encode_string:
251 * @str: input string to be encoded
252 * @str_enc: output string to store the encoded input string
253 * @len: maximum size of the output string, which may be
254 * four times as long as the input string
255 *
256 * Encode all potentially unsafe characters of a string to the
257 * corresponding 2 char hex value prefixed by '\x'.
258 *
259 * Returns: 0 if the entire string was copied, non-zero otherwise.
260 **/
261 _public_ int udev_util_encode_string(const char *str, char *str_enc, size_t len)
262 {
263 return encode_devnode_name(str, str_enc, len);
264 }
265
266 unsigned int util_string_hash32(const char *str)
267 {
268 return MurmurHash2(str, strlen(str), 0);
269 }
270
271 /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
272 uint64_t util_string_bloom64(const char *str)
273 {
274 uint64_t bits = 0;
275 unsigned int hash = util_string_hash32(str);
276
277 bits |= 1LLU << (hash & 63);
278 bits |= 1LLU << ((hash >> 6) & 63);
279 bits |= 1LLU << ((hash >> 12) & 63);
280 bits |= 1LLU << ((hash >> 18) & 63);
281 return bits;
282 }