X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=libudev%2Flibudev-util.c;h=e08349e0fa345d051a30d66d4d7a03f3b06592ea;hb=6dd19db57c672995aa814d3f9cfd1df9d5b723ef;hp=dcc4a0fd16e78e9270a352f13da947c4d60d0e90;hpb=48a9b173e88738ff4eefb3519f1d27711b417c8d;p=elogind.git diff --git a/libudev/libudev-util.c b/libudev/libudev-util.c index dcc4a0fd1..e08349e0f 100644 --- a/libudev/libudev-util.c +++ b/libudev/libudev-util.c @@ -1,7 +1,7 @@ /* * libudev - interface to udev device information * - * Copyright (C) 2008-2009 Kay Sievers + * Copyright (C) 2008-2011 Kay Sievers * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -18,23 +18,30 @@ #include #include #include +#include #include #include "libudev.h" #include "libudev-private.h" -static ssize_t get_sys_link(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size) +/** + * SECTION:libudev-util + * @short_description: utils + */ + +ssize_t util_get_sys_core_link_value(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size) { char path[UTIL_PATH_SIZE]; + char target[UTIL_PATH_SIZE]; ssize_t len; const char *pos; util_strscpyl(path, sizeof(path), syspath, "/", slink, NULL); - len = readlink(path, path, sizeof(path)); - if (len < 0 || len >= (ssize_t) sizeof(path)) + len = readlink(path, target, sizeof(target)); + if (len <= 0 || len == (ssize_t)sizeof(target)) return -1; - path[len] = '\0'; - pos = strrchr(path, '/'); + target[len] = '\0'; + pos = strrchr(target, '/'); if (pos == NULL) return -1; pos = &pos[1]; @@ -42,27 +49,17 @@ static ssize_t get_sys_link(struct udev *udev, const char *slink, const char *sy return util_strscpy(value, size, pos); } -ssize_t util_get_sys_subsystem(struct udev *udev, const char *syspath, char *subsystem, size_t size) -{ - return get_sys_link(udev, "subsystem", syspath, subsystem, size); -} - -ssize_t util_get_sys_driver(struct udev *udev, const char *syspath, char *driver, size_t size) -{ - return get_sys_link(udev, "driver", syspath, driver, size); -} - int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size) { char link_target[UTIL_PATH_SIZE]; - int len; + ssize_t len; int i; int back; char *base; len = readlink(syspath, link_target, sizeof(link_target)); - if (len <= 0) + if (len <= 0 || len == (ssize_t)sizeof(link_target)) return -1; link_target[len] = '\0'; dbg(udev, "path link '%s' points to '%s'\n", syspath, link_target); @@ -87,13 +84,13 @@ int util_log_priority(const char *priority) int prio; prio = strtol(priority, &endptr, 10); - if (endptr[0] == '\0') + if (endptr[0] == '\0' || isspace(endptr[0])) return prio; if (strncmp(priority, "err", 3) == 0) return LOG_ERR; - if (strcmp(priority, "info") == 0) + if (strncmp(priority, "info", 4) == 0) return LOG_INFO; - if (strcmp(priority, "debug") == 0) + if (strncmp(priority, "debug", 5) == 0) return LOG_DEBUG; return 0; } @@ -350,7 +347,7 @@ static int utf8_encoded_valid_unichar(const char *str) return len; } -int udev_util_replace_whitespace(const char *str, char *to, size_t len) +int util_replace_whitespace(const char *str, char *to, size_t len) { size_t i, j; @@ -390,7 +387,7 @@ static int is_whitelisted(char c, const char *white) } /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */ -int udev_util_replace_chars(char *str, const char *white) +int util_replace_chars(char *str, const char *white) { size_t i = 0; int replaced = 0; @@ -433,70 +430,134 @@ int udev_util_replace_chars(char *str, const char *white) } /** - * util_encode_string: + * udev_util_encode_string: * @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 - * corresponding hex value prefixed by '\x'. + * corresponding 2 char hex value prefixed by '\x'. * * Returns: 0 if the entire string was copied, non-zero otherwise. **/ -int udev_util_encode_string(const char *str, char *str_enc, size_t len) +UDEV_EXPORT int udev_util_encode_string(const char *str, char *str_enc, size_t len) { size_t i, j; - if (str == NULL || str_enc == NULL || len == 0) + if (str == NULL || str_enc == NULL) return -1; - str_enc[0] = '\0'; for (i = 0, j = 0; str[i] != '\0'; i++) { int seqlen; seqlen = utf8_encoded_valid_unichar(&str[i]); if (seqlen > 1) { + if (len-j < (size_t)seqlen) + goto err; memcpy(&str_enc[j], &str[i], seqlen); j += seqlen; i += (seqlen-1); } else if (str[i] == '\\' || !is_whitelisted(str[i], NULL)) { + if (len-j < 4) + goto err; sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]); j += 4; } else { + if (len-j < 1) + goto err; str_enc[j] = str[i]; j++; } - if (j+3 >= len) - goto err; } + if (len-j < 1) + goto err; str_enc[j] = '\0'; return 0; err: return -1; } -void util_set_fd_cloexec(int fd) +/* + * http://sites.google.com/site/murmurhash/ + * + * All code is released to the public domain. For business purposes, + * Murmurhash is under the MIT license. + * + */ +static unsigned int murmur_hash2(const char *key, int len, unsigned int seed) { - int flags; - - flags = fcntl(fd, F_GETFD); - if (flags < 0) - flags = FD_CLOEXEC; - else - flags |= FD_CLOEXEC; - fcntl(fd, F_SETFD, flags); + /* + * 'm' and 'r' are mixing constants generated offline. + * They're not really 'magic', they just happen to work well. + */ + const unsigned int m = 0x5bd1e995; + const int r = 24; + + /* initialize the hash to a 'random' value */ + unsigned int h = seed ^ len; + + /* mix 4 bytes at a time into the hash */ + const unsigned char * data = (const unsigned char *)key; + + while(len >= 4) { + unsigned int k = *(unsigned int *)data; + + k *= m; + k ^= k >> r; + k *= m; + h *= m; + h ^= k; + + data += 4; + len -= 4; + } + + /* handle the last few bytes of the input array */ + switch(len) { + case 3: + h ^= data[2] << 16; + case 2: + h ^= data[1] << 8; + case 1: + h ^= data[0]; + h *= m; + }; + + /* do a few final mixes of the hash to ensure the last few bytes are well-incorporated */ + h ^= h >> 13; + h *= m; + h ^= h >> 15; + + return h; } unsigned int util_string_hash32(const char *str) { - unsigned int hash = 0; + return murmur_hash2(str, strlen(str), 0); +} - while (str[0] != '\0') { - hash += str[0] << 4; - hash += str[0] >> 4; - hash *= 11; - str++; - } - return hash; +/* get a bunch of bit numbers out of the hash, and set the bits in our bit field */ +uint64_t util_string_bloom64(const char *str) +{ + uint64_t bits = 0; + unsigned int hash = util_string_hash32(str); + + bits |= 1LLU << (hash & 63); + bits |= 1LLU << ((hash >> 6) & 63); + bits |= 1LLU << ((hash >> 12) & 63); + bits |= 1LLU << ((hash >> 18) & 63); + return bits; +} + +#define USEC_PER_SEC 1000000ULL +#define NSEC_PER_USEC 1000ULL +unsigned long long now_usec(void) +{ + struct timespec ts; + + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) + return 0; + return (unsigned long long) ts.tv_sec * USEC_PER_SEC + + (unsigned long long) ts.tv_nsec / NSEC_PER_USEC; }