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