1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

convert to libudev and delete udev_utils_string.c

This commit is contained in:
Kay Sievers 2008-09-10 18:39:23 +02:00
parent 7525097713
commit ecc9ec579f
14 changed files with 27 additions and 329 deletions

View File

@ -13,8 +13,7 @@ create_floppy_devices_SOURCES = \
../../udev/lib/libudev.c \
../../udev/lib/libudev-util.c \
../../udev/udev_sysdeps.c \
../../udev/udev_utils.c \
../../udev/udev_utils_string.c
../../udev/udev_utils.c
if USE_SELINUX
create_floppy_devices_LDADD = \

View File

@ -14,8 +14,7 @@ usb_id_SOURCES = \
../../udev/lib/libudev-util.c \
../../udev/udev_sysdeps.c \
../../udev/udev_sysfs.c \
../../udev/udev_utils.c \
../../udev/udev_utils_string.c
../../udev/udev_utils.c
if USE_SELINUX
usb_id_LDADD = \

View File

@ -29,7 +29,6 @@ common_files = \
udev_sysfs.c \
udev_utils.c \
udev_utils_file.c \
udev_utils_string.c \
lib/list.h \
lib/libudev.h \
lib/libudev-private.h \

View File

@ -171,13 +171,6 @@ extern int add_matching_files(struct udev *udev, struct list_head *name_list, co
extern uid_t lookup_user(struct udev *udev, const char *user);
extern gid_t lookup_group(struct udev *udev, const char *group);
/* udev_utils_string.c */
extern int string_is_true(const char *str);
extern void remove_trailing_chars(char *path, char c);
extern size_t path_encode(char *s, size_t len);
extern size_t path_decode(char *s);
extern int replace_chars(char *str, const char *white);
/* udev_utils_file.c */
extern int create_path(struct udev *udev, const char *path);
extern int delete_path(struct udev *udev, const char *path);

View File

@ -38,7 +38,7 @@ static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *f
strlcpy(filename, udev_get_dev_path(udev), len);
start = strlcat(filename, "/.udev/db/", len);
strlcat(filename, devpath, len);
return path_encode(&filename[start], len - start);
return util_path_encode(&filename[start], len - start);
}
/* reverse mapping from the device file name to the devpath */
@ -53,10 +53,10 @@ static int name_index(struct udev *udev, const char *devpath, const char *name,
strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
start = strlcat(filename, "/.udev/names/", sizeof(filename));
strlcat(filename, name, sizeof(filename));
path_encode(&filename[start], sizeof(filename) - start);
util_path_encode(&filename[start], sizeof(filename) - start);
/* entry with the devpath */
strlcpy(device, devpath, sizeof(device));
path_encode(device, sizeof(device));
util_path_encode(device, sizeof(device));
strlcat(filename, "/", sizeof(filename));
strlcat(filename, device, sizeof(filename));
@ -84,7 +84,7 @@ int udev_db_get_devices_by_name(struct udev *udev, const char *name, struct list
strlcpy(dirname, udev_get_dev_path(udev), sizeof(dirname));
start = strlcat(dirname, "/.udev/names/", sizeof(dirname));
strlcat(dirname, name, sizeof(dirname));
path_encode(&dirname[start], sizeof(dirname) - start);
util_path_encode(&dirname[start], sizeof(dirname) - start);
dir = opendir(dirname);
if (dir == NULL) {
@ -105,7 +105,7 @@ int udev_db_get_devices_by_name(struct udev *udev, const char *name, struct list
continue;
strlcpy(device, ent->d_name, sizeof(device));
path_decode(device);
util_path_decode(device);
name_list_add(udev, name_list, device, 0);
rc++;
}
@ -317,7 +317,7 @@ int udev_db_get_all_entries(struct udev *udev, struct list_head *name_list)
continue;
strlcpy(device, ent->d_name, sizeof(device));
path_decode(device);
util_path_decode(device);
name_list_add(udev, name_list, device, 1);
dbg(udev, "added '%s'\n", device);
}

View File

@ -283,7 +283,7 @@ void udev_node_update_symlinks(struct udevice *udevice, struct udevice *udevice_
}
/* export symlinks to environment */
remove_trailing_chars(symlinks, ' ');
util_remove_trailing_chars(symlinks, ' ');
if (symlinks[0] != '\0')
setenv("DEVLINKS", symlinks, 1);

View File

@ -863,7 +863,7 @@ found:
size = sizeof(temp2)-1;
while (size > 0 && isspace(temp2[size-1]))
temp2[--size] = '\0';
count = replace_chars(temp2, ALLOWED_CHARS_INPUT);
count = util_replace_chars(temp2, ALLOWED_CHARS_INPUT);
if (count > 0)
info(udevice->udev, "%i character(s) replaced\n" , count);
strlcat(string, temp2, maxsize);
@ -921,7 +921,7 @@ found:
strlcat(symlinks, name_loop->name, sizeof(symlinks));
strlcat(symlinks, " ", sizeof(symlinks));
}
remove_trailing_chars(symlinks, ' ');
util_remove_trailing_chars(symlinks, ' ');
strlcat(string, symlinks, maxsize);
}
break;
@ -1240,10 +1240,10 @@ try_parent:
int count;
dbg(udevice->udev, "PROGRAM matches\n");
remove_trailing_chars(result, '\n');
util_remove_trailing_chars(result, '\n');
if (rule->string_escape == ESCAPE_UNSET ||
rule->string_escape == ESCAPE_REPLACE) {
count = replace_chars(result, ALLOWED_CHARS_INPUT);
count = util_replace_chars(result, ALLOWED_CHARS_INPUT);
if (count > 0)
info(udevice->udev, "%i character(s) replaced\n" , count);
}
@ -1461,7 +1461,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udevice)
udev_rules_apply_format(udevice, temp, sizeof(temp));
if (rule->string_escape == ESCAPE_UNSET ||
rule->string_escape == ESCAPE_REPLACE) {
count = replace_chars(temp, ALLOWED_CHARS_FILE " ");
count = util_replace_chars(temp, ALLOWED_CHARS_FILE " ");
if (count > 0)
info(udevice->udev, "%i character(s) replaced\n" , count);
}
@ -1496,7 +1496,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udevice)
udev_rules_apply_format(udevice, udevice->name, sizeof(udevice->name));
if (rule->string_escape == ESCAPE_UNSET ||
rule->string_escape == ESCAPE_REPLACE) {
count = replace_chars(udevice->name, ALLOWED_CHARS_FILE);
count = util_replace_chars(udevice->name, ALLOWED_CHARS_FILE);
if (count > 0)
info(udevice->udev, "%i character(s) replaced\n", count);
}

View File

@ -156,7 +156,7 @@ struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath)
dbg(udev, "open '%s'\n", devpath);
strlcpy(devpath_real, devpath, sizeof(devpath_real));
remove_trailing_chars(devpath_real, '/');
util_remove_trailing_chars(devpath_real, '/');
if (devpath[0] == '\0' )
return NULL;
@ -397,7 +397,7 @@ char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *a
/* got a valid value, store and return it */
value[size] = '\0';
remove_trailing_chars(value, '\n');
util_remove_trailing_chars(value, '\n');
dbg(udev, "cache '%s' with attribute value '%s'\n", path_full, value);
strlcpy(attr->value_local, value, sizeof(attr->value_local));
attr->value = attr->value_local;

View File

@ -32,27 +32,6 @@
#include "udev.h"
int log_priority(const char *priority)
{
char *endptr;
int prio;
prio = strtol(priority, &endptr, 10);
if (endptr[0] == '\0')
return prio;
if (strncasecmp(priority, "err", 3) == 0)
return LOG_ERR;
if (strcasecmp(priority, "info") == 0)
return LOG_INFO;
if (strcasecmp(priority, "debug") == 0)
return LOG_DEBUG;
if (string_is_true(priority))
return LOG_ERR;
return 0;
}
struct name_entry *name_list_add(struct udev *udev, struct list_head *name_list, const char *name, int sort)
{
struct name_entry *name_loop;

View File

@ -1,271 +0,0 @@
/*
* Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <dirent.h>
#include <syslog.h>
#include <sys/utsname.h>
#include "udev.h"
int string_is_true(const char *str)
{
if (strcasecmp(str, "true") == 0)
return 1;
if (strcasecmp(str, "yes") == 0)
return 1;
if (strcasecmp(str, "1") == 0)
return 1;
return 0;
}
void remove_trailing_chars(char *path, char c)
{
size_t len;
if (path == NULL)
return;
len = strlen(path);
while (len > 0 && path[len-1] == c)
path[--len] = '\0';
}
size_t path_encode(char *s, size_t len)
{
char t[(len * 3)+1];
size_t i, j;
t[0] = '\0';
for (i = 0, j = 0; s[i] != '\0'; i++) {
if (s[i] == '/') {
memcpy(&t[j], "\\x2f", 4);
j += 4;
} else if (s[i] == '\\') {
memcpy(&t[j], "\\x5c", 4);
j += 4;
} else {
t[j] = s[i];
j++;
}
}
t[j] = '\0';
strncpy(s, t, len);
return j;
}
size_t path_decode(char *s)
{
size_t i, j;
for (i = 0, j = 0; s[i] != '\0'; j++) {
if (memcmp(&s[i], "\\x2f", 4) == 0) {
s[j] = '/';
i += 4;
}else if (memcmp(&s[i], "\\x5c", 4) == 0) {
s[j] = '\\';
i += 4;
} else {
s[j] = s[i];
i++;
}
}
s[j] = '\0';
return j;
}
/* count of characters used to encode one unicode char */
static int utf8_encoded_expected_len(const char *str)
{
unsigned char c = (unsigned char)str[0];
if (c < 0x80)
return 1;
if ((c & 0xe0) == 0xc0)
return 2;
if ((c & 0xf0) == 0xe0)
return 3;
if ((c & 0xf8) == 0xf0)
return 4;
if ((c & 0xfc) == 0xf8)
return 5;
if ((c & 0xfe) == 0xfc)
return 6;
return 0;
}
/* decode one unicode char */
static int utf8_encoded_to_unichar(const char *str)
{
int unichar;
int len;
int i;
len = utf8_encoded_expected_len(str);
switch (len) {
case 1:
return (int)str[0];
case 2:
unichar = str[0] & 0x1f;
break;
case 3:
unichar = (int)str[0] & 0x0f;
break;
case 4:
unichar = (int)str[0] & 0x07;
break;
case 5:
unichar = (int)str[0] & 0x03;
break;
case 6:
unichar = (int)str[0] & 0x01;
break;
default:
return -1;
}
for (i = 1; i < len; i++) {
if (((int)str[i] & 0xc0) != 0x80)
return -1;
unichar <<= 6;
unichar |= (int)str[i] & 0x3f;
}
return unichar;
}
/* expected size used to encode one unicode char */
static int utf8_unichar_to_encoded_len(int unichar)
{
if (unichar < 0x80)
return 1;
if (unichar < 0x800)
return 2;
if (unichar < 0x10000)
return 3;
if (unichar < 0x200000)
return 4;
if (unichar < 0x4000000)
return 5;
return 6;
}
/* check if unicode char has a valid numeric range */
static int utf8_unichar_valid_range(int unichar)
{
if (unichar > 0x10ffff)
return 0;
if ((unichar & 0xfffff800) == 0xd800)
return 0;
if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
return 0;
if ((unichar & 0xffff) == 0xffff)
return 0;
return 1;
}
/* validate one encoded unicode char and return its length */
static int utf8_encoded_valid_unichar(const char *str)
{
int len;
int unichar;
int i;
len = utf8_encoded_expected_len(str);
if (len == 0)
return -1;
/* ascii is valid */
if (len == 1)
return 1;
/* check if expected encoded chars are available */
for (i = 0; i < len; i++)
if ((str[i] & 0x80) != 0x80)
return -1;
unichar = utf8_encoded_to_unichar(str);
/* check if encoded length matches encoded value */
if (utf8_unichar_to_encoded_len(unichar) != len)
return -1;
/* check if value has valid range */
if (!utf8_unichar_valid_range(unichar))
return -1;
return len;
}
/* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
int replace_chars(char *str, const char *white)
{
size_t i = 0;
int replaced = 0;
while (str[i] != '\0') {
int len;
/* accept whitelist */
if (white != NULL && strchr(white, str[i]) != NULL) {
i++;
continue;
}
/* accept plain ascii char */
if ((str[i] >= '0' && str[i] <= '9') ||
(str[i] >= 'A' && str[i] <= 'Z') ||
(str[i] >= 'a' && str[i] <= 'z')) {
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]);
if (len > 1) {
i += len;
continue;
}
/* if space is allowed, replace whitespace with ordinary space */
if (isspace(str[i]) && strchr(white, ' ') != NULL) {
str[i] = ' ';
i++;
replaced++;
continue;
}
/* everything else is replaced with '_' */
str[i] = '_';
i++;
replaced++;
}
return replaced;
}

View File

@ -91,12 +91,12 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
switch (option) {
case 'l':
case 'l' + 256:
i = log_priority(optarg);
i = util_log_priority(optarg);
if (i < 0) {
fprintf(stderr, "invalid number '%s'\n", optarg);
goto exit;
}
udev_ctrl_send_set_log_level(uctrl, log_priority(optarg));
udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg));
rc = 0;
break;
case 's':
@ -151,7 +151,7 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
"this will stop working in a future release\n");
if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
udev_ctrl_send_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
udev_ctrl_send_set_log_level(uctrl, util_log_priority(&arg[strlen("log_priority=")]));
rc = 0;
goto exit;
} else if (!strcmp(arg, "stop_exec_queue")) {

View File

@ -289,7 +289,7 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
strlcpy(name, &optarg[strlen(udev_get_dev_path(udev))+1], sizeof(name));
else
strlcpy(name, optarg, sizeof(name));
remove_trailing_chars(name, '/');
util_remove_trailing_chars(name, '/');
dbg(udev, "name: %s\n", name);
break;
case 'p':
@ -298,7 +298,7 @@ int udevadm_info(struct udev *udev, int argc, char *argv[])
strlcpy(path, &optarg[strlen(udev_get_sys_path(udev))], sizeof(path));
else
strlcpy(path, optarg, sizeof(path));
remove_trailing_chars(path, '/');
util_remove_trailing_chars(path, '/');
/* possibly resolve to real devpath */
if (sysfs_resolve_link(udev, path, sizeof(path)) != 0) {

View File

@ -179,7 +179,7 @@ static int pass_to_socket(struct udev *udev, const char *devpath, const char *ac
strlcat(path, name_loop->name, sizeof(path));
strlcat(path, " ", sizeof(path));
}
remove_trailing_chars(path, ' ');
util_remove_trailing_chars(path, ' ');
if (path[0] != '\0') {
bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVLINKS=%s", path);
bufpos++;
@ -316,7 +316,7 @@ static int attr_match(const char *path, const char *attr_value)
if (size < 0)
return 0;
value[size] = '\0';
remove_trailing_chars(value, '\n');
util_remove_trailing_chars(value, '\n');
/* match if attribute value matches */
if (fnmatch(match_value, value, 0) == 0)
@ -553,7 +553,7 @@ static void scan_failed(struct udev *udev)
if(start >= sizeof(device))
start = sizeof(device) - 1;
strlcat(device, dent->d_name, sizeof(device));
path_decode(&device[start]);
util_path_decode(&device[start]);
device_list_insert(udev, device);
}
closedir(dir);

View File

@ -180,7 +180,7 @@ static void export_event_state(struct udevd_uevent_msg *msg, enum event_state st
strlcat(filename_failed, "/", sizeof(filename_failed));
start = strlcat(filename_failed, ".udev/failed/", sizeof(filename_failed));
strlcat(filename_failed, msg->devpath, sizeof(filename_failed));
path_encode(&filename_failed[start], sizeof(filename_failed) - start);
util_path_encode(&filename_failed[start], sizeof(filename_failed) - start);
switch (state) {
case EVENT_QUEUED:
@ -200,7 +200,7 @@ static void export_event_state(struct udevd_uevent_msg *msg, enum event_state st
strlcat(filename_failed_old, "/", sizeof(filename_failed_old));
start = strlcat(filename_failed_old, ".udev/failed/", sizeof(filename_failed_old));
strlcat(filename_failed_old, msg->devpath_old, sizeof(filename_failed_old));
path_encode(&filename_failed_old[start], sizeof(filename) - start);
util_path_encode(&filename_failed_old[start], sizeof(filename) - start);
if (rename(filename_failed_old, filename_failed) == 0)
info(msg->udev, "renamed devpath, moved failed state of '%s' to %s'\n",