]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libudev/libudev-util.c
udev: switch to systemd logging functions
[thirdparty/systemd.git] / src / libudev / libudev-util.c
CommitLineData
88a6477e
KS
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***/
eb1f0e66 19
eb1f0e66
KS
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>
75250977 27#include <ctype.h>
93b0f384 28#include <fcntl.h>
9c6a11b1 29#include <time.h>
9e13dbae
KS
30#include <pwd.h>
31#include <grp.h>
eb1f0e66 32#include <sys/stat.h>
9e13dbae 33#include <sys/param.h>
eb1f0e66 34
8f6ce71f 35#include "device-nodes.h"
eb1f0e66
KS
36#include "libudev.h"
37#include "libudev-private.h"
02a36bc9 38#include "utf8.h"
57d0e6b2 39#include "MurmurHash2.h"
eb1f0e66 40
0bbe8838
KS
41/**
42 * SECTION:libudev-util
43 * @short_description: utils
21dbe43a
KS
44 *
45 * Utilities useful when dealing with devices and device node names.
0bbe8838
KS
46 */
47
9e13dbae
KS
48/* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
49int 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
d5a89d7d 61 strscpy(temp, sizeof(temp), string);
9e13dbae
KS
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)
d5a89d7d 93 strscpy(result, maxsize, val);
9e13dbae
KS
94 else
95 result[0] = '\0';
25e773ee 96 log_debug("value '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
9e13dbae
KS
97 } else {
98 size_t l;
99 char *s;
100
101 s = result;
d5a89d7d 102 l = strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL);
9e13dbae 103 if (attr != NULL)
d5a89d7d 104 strpcpyl(&s, l, "/", attr, NULL);
25e773ee 105 log_debug("path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result);
9e13dbae
KS
106 }
107 udev_device_unref(dev);
108 return 0;
109}
955d98c9 110
cdfdc85f 111ssize_t util_get_sys_core_link_value(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size)
eb1f0e66 112{
912541b0
KS
113 char path[UTIL_PATH_SIZE];
114 char target[UTIL_PATH_SIZE];
115 ssize_t len;
116 const char *pos;
117
d5a89d7d 118 strscpyl(path, sizeof(path), syspath, "/", slink, NULL);
912541b0
KS
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];
d5a89d7d 127 return strscpy(value, size, pos);
eb1f0e66 128}
95d90c4f 129
8753fadf 130int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
b21b95d7 131{
912541b0
KS
132 char link_target[UTIL_PATH_SIZE];
133
134 ssize_t len;
135 int i;
136 int back;
bd59d823 137 char *base = NULL;
912541b0
KS
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';
912541b0 143
33502ffe 144 for (back = 0; startswith(&link_target[back * 3], "../"); back++)
912541b0 145 ;
912541b0
KS
146 for (i = 0; i <= back; i++) {
147 base = strrchr(syspath, '/');
148 if (base == NULL)
bd59d823 149 return -EINVAL;
912541b0
KS
150 base[0] = '\0';
151 }
4dd1de72 152
d5a89d7d 153 strscpyl(base, size - (base - syspath), "/", &link_target[back * 3], NULL);
912541b0 154 return 0;
b21b95d7 155}
7a01f11a 156
7a01f11a
KS
157int util_log_priority(const char *priority)
158{
912541b0
KS
159 char *endptr;
160 int prio;
161
ee7122c0
ZJS
162 prio = strtoul(priority, &endptr, 10);
163 if (endptr[0] == '\0' || isspace(endptr[0])) {
164 if (prio >= 0 && prio <= 7)
165 return prio;
166 else
167 return -ERANGE;
168 }
ba7408a6
TG
169
170 return log_level_from_string(priority);
7a01f11a
KS
171}
172
065db052 173size_t util_path_encode(const char *src, char *dest, size_t size)
7a01f11a 174{
912541b0
KS
175 size_t i, j;
176
177 for (i = 0, j = 0; src[i] != '\0'; i++) {
178 if (src[i] == '/') {
179 if (j+4 >= size) {
180 j = 0;
181 break;
182 }
183 memcpy(&dest[j], "\\x2f", 4);
184 j += 4;
185 } else if (src[i] == '\\') {
186 if (j+4 >= size) {
187 j = 0;
188 break;
189 }
190 memcpy(&dest[j], "\\x5c", 4);
191 j += 4;
192 } else {
193 if (j+1 >= size) {
194 j = 0;
195 break;
196 }
197 dest[j] = src[i];
198 j++;
199 }
200 }
201 dest[j] = '\0';
202 return j;
7a01f11a
KS
203}
204
7a01f11a
KS
205void util_remove_trailing_chars(char *path, char c)
206{
912541b0 207 size_t len;
7a01f11a 208
912541b0
KS
209 if (path == NULL)
210 return;
211 len = strlen(path);
212 while (len > 0 && path[len-1] == c)
213 path[--len] = '\0';
7a01f11a 214}
3eb46ec6 215
0bbe8838 216int util_replace_whitespace(const char *str, char *to, size_t len)
92f43136 217{
912541b0
KS
218 size_t i, j;
219
220 /* strip trailing whitespace */
221 len = strnlen(str, len);
222 while (len && isspace(str[len-1]))
223 len--;
224
225 /* strip leading whitespace */
226 i = 0;
227 while (isspace(str[i]) && (i < len))
228 i++;
229
230 j = 0;
231 while (i < len) {
232 /* substitute multiple whitespace with a single '_' */
233 if (isspace(str[i])) {
234 while (isspace(str[i]))
235 i++;
236 to[j++] = '_';
237 }
238 to[j++] = str[i++];
239 }
240 to[j] = '\0';
241 return 0;
92f43136
KS
242}
243
75250977 244/* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
0bbe8838 245int util_replace_chars(char *str, const char *white)
75250977 246{
912541b0
KS
247 size_t i = 0;
248 int replaced = 0;
249
250 while (str[i] != '\0') {
251 int len;
252
8f6ce71f 253 if (whitelisted_char_for_devnode(str[i], white)) {
912541b0
KS
254 i++;
255 continue;
256 }
257
258 /* accept hex encoding */
259 if (str[i] == '\\' && str[i+1] == 'x') {
260 i += 2;
261 continue;
262 }
263
264 /* accept valid utf8 */
265 len = utf8_encoded_valid_unichar(&str[i]);
266 if (len > 1) {
267 i += len;
268 continue;
269 }
270
271 /* if space is allowed, replace whitespace with ordinary space */
272 if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) {
273 str[i] = ' ';
274 i++;
275 replaced++;
276 continue;
277 }
278
279 /* everything else is replaced with '_' */
280 str[i] = '_';
281 i++;
282 replaced++;
283 }
284 return replaced;
75250977 285}
92f43136
KS
286
287/**
0bbe8838 288 * udev_util_encode_string:
92f43136
KS
289 * @str: input string to be encoded
290 * @str_enc: output string to store the encoded input string
291 * @len: maximum size of the output string, which may be
292 * four times as long as the input string
293 *
294 * Encode all potentially unsafe characters of a string to the
0bbe8838 295 * corresponding 2 char hex value prefixed by '\x'.
92f43136
KS
296 *
297 * Returns: 0 if the entire string was copied, non-zero otherwise.
298 **/
54cf0b7f 299_public_ int udev_util_encode_string(const char *str, char *str_enc, size_t len)
92f43136 300{
8f6ce71f 301 return encode_devnode_name(str, str_enc, len);
92f43136 302}
4b09a2fc 303
c7dff03e 304unsigned int util_string_hash32(const char *str)
e14bdd88 305{
57d0e6b2 306 return MurmurHash2(str, strlen(str), 0);
28460195 307}
c7dff03e 308
28460195
KS
309/* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
310uint64_t util_string_bloom64(const char *str)
311{
912541b0
KS
312 uint64_t bits = 0;
313 unsigned int hash = util_string_hash32(str);
314
315 bits |= 1LLU << (hash & 63);
316 bits |= 1LLU << ((hash >> 6) & 63);
317 bits |= 1LLU << ((hash >> 12) & 63);
318 bits |= 1LLU << ((hash >> 18) & 63);
319 return bits;
e14bdd88 320}