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