chiark / gitweb /
libudev: udev_device - add attribute cache
[elogind.git] / udev / lib / libudev-util.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <dirent.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32
33 #include "libudev.h"
34 #include "libudev-private.h"
35
36 static ssize_t get_sys_link(struct udev *udev, const char *slink, const char *devpath, char *subsystem, size_t size)
37 {
38         char path[UTIL_PATH_SIZE];
39         ssize_t len;
40         const char *pos;
41
42         util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
43         util_strlcat(path, devpath, sizeof(path));
44         util_strlcat(path, "/", sizeof(path));
45         util_strlcat(path, slink, sizeof(path));
46         len = readlink(path, path, sizeof(path));
47         if (len < 0 || len >= (ssize_t) sizeof(path))
48                 return -1;
49         path[len] = '\0';
50         pos = strrchr(path, '/');
51         if (pos == NULL)
52                 return -1;
53         pos = &pos[1];
54         return util_strlcpy(subsystem, pos, size);
55 }
56
57 ssize_t util_get_sys_subsystem(struct udev *udev, const char *devpath, char *subsystem, size_t size)
58 {
59         return get_sys_link(udev, "subsystem", devpath, subsystem, size);
60 }
61
62 ssize_t util_get_sys_driver(struct udev *udev, const char *devpath, char *driver, size_t size)
63 {
64         return get_sys_link(udev, "driver", devpath, driver, size);
65 }
66
67 int util_resolve_sys_link(struct udev *udev, char *devpath, size_t size)
68 {
69         char link_path[UTIL_PATH_SIZE];
70         char link_target[UTIL_PATH_SIZE];
71
72         int len;
73         int i;
74         int back;
75
76         util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
77         util_strlcat(link_path, devpath, sizeof(link_path));
78         len = readlink(link_path, link_target, sizeof(link_target));
79         if (len <= 0)
80                 return -1;
81         link_target[len] = '\0';
82         dbg(udev, "path link '%s' points to '%s'\n", devpath, link_target);
83
84         for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
85                 ;
86         dbg(udev, "base '%s', tail '%s', back %i\n", devpath, &link_target[back * 3], back);
87         for (i = 0; i <= back; i++) {
88                 char *pos = strrchr(devpath, '/');
89
90                 if (pos == NULL)
91                         return -1;
92                 pos[0] = '\0';
93         }
94         dbg(udev, "after moving back '%s'\n", devpath);
95         util_strlcat(devpath, "/", size);
96         util_strlcat(devpath, &link_target[back * 3], size);
97         return 0;
98 }
99
100 struct util_name_entry *util_name_list_add(struct udev *udev, struct list_head *name_list,
101                                            const char *name, const char *value, int sort)
102 {
103         struct util_name_entry *name_loop;
104         struct util_name_entry *name_new;
105
106         /* avoid duplicate entries */
107         list_for_each_entry(name_loop, name_list, node) {
108                 if (strcmp(name_loop->name, name) == 0) {
109                         dbg(udev, "'%s' is already in the list\n", name);
110                         return name_loop;
111                 }
112         }
113
114         if (sort) {
115                 list_for_each_entry(name_loop, name_list, node) {
116                         if (strcmp(name_loop->name, name) > 0)
117                                 break;
118                 }
119         }
120
121         name_new = malloc(sizeof(struct util_name_entry));
122         if (name_new == NULL)
123                 return NULL;
124         memset(name_new, 0x00, sizeof(struct util_name_entry));
125         name_new->name = strdup(name);
126         if (name_new->name == NULL) {
127                 free(name_new);
128                 return NULL;
129         }
130         if (value != NULL) {
131                 name_new->value = strdup(value);
132                 if (name_new->value == NULL) {
133                         free(name_new);
134                         return NULL;
135                 }
136         }
137         dbg(udev, "adding '%s=%s'\n", name_new->name, name_new->value);
138         list_add_tail(&name_new->node, &name_loop->node);
139         return name_new;
140 }
141
142 void util_name_list_cleanup(struct udev *udev, struct list_head *name_list)
143 {
144         struct util_name_entry *name_loop;
145         struct util_name_entry *name_tmp;
146
147         list_for_each_entry_safe(name_loop, name_tmp, name_list, node) {
148                 list_del(&name_loop->node);
149                 free(name_loop->name);
150                 free(name_loop->value);
151                 free(name_loop);
152         }
153 }
154
155 int util_log_priority(const char *priority)
156 {
157         char *endptr;
158         int prio;
159
160         prio = strtol(priority, &endptr, 10);
161         if (endptr[0] == '\0')
162                 return prio;
163         if (strncasecmp(priority, "err", 3) == 0)
164                 return LOG_ERR;
165         if (strcasecmp(priority, "info") == 0)
166                 return LOG_INFO;
167         if (strcasecmp(priority, "debug") == 0)
168                 return LOG_DEBUG;
169         return 0;
170 }
171
172 size_t util_path_encode(char *s, size_t len)
173 {
174         char t[(len * 3)+1];
175         size_t i, j;
176
177         t[0] = '\0';
178         for (i = 0, j = 0; s[i] != '\0'; i++) {
179                 if (s[i] == '/') {
180                         memcpy(&t[j], "\\x2f", 4);
181                         j += 4;
182                 } else if (s[i] == '\\') {
183                         memcpy(&t[j], "\\x5c", 4);
184                         j += 4;
185                 } else {
186                         t[j] = s[i];
187                         j++;
188                 }
189         }
190         t[j] = '\0';
191         strncpy(s, t, len);
192         return j;
193 }
194
195 size_t util_path_decode(char *s)
196 {
197         size_t i, j;
198
199         for (i = 0, j = 0; s[i] != '\0'; j++) {
200                 if (memcmp(&s[i], "\\x2f", 4) == 0) {
201                         s[j] = '/';
202                         i += 4;
203                 }else if (memcmp(&s[i], "\\x5c", 4) == 0) {
204                         s[j] = '\\';
205                         i += 4;
206                 } else {
207                         s[j] = s[i];
208                         i++;
209                 }
210         }
211         s[j] = '\0';
212         return j;
213 }
214
215 void util_remove_trailing_chars(char *path, char c)
216 {
217         size_t len;
218
219         if (path == NULL)
220                 return;
221         len = strlen(path);
222         while (len > 0 && path[len-1] == c)
223                 path[--len] = '\0';
224 }
225
226 size_t util_strlcpy(char *dst, const char *src, size_t size)
227 {
228         size_t bytes = 0;
229         char *q = dst;
230         const char *p = src;
231         char ch;
232
233         while ((ch = *p++)) {
234                 if (bytes+1 < size)
235                         *q++ = ch;
236                 bytes++;
237         }
238
239         /* If size == 0 there is no space for a final null... */
240         if (size)
241                 *q = '\0';
242         return bytes;
243 }
244
245 size_t util_strlcat(char *dst, const char *src, size_t size)
246 {
247         size_t bytes = 0;
248         char *q = dst;
249         const char *p = src;
250         char ch;
251
252         while (bytes < size && *q) {
253                 q++;
254                 bytes++;
255         }
256         if (bytes == size)
257                 return (bytes + strlen(src));
258
259         while ((ch = *p++)) {
260                 if (bytes+1 < size)
261                 *q++ = ch;
262                 bytes++;
263         }
264
265         *q = '\0';
266         return bytes;
267 }
268
269 /* count of characters used to encode one unicode char */
270 static int utf8_encoded_expected_len(const char *str)
271 {
272         unsigned char c = (unsigned char)str[0];
273
274         if (c < 0x80)
275                 return 1;
276         if ((c & 0xe0) == 0xc0)
277                 return 2;
278         if ((c & 0xf0) == 0xe0)
279                 return 3;
280         if ((c & 0xf8) == 0xf0)
281                 return 4;
282         if ((c & 0xfc) == 0xf8)
283                 return 5;
284         if ((c & 0xfe) == 0xfc)
285                 return 6;
286         return 0;
287 }
288
289 /* decode one unicode char */
290 static int utf8_encoded_to_unichar(const char *str)
291 {
292         int unichar;
293         int len;
294         int i;
295
296         len = utf8_encoded_expected_len(str);
297         switch (len) {
298         case 1:
299                 return (int)str[0];
300         case 2:
301                 unichar = str[0] & 0x1f;
302                 break;
303         case 3:
304                 unichar = (int)str[0] & 0x0f;
305                 break;
306         case 4:
307                 unichar = (int)str[0] & 0x07;
308                 break;
309         case 5:
310                 unichar = (int)str[0] & 0x03;
311                 break;
312         case 6:
313                 unichar = (int)str[0] & 0x01;
314                 break;
315         default:
316                 return -1;
317         }
318
319         for (i = 1; i < len; i++) {
320                 if (((int)str[i] & 0xc0) != 0x80)
321                         return -1;
322                 unichar <<= 6;
323                 unichar |= (int)str[i] & 0x3f;
324         }
325
326         return unichar;
327 }
328
329 /* expected size used to encode one unicode char */
330 static int utf8_unichar_to_encoded_len(int unichar)
331 {
332         if (unichar < 0x80)
333                 return 1;
334         if (unichar < 0x800)
335                 return 2;
336         if (unichar < 0x10000)
337                 return 3;
338         if (unichar < 0x200000)
339                 return 4;
340         if (unichar < 0x4000000)
341                 return 5;
342         return 6;
343 }
344
345 /* check if unicode char has a valid numeric range */
346 static int utf8_unichar_valid_range(int unichar)
347 {
348         if (unichar > 0x10ffff)
349                 return 0;
350         if ((unichar & 0xfffff800) == 0xd800)
351                 return 0;
352         if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
353                 return 0;
354         if ((unichar & 0xffff) == 0xffff)
355                 return 0;
356         return 1;
357 }
358
359 /* validate one encoded unicode char and return its length */
360 static int utf8_encoded_valid_unichar(const char *str)
361 {
362         int len;
363         int unichar;
364         int i;
365
366         len = utf8_encoded_expected_len(str);
367         if (len == 0)
368                 return -1;
369
370         /* ascii is valid */
371         if (len == 1)
372                 return 1;
373
374         /* check if expected encoded chars are available */
375         for (i = 0; i < len; i++)
376                 if ((str[i] & 0x80) != 0x80)
377                         return -1;
378
379         unichar = utf8_encoded_to_unichar(str);
380
381         /* check if encoded length matches encoded value */
382         if (utf8_unichar_to_encoded_len(unichar) != len)
383                 return -1;
384
385         /* check if value has valid range */
386         if (!utf8_unichar_valid_range(unichar))
387                 return -1;
388
389         return len;
390 }
391
392 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
393 int util_replace_chars(char *str, const char *white)
394 {
395         size_t i = 0;
396         int replaced = 0;
397
398         while (str[i] != '\0') {
399                 int len;
400
401                 /* accept whitelist */
402                 if (white != NULL && strchr(white, str[i]) != NULL) {
403                         i++;
404                         continue;
405                 }
406
407                 /* accept plain ascii char */
408                 if ((str[i] >= '0' && str[i] <= '9') ||
409                     (str[i] >= 'A' && str[i] <= 'Z') ||
410                     (str[i] >= 'a' && str[i] <= 'z')) {
411                         i++;
412                         continue;
413                 }
414
415                 /* accept hex encoding */
416                 if (str[i] == '\\' && str[i+1] == 'x') {
417                         i += 2;
418                         continue;
419                 }
420
421                 /* accept valid utf8 */
422                 len = utf8_encoded_valid_unichar(&str[i]);
423                 if (len > 1) {
424                         i += len;
425                         continue;
426                 }
427
428                 /* if space is allowed, replace whitespace with ordinary space */
429                 if (isspace(str[i]) && strchr(white, ' ') != NULL) {
430                         str[i] = ' ';
431                         i++;
432                         replaced++;
433                         continue;
434                 }
435
436                 /* everything else is replaced with '_' */
437                 str[i] = '_';
438                 i++;
439                 replaced++;
440         }
441
442         return replaced;
443 }