]>
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/>.
29 #include "MurmurHash2.h"
30 #include "device-nodes.h"
31 #include "libudev-private.h"
32 #include "syslog-util.h"
36 * SECTION:libudev-util
37 * @short_description: utils
39 * Utilities useful when dealing with devices and device node names.
42 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
43 int util_resolve_subsys_kernel(struct udev
*udev
, const char *string
,
44 char *result
, size_t maxsize
, int read_value
)
46 char temp
[UTIL_PATH_SIZE
];
49 struct udev_device
*dev
;
55 strscpy(temp
, sizeof(temp
), string
);
59 sysname
= strchr(subsys
, '/');
63 sysname
= &sysname
[1];
65 attr
= strchr(sysname
, ']');
75 if (read_value
&& attr
== NULL
)
78 dev
= udev_device_new_from_subsystem_sysname(udev
, subsys
, sysname
);
85 val
= udev_device_get_sysattr_value(dev
, attr
);
87 strscpy(result
, maxsize
, val
);
90 log_debug("value '[%s/%s]%s' is '%s'", subsys
, sysname
, attr
, result
);
96 l
= strpcpyl(&s
, maxsize
, udev_device_get_syspath(dev
), NULL
);
98 strpcpyl(&s
, l
, "/", attr
, NULL
);
99 log_debug("path '[%s/%s]%s' is '%s'", subsys
, sysname
, attr
, result
);
101 udev_device_unref(dev
);
105 int util_log_priority(const char *priority
)
110 prio
= strtoul(priority
, &endptr
, 10);
111 if (endptr
[0] == '\0' || isspace(endptr
[0])) {
112 if (prio
>= 0 && prio
<= 7)
118 return log_level_from_string(priority
);
121 size_t util_path_encode(const char *src
, char *dest
, size_t size
)
125 for (i
= 0, j
= 0; src
[i
] != '\0'; i
++) {
131 memcpy(&dest
[j
], "\\x2f", 4);
133 } else if (src
[i
] == '\\') {
138 memcpy(&dest
[j
], "\\x5c", 4);
154 * Copy from 'str' to 'to', while removing all leading and trailing whitespace,
155 * and replacing each run of consecutive whitespace with a single underscore.
156 * The chars from 'str' are copied up to the \0 at the end of the string, or
157 * at most 'len' chars. This appends \0 to 'to', at the end of the copied
160 * If 'len' chars are copied into 'to', the final \0 is placed at len+1
161 * (i.e. 'to[len] = \0'), so the 'to' buffer must have at least len+1
164 * Note this may be called with 'str' == 'to', i.e. to replace whitespace
165 * in-place in a buffer. This function can handle that situation.
167 int util_replace_whitespace(const char *str
, char *to
, size_t len
)
171 /* strip trailing whitespace */
172 len
= strnlen(str
, len
);
173 while (len
&& isspace(str
[len
-1]))
176 /* strip leading whitespace */
178 while ((i
< len
) && isspace(str
[i
]))
183 /* substitute multiple whitespace with a single '_' */
184 if (isspace(str
[i
])) {
185 while (isspace(str
[i
]))
195 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
196 int util_replace_chars(char *str
, const char *white
)
201 while (str
[i
] != '\0') {
204 if (whitelisted_char_for_devnode(str
[i
], white
)) {
209 /* accept hex encoding */
210 if (str
[i
] == '\\' && str
[i
+1] == 'x') {
215 /* accept valid utf8 */
216 len
= utf8_encoded_valid_unichar(&str
[i
]);
222 /* if space is allowed, replace whitespace with ordinary space */
223 if (isspace(str
[i
]) && white
!= NULL
&& strchr(white
, ' ') != NULL
) {
230 /* everything else is replaced with '_' */
239 * udev_util_encode_string:
240 * @str: input string to be encoded
241 * @str_enc: output string to store the encoded input string
242 * @len: maximum size of the output string, which may be
243 * four times as long as the input string
245 * Encode all potentially unsafe characters of a string to the
246 * corresponding 2 char hex value prefixed by '\x'.
248 * Returns: 0 if the entire string was copied, non-zero otherwise.
250 _public_
int udev_util_encode_string(const char *str
, char *str_enc
, size_t len
)
252 return encode_devnode_name(str
, str_enc
, len
);
255 unsigned int util_string_hash32(const char *str
)
257 return MurmurHash2(str
, strlen(str
), 0);
260 /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
261 uint64_t util_string_bloom64(const char *str
)
264 unsigned int hash
= util_string_hash32(str
);
266 bits
|= 1LLU << (hash
& 63);
267 bits
|= 1LLU << ((hash
>> 6) & 63);
268 bits
|= 1LLU << ((hash
>> 12) & 63);
269 bits
|= 1LLU << ((hash
>> 18) & 63);