]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-util.c
2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
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.
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.
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/>.
27 #include "device-nodes.h"
29 #include "libudev-private.h"
31 #include "MurmurHash2.h"
34 * SECTION:libudev-util
35 * @short_description: utils
37 * Utilities useful when dealing with devices and device node names.
40 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
41 int util_resolve_subsys_kernel(struct udev
*udev
, const char *string
,
42 char *result
, size_t maxsize
, int read_value
)
44 char temp
[UTIL_PATH_SIZE
];
47 struct udev_device
*dev
;
53 strscpy(temp
, sizeof(temp
), string
);
57 sysname
= strchr(subsys
, '/');
61 sysname
= &sysname
[1];
63 attr
= strchr(sysname
, ']');
73 if (read_value
&& attr
== NULL
)
76 dev
= udev_device_new_from_subsystem_sysname(udev
, subsys
, sysname
);
83 val
= udev_device_get_sysattr_value(dev
, attr
);
85 strscpy(result
, maxsize
, val
);
88 log_debug("value '[%s/%s]%s' is '%s'", subsys
, sysname
, attr
, result
);
94 l
= strpcpyl(&s
, maxsize
, udev_device_get_syspath(dev
), NULL
);
96 strpcpyl(&s
, l
, "/", attr
, NULL
);
97 log_debug("path '[%s/%s]%s' is '%s'", subsys
, sysname
, attr
, result
);
99 udev_device_unref(dev
);
103 ssize_t
util_get_sys_core_link_value(struct udev
*udev
, const char *slink
, const char *syspath
, char *value
, size_t size
)
105 char path
[UTIL_PATH_SIZE
];
106 char target
[UTIL_PATH_SIZE
];
110 strscpyl(path
, sizeof(path
), syspath
, "/", slink
, NULL
);
111 len
= readlink(path
, target
, sizeof(target
));
112 if (len
<= 0 || len
== (ssize_t
)sizeof(target
))
115 pos
= strrchr(target
, '/');
119 return strscpy(value
, size
, pos
);
122 int util_resolve_sys_link(struct udev
*udev
, char *syspath
, size_t size
)
124 char link_target
[UTIL_PATH_SIZE
];
131 len
= readlink(syspath
, link_target
, sizeof(link_target
));
132 if (len
<= 0 || len
== (ssize_t
)sizeof(link_target
))
134 link_target
[len
] = '\0';
136 for (back
= 0; startswith(&link_target
[back
* 3], "../"); back
++)
138 for (i
= 0; i
<= back
; i
++) {
139 base
= strrchr(syspath
, '/');
145 strscpyl(base
, size
- (base
- syspath
), "/", &link_target
[back
* 3], NULL
);
149 int util_log_priority(const char *priority
)
154 prio
= strtoul(priority
, &endptr
, 10);
155 if (endptr
[0] == '\0' || isspace(endptr
[0])) {
156 if (prio
>= 0 && prio
<= 7)
162 return log_level_from_string(priority
);
165 size_t util_path_encode(const char *src
, char *dest
, size_t size
)
169 for (i
= 0, j
= 0; src
[i
] != '\0'; i
++) {
175 memcpy(&dest
[j
], "\\x2f", 4);
177 } else if (src
[i
] == '\\') {
182 memcpy(&dest
[j
], "\\x5c", 4);
197 void util_remove_trailing_chars(char *path
, char c
)
204 while (len
> 0 && path
[len
-1] == c
)
208 int util_replace_whitespace(const char *str
, char *to
, size_t len
)
212 /* strip trailing whitespace */
213 len
= strnlen(str
, len
);
214 while (len
&& isspace(str
[len
-1]))
217 /* strip leading whitespace */
219 while ((i
< len
) && isspace(str
[i
]))
224 /* substitute multiple whitespace with a single '_' */
225 if (isspace(str
[i
])) {
226 while (isspace(str
[i
]))
236 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
237 int util_replace_chars(char *str
, const char *white
)
242 while (str
[i
] != '\0') {
245 if (whitelisted_char_for_devnode(str
[i
], white
)) {
250 /* accept hex encoding */
251 if (str
[i
] == '\\' && str
[i
+1] == 'x') {
256 /* accept valid utf8 */
257 len
= utf8_encoded_valid_unichar(&str
[i
]);
263 /* if space is allowed, replace whitespace with ordinary space */
264 if (isspace(str
[i
]) && white
!= NULL
&& strchr(white
, ' ') != NULL
) {
271 /* everything else is replaced with '_' */
280 * udev_util_encode_string:
281 * @str: input string to be encoded
282 * @str_enc: output string to store the encoded input string
283 * @len: maximum size of the output string, which may be
284 * four times as long as the input string
286 * Encode all potentially unsafe characters of a string to the
287 * corresponding 2 char hex value prefixed by '\x'.
289 * Returns: 0 if the entire string was copied, non-zero otherwise.
291 _public_
int udev_util_encode_string(const char *str
, char *str_enc
, size_t len
)
293 return encode_devnode_name(str
, str_enc
, len
);
296 unsigned int util_string_hash32(const char *str
)
298 return MurmurHash2(str
, strlen(str
), 0);
301 /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
302 uint64_t util_string_bloom64(const char *str
)
305 unsigned int hash
= util_string_hash32(str
);
307 bits
|= 1LLU << (hash
& 63);
308 bits
|= 1LLU << ((hash
>> 6) & 63);
309 bits
|= 1LLU << ((hash
>> 12) & 63);
310 bits
|= 1LLU << ((hash
>> 18) & 63);