2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2008-08-27 22:02:41 +02:00
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <ctype.h>
|
2008-08-27 22:02:41 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2018-11-21 09:27:38 +01:00
|
|
|
#include "sd-device.h"
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "device-nodes.h"
|
2018-11-16 03:44:17 +01:00
|
|
|
#include "libudev-util.h"
|
2018-11-21 08:17:36 +01:00
|
|
|
#include "string-util.h"
|
2018-11-16 03:44:17 +01:00
|
|
|
#include "strxcpyx.h"
|
2013-09-17 21:39:09 +02:00
|
|
|
#include "utf8.h"
|
2008-08-27 22:02:41 +02:00
|
|
|
|
2011-10-06 01:10:59 +02:00
|
|
|
/**
|
|
|
|
* SECTION:libudev-util
|
|
|
|
* @short_description: utils
|
2012-04-20 03:25:36 +02:00
|
|
|
*
|
|
|
|
* Utilities useful when dealing with devices and device node names.
|
2011-10-06 01:10:59 +02:00
|
|
|
*/
|
|
|
|
|
2012-04-17 00:26:02 +02:00
|
|
|
/* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
|
2018-11-21 09:27:38 +01:00
|
|
|
int util_resolve_subsys_kernel(const char *string, char *result, size_t maxsize, bool read_value) {
|
|
|
|
char temp[UTIL_PATH_SIZE], *subsys, *sysname, *attr;
|
|
|
|
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
|
|
|
|
const char *val;
|
|
|
|
int r;
|
2012-04-17 00:26:02 +02:00
|
|
|
|
|
|
|
if (string[0] != '[')
|
2018-11-21 09:27:38 +01:00
|
|
|
return -EINVAL;
|
2012-04-17 00:26:02 +02:00
|
|
|
|
2013-01-09 19:06:46 +01:00
|
|
|
strscpy(temp, sizeof(temp), string);
|
2012-04-17 00:26:02 +02:00
|
|
|
|
|
|
|
subsys = &temp[1];
|
|
|
|
|
|
|
|
sysname = strchr(subsys, '/');
|
2018-10-29 18:40:23 +01:00
|
|
|
if (!sysname)
|
2018-11-21 09:27:38 +01:00
|
|
|
return -EINVAL;
|
2012-04-17 00:26:02 +02:00
|
|
|
sysname[0] = '\0';
|
|
|
|
sysname = &sysname[1];
|
|
|
|
|
|
|
|
attr = strchr(sysname, ']');
|
2018-10-29 18:40:23 +01:00
|
|
|
if (!attr)
|
2018-11-21 09:27:38 +01:00
|
|
|
return -EINVAL;
|
2012-04-17 00:26:02 +02:00
|
|
|
attr[0] = '\0';
|
|
|
|
attr = &attr[1];
|
|
|
|
if (attr[0] == '/')
|
|
|
|
attr = &attr[1];
|
|
|
|
if (attr[0] == '\0')
|
|
|
|
attr = NULL;
|
|
|
|
|
2018-10-29 18:40:23 +01:00
|
|
|
if (read_value && !attr)
|
2018-11-21 09:27:38 +01:00
|
|
|
return -EINVAL;
|
2012-04-17 00:26:02 +02:00
|
|
|
|
2018-11-21 09:27:38 +01:00
|
|
|
r = sd_device_new_from_subsystem_sysname(&dev, subsys, sysname);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-04-17 00:26:02 +02:00
|
|
|
|
|
|
|
if (read_value) {
|
2018-11-21 09:27:38 +01:00
|
|
|
r = sd_device_get_sysattr_value(dev, attr, &val);
|
|
|
|
if (r < 0 && r != -ENOENT)
|
|
|
|
return r;
|
|
|
|
if (r == -ENOENT)
|
2012-04-17 00:26:02 +02:00
|
|
|
result[0] = '\0';
|
2018-11-21 09:27:38 +01:00
|
|
|
else
|
|
|
|
strscpy(result, maxsize, val);
|
2014-11-28 14:26:31 +01:00
|
|
|
log_debug("value '[%s/%s]%s' is '%s'", subsys, sysname, attr, result);
|
2012-04-17 00:26:02 +02:00
|
|
|
} else {
|
2018-11-21 09:27:38 +01:00
|
|
|
r = sd_device_get_syspath(dev, &val);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
strscpyl(result, maxsize, val, attr ? "/" : NULL, attr ?: NULL, NULL);
|
|
|
|
log_debug("path '[%s/%s]%s' is '%s'", subsys, sysname, strempty(attr), result);
|
2012-04-17 00:26:02 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-13 14:45:51 +01:00
|
|
|
|
2020-06-23 08:31:16 +02:00
|
|
|
/* allow chars in allow list, plain ascii, hex-escaping and valid utf8 */
|
|
|
|
size_t util_replace_chars(char *str, const char *allow) {
|
2018-11-21 08:35:20 +01:00
|
|
|
size_t i = 0, replaced = 0;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-11-21 08:32:54 +01:00
|
|
|
assert(str);
|
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
while (str[i] != '\0') {
|
|
|
|
int len;
|
|
|
|
|
2020-06-23 08:31:16 +02:00
|
|
|
if (allow_listed_char_for_devnode(str[i], allow)) {
|
2012-01-10 01:34:15 +01:00
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* accept hex encoding */
|
|
|
|
if (str[i] == '\\' && str[i+1] == 'x') {
|
|
|
|
i += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* accept valid utf8 */
|
2019-02-26 12:37:40 +01:00
|
|
|
len = utf8_encoded_valid_unichar(str + i, (size_t) -1);
|
2012-01-10 01:34:15 +01:00
|
|
|
if (len > 1) {
|
|
|
|
i += len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if space is allowed, replace whitespace with ordinary space */
|
2020-06-23 08:31:16 +02:00
|
|
|
if (isspace(str[i]) && allow && strchr(allow, ' ')) {
|
2012-01-10 01:34:15 +01:00
|
|
|
str[i] = ' ';
|
|
|
|
i++;
|
|
|
|
replaced++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* everything else is replaced with '_' */
|
|
|
|
str[i] = '_';
|
|
|
|
i++;
|
|
|
|
replaced++;
|
|
|
|
}
|
|
|
|
return replaced;
|
2008-09-10 18:24:39 +02:00
|
|
|
}
|
2008-11-05 21:49:52 +01:00
|
|
|
|
|
|
|
/**
|
2011-10-06 01:10:59 +02:00
|
|
|
* udev_util_encode_string:
|
2008-11-05 21:49:52 +01:00
|
|
|
* @str: input string to be encoded
|
|
|
|
* @str_enc: output string to store the encoded input string
|
|
|
|
* @len: maximum size of the output string, which may be
|
|
|
|
* four times as long as the input string
|
|
|
|
*
|
|
|
|
* Encode all potentially unsafe characters of a string to the
|
2011-10-06 01:10:59 +02:00
|
|
|
* corresponding 2 char hex value prefixed by '\x'.
|
2008-11-05 21:49:52 +01:00
|
|
|
*
|
|
|
|
* Returns: 0 if the entire string was copied, non-zero otherwise.
|
|
|
|
**/
|
2018-08-22 12:51:32 +02:00
|
|
|
_public_ int udev_util_encode_string(const char *str, char *str_enc, size_t len) {
|
2013-09-18 18:12:04 +02:00
|
|
|
return encode_devnode_name(str, str_enc, len);
|
2008-11-05 21:49:52 +01:00
|
|
|
}
|