]>
Commit | Line | Data |
---|---|---|
88a6477e KS |
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 | ***/ | |
eb1f0e66 | 19 | |
eb1f0e66 KS |
20 | #include <stdio.h> |
21 | #include <stdlib.h> | |
22 | #include <stddef.h> | |
23 | #include <unistd.h> | |
24 | #include <errno.h> | |
25 | #include <string.h> | |
26 | #include <dirent.h> | |
75250977 | 27 | #include <ctype.h> |
93b0f384 | 28 | #include <fcntl.h> |
9c6a11b1 | 29 | #include <time.h> |
9e13dbae KS |
30 | #include <pwd.h> |
31 | #include <grp.h> | |
eb1f0e66 | 32 | #include <sys/stat.h> |
9e13dbae | 33 | #include <sys/param.h> |
eb1f0e66 | 34 | |
8f6ce71f | 35 | #include "device-nodes.h" |
eb1f0e66 KS |
36 | #include "libudev.h" |
37 | #include "libudev-private.h" | |
02a36bc9 | 38 | #include "utf8.h" |
57d0e6b2 | 39 | #include "MurmurHash2.h" |
eb1f0e66 | 40 | |
0bbe8838 KS |
41 | /** |
42 | * SECTION:libudev-util | |
43 | * @short_description: utils | |
21dbe43a KS |
44 | * |
45 | * Utilities useful when dealing with devices and device node names. | |
0bbe8838 KS |
46 | */ |
47 | ||
9e13dbae KS |
48 | int util_delete_path(struct udev *udev, const char *path) |
49 | { | |
50 | char p[UTIL_PATH_SIZE]; | |
51 | char *pos; | |
52 | int err = 0; | |
53 | ||
54 | if (path[0] == '/') | |
55 | while(path[1] == '/') | |
56 | path++; | |
d5a89d7d | 57 | strscpy(p, sizeof(p), path); |
9e13dbae KS |
58 | pos = strrchr(p, '/'); |
59 | if (pos == p || pos == NULL) | |
60 | return 0; | |
61 | ||
62 | for (;;) { | |
63 | *pos = '\0'; | |
64 | pos = strrchr(p, '/'); | |
65 | ||
66 | /* don't remove the last one */ | |
67 | if ((pos == p) || (pos == NULL)) | |
68 | break; | |
69 | ||
70 | err = rmdir(p); | |
71 | if (err < 0) { | |
72 | if (errno == ENOENT) | |
73 | err = 0; | |
74 | break; | |
75 | } | |
76 | } | |
77 | return err; | |
78 | } | |
79 | ||
80 | uid_t util_lookup_user(struct udev *udev, const char *user) | |
81 | { | |
82 | char *endptr; | |
83 | struct passwd pwbuf; | |
84 | struct passwd *pw; | |
85 | uid_t uid; | |
86 | size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); | |
87 | char *buf = alloca(buflen); | |
88 | ||
090be865 | 89 | if (streq(user, "root")) |
9e13dbae KS |
90 | return 0; |
91 | uid = strtoul(user, &endptr, 10); | |
92 | if (endptr[0] == '\0') | |
93 | return uid; | |
94 | ||
95 | errno = getpwnam_r(user, &pwbuf, buf, buflen, &pw); | |
96 | if (pw != NULL) | |
97 | return pw->pw_uid; | |
98 | if (errno == 0 || errno == ENOENT || errno == ESRCH) | |
c8f8394a | 99 | udev_err(udev, "specified user '%s' unknown\n", user); |
9e13dbae | 100 | else |
c8f8394a | 101 | udev_err(udev, "error resolving user '%s': %m\n", user); |
9e13dbae KS |
102 | return 0; |
103 | } | |
104 | ||
105 | gid_t util_lookup_group(struct udev *udev, const char *group) | |
106 | { | |
107 | char *endptr; | |
108 | struct group grbuf; | |
109 | struct group *gr; | |
110 | gid_t gid = 0; | |
111 | size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); | |
112 | char *buf = NULL; | |
113 | ||
090be865 | 114 | if (streq(group, "root")) |
9e13dbae KS |
115 | return 0; |
116 | gid = strtoul(group, &endptr, 10); | |
117 | if (endptr[0] == '\0') | |
118 | return gid; | |
119 | gid = 0; | |
120 | for (;;) { | |
121 | char *newbuf; | |
122 | ||
123 | newbuf = realloc(buf, buflen); | |
124 | if (!newbuf) | |
125 | break; | |
126 | buf = newbuf; | |
127 | errno = getgrnam_r(group, &grbuf, buf, buflen, &gr); | |
128 | if (gr != NULL) { | |
129 | gid = gr->gr_gid; | |
130 | } else if (errno == ERANGE) { | |
131 | buflen *= 2; | |
132 | continue; | |
133 | } else if (errno == 0 || errno == ENOENT || errno == ESRCH) { | |
c8f8394a | 134 | udev_err(udev, "specified group '%s' unknown\n", group); |
9e13dbae | 135 | } else { |
c8f8394a | 136 | udev_err(udev, "error resolving group '%s': %m\n", group); |
9e13dbae KS |
137 | } |
138 | break; | |
139 | } | |
140 | free(buf); | |
141 | return gid; | |
142 | } | |
143 | ||
144 | /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */ | |
145 | int util_resolve_subsys_kernel(struct udev *udev, const char *string, | |
146 | char *result, size_t maxsize, int read_value) | |
147 | { | |
148 | char temp[UTIL_PATH_SIZE]; | |
149 | char *subsys; | |
150 | char *sysname; | |
151 | struct udev_device *dev; | |
152 | char *attr; | |
153 | ||
154 | if (string[0] != '[') | |
155 | return -1; | |
156 | ||
d5a89d7d | 157 | strscpy(temp, sizeof(temp), string); |
9e13dbae KS |
158 | |
159 | subsys = &temp[1]; | |
160 | ||
161 | sysname = strchr(subsys, '/'); | |
162 | if (sysname == NULL) | |
163 | return -1; | |
164 | sysname[0] = '\0'; | |
165 | sysname = &sysname[1]; | |
166 | ||
167 | attr = strchr(sysname, ']'); | |
168 | if (attr == NULL) | |
169 | return -1; | |
170 | attr[0] = '\0'; | |
171 | attr = &attr[1]; | |
172 | if (attr[0] == '/') | |
173 | attr = &attr[1]; | |
174 | if (attr[0] == '\0') | |
175 | attr = NULL; | |
176 | ||
177 | if (read_value && attr == NULL) | |
178 | return -1; | |
179 | ||
180 | dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname); | |
181 | if (dev == NULL) | |
182 | return -1; | |
183 | ||
184 | if (read_value) { | |
185 | const char *val; | |
186 | ||
187 | val = udev_device_get_sysattr_value(dev, attr); | |
188 | if (val != NULL) | |
d5a89d7d | 189 | strscpy(result, maxsize, val); |
9e13dbae KS |
190 | else |
191 | result[0] = '\0'; | |
c8f8394a | 192 | udev_dbg(udev, "value '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result); |
9e13dbae KS |
193 | } else { |
194 | size_t l; | |
195 | char *s; | |
196 | ||
197 | s = result; | |
d5a89d7d | 198 | l = strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL); |
9e13dbae | 199 | if (attr != NULL) |
d5a89d7d | 200 | strpcpyl(&s, l, "/", attr, NULL); |
c8f8394a | 201 | udev_dbg(udev, "path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result); |
9e13dbae KS |
202 | } |
203 | udev_device_unref(dev); | |
204 | return 0; | |
205 | } | |
955d98c9 | 206 | |
cdfdc85f | 207 | ssize_t util_get_sys_core_link_value(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size) |
eb1f0e66 | 208 | { |
912541b0 KS |
209 | char path[UTIL_PATH_SIZE]; |
210 | char target[UTIL_PATH_SIZE]; | |
211 | ssize_t len; | |
212 | const char *pos; | |
213 | ||
d5a89d7d | 214 | strscpyl(path, sizeof(path), syspath, "/", slink, NULL); |
912541b0 KS |
215 | len = readlink(path, target, sizeof(target)); |
216 | if (len <= 0 || len == (ssize_t)sizeof(target)) | |
217 | return -1; | |
218 | target[len] = '\0'; | |
219 | pos = strrchr(target, '/'); | |
220 | if (pos == NULL) | |
221 | return -1; | |
222 | pos = &pos[1]; | |
d5a89d7d | 223 | return strscpy(value, size, pos); |
eb1f0e66 | 224 | } |
95d90c4f | 225 | |
8753fadf | 226 | int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size) |
b21b95d7 | 227 | { |
912541b0 KS |
228 | char link_target[UTIL_PATH_SIZE]; |
229 | ||
230 | ssize_t len; | |
231 | int i; | |
232 | int back; | |
bd59d823 | 233 | char *base = NULL; |
912541b0 KS |
234 | |
235 | len = readlink(syspath, link_target, sizeof(link_target)); | |
236 | if (len <= 0 || len == (ssize_t)sizeof(link_target)) | |
237 | return -1; | |
238 | link_target[len] = '\0'; | |
912541b0 | 239 | |
33502ffe | 240 | for (back = 0; startswith(&link_target[back * 3], "../"); back++) |
912541b0 | 241 | ; |
912541b0 KS |
242 | for (i = 0; i <= back; i++) { |
243 | base = strrchr(syspath, '/'); | |
244 | if (base == NULL) | |
bd59d823 | 245 | return -EINVAL; |
912541b0 KS |
246 | base[0] = '\0'; |
247 | } | |
4dd1de72 | 248 | |
d5a89d7d | 249 | strscpyl(base, size - (base - syspath), "/", &link_target[back * 3], NULL); |
912541b0 | 250 | return 0; |
b21b95d7 | 251 | } |
7a01f11a | 252 | |
7a01f11a KS |
253 | int util_log_priority(const char *priority) |
254 | { | |
912541b0 KS |
255 | char *endptr; |
256 | int prio; | |
257 | ||
258 | prio = strtol(priority, &endptr, 10); | |
259 | if (endptr[0] == '\0' || isspace(endptr[0])) | |
260 | return prio; | |
33502ffe | 261 | if (startswith(priority, "err")) |
912541b0 | 262 | return LOG_ERR; |
33502ffe | 263 | if (startswith(priority, "info")) |
912541b0 | 264 | return LOG_INFO; |
33502ffe | 265 | if (startswith(priority, "debug")) |
912541b0 KS |
266 | return LOG_DEBUG; |
267 | return 0; | |
7a01f11a KS |
268 | } |
269 | ||
065db052 | 270 | size_t util_path_encode(const char *src, char *dest, size_t size) |
7a01f11a | 271 | { |
912541b0 KS |
272 | size_t i, j; |
273 | ||
274 | for (i = 0, j = 0; src[i] != '\0'; i++) { | |
275 | if (src[i] == '/') { | |
276 | if (j+4 >= size) { | |
277 | j = 0; | |
278 | break; | |
279 | } | |
280 | memcpy(&dest[j], "\\x2f", 4); | |
281 | j += 4; | |
282 | } else if (src[i] == '\\') { | |
283 | if (j+4 >= size) { | |
284 | j = 0; | |
285 | break; | |
286 | } | |
287 | memcpy(&dest[j], "\\x5c", 4); | |
288 | j += 4; | |
289 | } else { | |
290 | if (j+1 >= size) { | |
291 | j = 0; | |
292 | break; | |
293 | } | |
294 | dest[j] = src[i]; | |
295 | j++; | |
296 | } | |
297 | } | |
298 | dest[j] = '\0'; | |
299 | return j; | |
7a01f11a KS |
300 | } |
301 | ||
7a01f11a KS |
302 | void util_remove_trailing_chars(char *path, char c) |
303 | { | |
912541b0 | 304 | size_t len; |
7a01f11a | 305 | |
912541b0 KS |
306 | if (path == NULL) |
307 | return; | |
308 | len = strlen(path); | |
309 | while (len > 0 && path[len-1] == c) | |
310 | path[--len] = '\0'; | |
7a01f11a | 311 | } |
3eb46ec6 | 312 | |
0bbe8838 | 313 | int util_replace_whitespace(const char *str, char *to, size_t len) |
92f43136 | 314 | { |
912541b0 KS |
315 | size_t i, j; |
316 | ||
317 | /* strip trailing whitespace */ | |
318 | len = strnlen(str, len); | |
319 | while (len && isspace(str[len-1])) | |
320 | len--; | |
321 | ||
322 | /* strip leading whitespace */ | |
323 | i = 0; | |
324 | while (isspace(str[i]) && (i < len)) | |
325 | i++; | |
326 | ||
327 | j = 0; | |
328 | while (i < len) { | |
329 | /* substitute multiple whitespace with a single '_' */ | |
330 | if (isspace(str[i])) { | |
331 | while (isspace(str[i])) | |
332 | i++; | |
333 | to[j++] = '_'; | |
334 | } | |
335 | to[j++] = str[i++]; | |
336 | } | |
337 | to[j] = '\0'; | |
338 | return 0; | |
92f43136 KS |
339 | } |
340 | ||
75250977 | 341 | /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */ |
0bbe8838 | 342 | int util_replace_chars(char *str, const char *white) |
75250977 | 343 | { |
912541b0 KS |
344 | size_t i = 0; |
345 | int replaced = 0; | |
346 | ||
347 | while (str[i] != '\0') { | |
348 | int len; | |
349 | ||
8f6ce71f | 350 | if (whitelisted_char_for_devnode(str[i], white)) { |
912541b0 KS |
351 | i++; |
352 | continue; | |
353 | } | |
354 | ||
355 | /* accept hex encoding */ | |
356 | if (str[i] == '\\' && str[i+1] == 'x') { | |
357 | i += 2; | |
358 | continue; | |
359 | } | |
360 | ||
361 | /* accept valid utf8 */ | |
362 | len = utf8_encoded_valid_unichar(&str[i]); | |
363 | if (len > 1) { | |
364 | i += len; | |
365 | continue; | |
366 | } | |
367 | ||
368 | /* if space is allowed, replace whitespace with ordinary space */ | |
369 | if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) { | |
370 | str[i] = ' '; | |
371 | i++; | |
372 | replaced++; | |
373 | continue; | |
374 | } | |
375 | ||
376 | /* everything else is replaced with '_' */ | |
377 | str[i] = '_'; | |
378 | i++; | |
379 | replaced++; | |
380 | } | |
381 | return replaced; | |
75250977 | 382 | } |
92f43136 KS |
383 | |
384 | /** | |
0bbe8838 | 385 | * udev_util_encode_string: |
92f43136 KS |
386 | * @str: input string to be encoded |
387 | * @str_enc: output string to store the encoded input string | |
388 | * @len: maximum size of the output string, which may be | |
389 | * four times as long as the input string | |
390 | * | |
391 | * Encode all potentially unsafe characters of a string to the | |
0bbe8838 | 392 | * corresponding 2 char hex value prefixed by '\x'. |
92f43136 KS |
393 | * |
394 | * Returns: 0 if the entire string was copied, non-zero otherwise. | |
395 | **/ | |
54cf0b7f | 396 | _public_ int udev_util_encode_string(const char *str, char *str_enc, size_t len) |
92f43136 | 397 | { |
8f6ce71f | 398 | return encode_devnode_name(str, str_enc, len); |
92f43136 | 399 | } |
4b09a2fc | 400 | |
c7dff03e | 401 | unsigned int util_string_hash32(const char *str) |
e14bdd88 | 402 | { |
57d0e6b2 | 403 | return MurmurHash2(str, strlen(str), 0); |
28460195 | 404 | } |
c7dff03e | 405 | |
28460195 KS |
406 | /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */ |
407 | uint64_t util_string_bloom64(const char *str) | |
408 | { | |
912541b0 KS |
409 | uint64_t bits = 0; |
410 | unsigned int hash = util_string_hash32(str); | |
411 | ||
412 | bits |= 1LLU << (hash & 63); | |
413 | bits |= 1LLU << ((hash >> 6) & 63); | |
414 | bits |= 1LLU << ((hash >> 12) & 63); | |
415 | bits |= 1LLU << ((hash >> 18) & 63); | |
416 | return bits; | |
e14bdd88 | 417 | } |
9c6a11b1 | 418 | |
c6eefe36 KS |
419 | ssize_t print_kmsg(const char *fmt, ...) |
420 | { | |
955d98c9 | 421 | _cleanup_close_ int fd = -1; |
c6eefe36 KS |
422 | va_list ap; |
423 | char text[1024]; | |
424 | ssize_t len; | |
425 | ssize_t ret; | |
426 | ||
427 | fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC); | |
428 | if (fd < 0) | |
429 | return -errno; | |
430 | ||
431 | len = snprintf(text, sizeof(text), "<30>systemd-udevd[%u]: ", getpid()); | |
432 | ||
433 | va_start(ap, fmt); | |
434 | len += vsnprintf(text + len, sizeof(text) - len, fmt, ap); | |
435 | va_end(ap); | |
436 | ||
437 | ret = write(fd, text, len); | |
438 | if (ret < 0) | |
955d98c9 LP |
439 | return -errno; |
440 | ||
c6eefe36 KS |
441 | return ret; |
442 | } |