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