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