mirror of
https://github.com/systemd/systemd.git
synced 2025-01-11 09:18:07 +03:00
udev: move util_replace_chars() to udev-util.c
This commit is contained in:
parent
9a56b87717
commit
393fcaf733
src
@ -75,49 +75,6 @@ int util_resolve_subsys_kernel(const char *string, char *result, size_t maxsize,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* allow chars in allow list, plain ascii, hex-escaping and valid utf8 */
|
||||
size_t util_replace_chars(char *str, const char *allow) {
|
||||
size_t i = 0, replaced = 0;
|
||||
|
||||
assert(str);
|
||||
|
||||
while (str[i] != '\0') {
|
||||
int len;
|
||||
|
||||
if (allow_listed_char_for_devnode(str[i], allow)) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* accept hex encoding */
|
||||
if (str[i] == '\\' && str[i+1] == 'x') {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* accept valid utf8 */
|
||||
len = utf8_encoded_valid_unichar(str + i, (size_t) -1);
|
||||
if (len > 1) {
|
||||
i += len;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* if space is allowed, replace whitespace with ordinary space */
|
||||
if (isspace(str[i]) && allow && strchr(allow, ' ')) {
|
||||
str[i] = ' ';
|
||||
i++;
|
||||
replaced++;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* everything else is replaced with '_' */
|
||||
str[i] = '_';
|
||||
i++;
|
||||
replaced++;
|
||||
}
|
||||
return replaced;
|
||||
}
|
||||
|
||||
/**
|
||||
* udev_util_encode_string:
|
||||
* @str: input string to be encoded
|
||||
|
@ -9,8 +9,6 @@
|
||||
#define UTIL_PATH_SIZE 1024
|
||||
#define UTIL_NAME_SIZE 512
|
||||
#define UTIL_LINE_SIZE 16384
|
||||
#define UDEV_ALLOWED_CHARS_INPUT "/ $%?,"
|
||||
size_t util_replace_chars(char *str, const char *white);
|
||||
int util_resolve_subsys_kernel(const char *string, char *result, size_t maxsize, bool read_value);
|
||||
|
||||
/* Cleanup functions */
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "device-nodes.h"
|
||||
#include "device-util.h"
|
||||
#include "env-file.h"
|
||||
#include "escape.h"
|
||||
@ -426,3 +427,47 @@ size_t udev_replace_whitespace(const char *str, char *to, size_t len) {
|
||||
to[j] = '\0';
|
||||
return j;
|
||||
}
|
||||
|
||||
size_t udev_replace_chars(char *str, const char *allow) {
|
||||
size_t i = 0, replaced = 0;
|
||||
|
||||
assert(str);
|
||||
|
||||
/* allow chars in allow list, plain ascii, hex-escaping and valid utf8. */
|
||||
|
||||
while (str[i] != '\0') {
|
||||
int len;
|
||||
|
||||
if (allow_listed_char_for_devnode(str[i], allow)) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* accept hex encoding */
|
||||
if (str[i] == '\\' && str[i+1] == 'x') {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* accept valid utf8 */
|
||||
len = utf8_encoded_valid_unichar(str + i, (size_t) -1);
|
||||
if (len > 1) {
|
||||
i += len;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* if space is allowed, replace whitespace with ordinary space */
|
||||
if (isspace(str[i]) && allow && strchr(allow, ' ')) {
|
||||
str[i] = ' ';
|
||||
i++;
|
||||
replaced++;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* everything else is replaced with '_' */
|
||||
str[i] = '_';
|
||||
i++;
|
||||
replaced++;
|
||||
}
|
||||
return replaced;
|
||||
}
|
||||
|
@ -37,3 +37,4 @@ void log_device_uevent(sd_device *device, const char *str);
|
||||
|
||||
int udev_rule_parse_value(char *str, char **ret_value, char **ret_endpos);
|
||||
size_t udev_replace_whitespace(const char *str, char *to, size_t len);
|
||||
size_t udev_replace_chars(char *str, const char *allow);
|
||||
|
@ -485,11 +485,11 @@ int main(int argc, char *argv[]) {
|
||||
model[40] = '\0';
|
||||
udev_util_encode_string(model, model_enc, sizeof(model_enc));
|
||||
udev_replace_whitespace((char *) id.model, model, 40);
|
||||
util_replace_chars(model, NULL);
|
||||
udev_replace_chars(model, NULL);
|
||||
udev_replace_whitespace((char *) id.serial_no, serial, 20);
|
||||
util_replace_chars(serial, NULL);
|
||||
udev_replace_chars(serial, NULL);
|
||||
udev_replace_whitespace((char *) id.fw_rev, revision, 8);
|
||||
util_replace_chars(revision, NULL);
|
||||
udev_replace_chars(revision, NULL);
|
||||
|
||||
if (export) {
|
||||
/* Set this to convey the disk speaks the ATA protocol */
|
||||
|
@ -457,12 +457,12 @@ static int set_inq_values(struct scsi_id_device *dev_scsi, const char *path) {
|
||||
udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
|
||||
|
||||
udev_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str)-1);
|
||||
util_replace_chars(vendor_str, NULL);
|
||||
udev_replace_chars(vendor_str, NULL);
|
||||
udev_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str)-1);
|
||||
util_replace_chars(model_str, NULL);
|
||||
udev_replace_chars(model_str, NULL);
|
||||
set_type(dev_scsi->type, type_str, sizeof(type_str));
|
||||
udev_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str)-1);
|
||||
util_replace_chars(revision_str, NULL);
|
||||
udev_replace_chars(revision_str, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -503,10 +503,10 @@ static int scsi_id(char *maj_min_dev) {
|
||||
printf("ID_TYPE=%s\n", type_str);
|
||||
if (dev_scsi.serial[0] != '\0') {
|
||||
udev_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str)-1);
|
||||
util_replace_chars(serial_str, NULL);
|
||||
udev_replace_chars(serial_str, NULL);
|
||||
printf("ID_SERIAL=%s\n", serial_str);
|
||||
udev_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str)-1);
|
||||
util_replace_chars(serial_str, NULL);
|
||||
udev_replace_chars(serial_str, NULL);
|
||||
printf("ID_SERIAL_SHORT=%s\n", serial_str);
|
||||
}
|
||||
if (dev_scsi.wwn[0] != '\0') {
|
||||
@ -533,7 +533,7 @@ static int scsi_id(char *maj_min_dev) {
|
||||
char serial_str[MAX_SERIAL_LEN];
|
||||
|
||||
udev_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str)-1);
|
||||
util_replace_chars(serial_str, NULL);
|
||||
udev_replace_chars(serial_str, NULL);
|
||||
printf("%s\n", serial_str);
|
||||
goto out;
|
||||
}
|
||||
|
@ -331,7 +331,7 @@ static int builtin_usb_id(sd_device *dev, int argc, char *argv[], bool test) {
|
||||
}
|
||||
udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
|
||||
udev_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
|
||||
util_replace_chars(vendor_str, NULL);
|
||||
udev_replace_chars(vendor_str, NULL);
|
||||
|
||||
r = sd_device_get_sysattr_value(dev_scsi, "model", &scsi_model);
|
||||
if (r < 0) {
|
||||
@ -340,7 +340,7 @@ static int builtin_usb_id(sd_device *dev, int argc, char *argv[], bool test) {
|
||||
}
|
||||
udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
|
||||
udev_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
|
||||
util_replace_chars(model_str, NULL);
|
||||
udev_replace_chars(model_str, NULL);
|
||||
|
||||
r = sd_device_get_sysattr_value(dev_scsi, "type", &scsi_type);
|
||||
if (r < 0) {
|
||||
@ -355,7 +355,7 @@ static int builtin_usb_id(sd_device *dev, int argc, char *argv[], bool test) {
|
||||
goto fallback;
|
||||
}
|
||||
udev_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
|
||||
util_replace_chars(revision_str, NULL);
|
||||
udev_replace_chars(revision_str, NULL);
|
||||
|
||||
/*
|
||||
* some broken devices have the same identifiers
|
||||
@ -381,7 +381,7 @@ fallback:
|
||||
usb_vendor = vendor_id;
|
||||
udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
|
||||
udev_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
|
||||
util_replace_chars(vendor_str, NULL);
|
||||
udev_replace_chars(vendor_str, NULL);
|
||||
}
|
||||
|
||||
if (model_str[0] == '\0') {
|
||||
@ -391,7 +391,7 @@ fallback:
|
||||
usb_model = product_id;
|
||||
udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
|
||||
udev_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
|
||||
util_replace_chars(model_str, NULL);
|
||||
udev_replace_chars(model_str, NULL);
|
||||
}
|
||||
|
||||
if (revision_str[0] == '\0') {
|
||||
@ -399,7 +399,7 @@ fallback:
|
||||
|
||||
if (sd_device_get_sysattr_value(dev_usb, "bcdDevice", &usb_rev) >= 0) {
|
||||
udev_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
|
||||
util_replace_chars(revision_str, NULL);
|
||||
udev_replace_chars(revision_str, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@ -418,7 +418,7 @@ fallback:
|
||||
|
||||
if (usb_serial) {
|
||||
udev_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
|
||||
util_replace_chars(serial_str, NULL);
|
||||
udev_replace_chars(serial_str, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -364,7 +364,7 @@ static ssize_t udev_event_subst_format(
|
||||
if (val != vbuf)
|
||||
strscpy(vbuf, sizeof(vbuf), val);
|
||||
delete_trailing_chars(vbuf, NULL);
|
||||
count = util_replace_chars(vbuf, UDEV_ALLOWED_CHARS_INPUT);
|
||||
count = udev_replace_chars(vbuf, UDEV_ALLOWED_CHARS_INPUT);
|
||||
if (count > 0)
|
||||
log_device_debug(dev, "%i character(s) replaced", count);
|
||||
l = strpcpy(&s, l, vbuf);
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
#define READ_END 0
|
||||
#define WRITE_END 1
|
||||
#define UDEV_ALLOWED_CHARS_INPUT "/ $%?,"
|
||||
|
||||
typedef struct UdevEvent {
|
||||
sd_device *dev;
|
||||
|
@ -1684,7 +1684,7 @@ static int udev_rule_apply_token_to_event(
|
||||
}
|
||||
|
||||
delete_trailing_chars(result, "\n");
|
||||
count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
|
||||
count = udev_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
|
||||
if (count > 0)
|
||||
log_rule_debug(dev, rules, "Replaced %zu character(s) in result of \"%s\"",
|
||||
count, buf);
|
||||
@ -2048,7 +2048,7 @@ static int udev_rule_apply_token_to_event(
|
||||
|
||||
(void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
|
||||
if (IN_SET(event->esc, ESCAPE_UNSET, ESCAPE_REPLACE)) {
|
||||
count = util_replace_chars(buf, "/");
|
||||
count = udev_replace_chars(buf, "/");
|
||||
if (count > 0)
|
||||
log_rule_debug(dev, rules, "Replaced %zu character(s) from result of NAME=\"%s\"",
|
||||
count, token->value);
|
||||
@ -2082,9 +2082,9 @@ static int udev_rule_apply_token_to_event(
|
||||
/* allow multiple symlinks separated by spaces */
|
||||
(void) udev_event_apply_format(event, token->value, buf, sizeof(buf), event->esc != ESCAPE_NONE);
|
||||
if (event->esc == ESCAPE_UNSET)
|
||||
count = util_replace_chars(buf, "/ ");
|
||||
count = udev_replace_chars(buf, "/ ");
|
||||
else if (event->esc == ESCAPE_REPLACE)
|
||||
count = util_replace_chars(buf, "/");
|
||||
count = udev_replace_chars(buf, "/");
|
||||
else
|
||||
count = 0;
|
||||
if (count > 0)
|
||||
|
Loading…
Reference in New Issue
Block a user