chiark / gitweb /
udev: util - use log_level_from_string()
[elogind.git] / src / libudev / libudev-util.c
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 ***/
19
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>
27 #include <ctype.h>
28 #include <fcntl.h>
29 #include <time.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <sys/stat.h>
33 #include <sys/param.h>
34
35 #include "device-nodes.h"
36 #include "libudev.h"
37 #include "libudev-private.h"
38 #include "utf8.h"
39 #include "MurmurHash2.h"
40
41 /**
42  * SECTION:libudev-util
43  * @short_description: utils
44  *
45  * Utilities useful when dealing with devices and device node names.
46  */
47
48 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
49 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
50                                char *result, size_t maxsize, int read_value)
51 {
52         char temp[UTIL_PATH_SIZE];
53         char *subsys;
54         char *sysname;
55         struct udev_device *dev;
56         char *attr;
57
58         if (string[0] != '[')
59                 return -1;
60
61         strscpy(temp, sizeof(temp), string);
62
63         subsys = &temp[1];
64
65         sysname = strchr(subsys, '/');
66         if (sysname == NULL)
67                 return -1;
68         sysname[0] = '\0';
69         sysname = &sysname[1];
70
71         attr = strchr(sysname, ']');
72         if (attr == NULL)
73                 return -1;
74         attr[0] = '\0';
75         attr = &attr[1];
76         if (attr[0] == '/')
77                 attr = &attr[1];
78         if (attr[0] == '\0')
79                 attr = NULL;
80
81         if (read_value && attr == NULL)
82                 return -1;
83
84         dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
85         if (dev == NULL)
86                 return -1;
87
88         if (read_value) {
89                 const char *val;
90
91                 val = udev_device_get_sysattr_value(dev, attr);
92                 if (val != NULL)
93                         strscpy(result, maxsize, val);
94                 else
95                         result[0] = '\0';
96                 udev_dbg(udev, "value '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
97         } else {
98                 size_t l;
99                 char *s;
100
101                 s = result;
102                 l = strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL);
103                 if (attr != NULL)
104                         strpcpyl(&s, l, "/", attr, NULL);
105                 udev_dbg(udev, "path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
106         }
107         udev_device_unref(dev);
108         return 0;
109 }
110
111 ssize_t util_get_sys_core_link_value(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size)
112 {
113         char path[UTIL_PATH_SIZE];
114         char target[UTIL_PATH_SIZE];
115         ssize_t len;
116         const char *pos;
117
118         strscpyl(path, sizeof(path), syspath, "/", slink, NULL);
119         len = readlink(path, target, sizeof(target));
120         if (len <= 0 || len == (ssize_t)sizeof(target))
121                 return -1;
122         target[len] = '\0';
123         pos = strrchr(target, '/');
124         if (pos == NULL)
125                 return -1;
126         pos = &pos[1];
127         return strscpy(value, size, pos);
128 }
129
130 int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
131 {
132         char link_target[UTIL_PATH_SIZE];
133
134         ssize_t len;
135         int i;
136         int back;
137         char *base = NULL;
138
139         len = readlink(syspath, link_target, sizeof(link_target));
140         if (len <= 0 || len == (ssize_t)sizeof(link_target))
141                 return -1;
142         link_target[len] = '\0';
143
144         for (back = 0; startswith(&link_target[back * 3], "../"); back++)
145                 ;
146         for (i = 0; i <= back; i++) {
147                 base = strrchr(syspath, '/');
148                 if (base == NULL)
149                         return -EINVAL;
150                 base[0] = '\0';
151         }
152
153         strscpyl(base, size - (base - syspath), "/", &link_target[back * 3], NULL);
154         return 0;
155 }
156
157 int util_log_priority(const char *priority)
158 {
159         char *endptr;
160         int prio;
161
162         prio = strtol(priority, &endptr, 10);
163         if (endptr[0] == '\0' || isspace(endptr[0]))
164                 return prio;
165
166         return log_level_from_string(priority);
167 }
168
169 size_t util_path_encode(const char *src, char *dest, size_t size)
170 {
171         size_t i, j;
172
173         for (i = 0, j = 0; src[i] != '\0'; i++) {
174                 if (src[i] == '/') {
175                         if (j+4 >= size) {
176                                 j = 0;
177                                 break;
178                         }
179                         memcpy(&dest[j], "\\x2f", 4);
180                         j += 4;
181                 } else if (src[i] == '\\') {
182                         if (j+4 >= size) {
183                                 j = 0;
184                                 break;
185                         }
186                         memcpy(&dest[j], "\\x5c", 4);
187                         j += 4;
188                 } else {
189                         if (j+1 >= size) {
190                                 j = 0;
191                                 break;
192                         }
193                         dest[j] = src[i];
194                         j++;
195                 }
196         }
197         dest[j] = '\0';
198         return j;
199 }
200
201 void util_remove_trailing_chars(char *path, char c)
202 {
203         size_t len;
204
205         if (path == NULL)
206                 return;
207         len = strlen(path);
208         while (len > 0 && path[len-1] == c)
209                 path[--len] = '\0';
210 }
211
212 int util_replace_whitespace(const char *str, char *to, size_t len)
213 {
214         size_t i, j;
215
216         /* strip trailing whitespace */
217         len = strnlen(str, len);
218         while (len && isspace(str[len-1]))
219                 len--;
220
221         /* strip leading whitespace */
222         i = 0;
223         while (isspace(str[i]) && (i < len))
224                 i++;
225
226         j = 0;
227         while (i < len) {
228                 /* substitute multiple whitespace with a single '_' */
229                 if (isspace(str[i])) {
230                         while (isspace(str[i]))
231                                 i++;
232                         to[j++] = '_';
233                 }
234                 to[j++] = str[i++];
235         }
236         to[j] = '\0';
237         return 0;
238 }
239
240 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
241 int util_replace_chars(char *str, const char *white)
242 {
243         size_t i = 0;
244         int replaced = 0;
245
246         while (str[i] != '\0') {
247                 int len;
248
249                 if (whitelisted_char_for_devnode(str[i], white)) {
250                         i++;
251                         continue;
252                 }
253
254                 /* accept hex encoding */
255                 if (str[i] == '\\' && str[i+1] == 'x') {
256                         i += 2;
257                         continue;
258                 }
259
260                 /* accept valid utf8 */
261                 len = utf8_encoded_valid_unichar(&str[i]);
262                 if (len > 1) {
263                         i += len;
264                         continue;
265                 }
266
267                 /* if space is allowed, replace whitespace with ordinary space */
268                 if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) {
269                         str[i] = ' ';
270                         i++;
271                         replaced++;
272                         continue;
273                 }
274
275                 /* everything else is replaced with '_' */
276                 str[i] = '_';
277                 i++;
278                 replaced++;
279         }
280         return replaced;
281 }
282
283 /**
284  * udev_util_encode_string:
285  * @str: input string to be encoded
286  * @str_enc: output string to store the encoded input string
287  * @len: maximum size of the output string, which may be
288  *       four times as long as the input string
289  *
290  * Encode all potentially unsafe characters of a string to the
291  * corresponding 2 char hex value prefixed by '\x'.
292  *
293  * Returns: 0 if the entire string was copied, non-zero otherwise.
294  **/
295 _public_ int udev_util_encode_string(const char *str, char *str_enc, size_t len)
296 {
297         return encode_devnode_name(str, str_enc, len);
298 }
299
300 unsigned int util_string_hash32(const char *str)
301 {
302         return MurmurHash2(str, strlen(str), 0);
303 }
304
305 /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
306 uint64_t util_string_bloom64(const char *str)
307 {
308         uint64_t bits = 0;
309         unsigned int hash = util_string_hash32(str);
310
311         bits |= 1LLU << (hash & 63);
312         bits |= 1LLU << ((hash >> 6) & 63);
313         bits |= 1LLU << ((hash >> 12) & 63);
314         bits |= 1LLU << ((hash >> 18) & 63);
315         return bits;
316 }