X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=libudev%2Flibudev-util.c;h=51dd017467c6ea577825ceee35ceb1663d31cf0c;hp=c0209f9cc613cdaaf54c4ec533edf4bf91e6d10b;hb=672ff71911f2a42e6af030c3ef8830f51439b0d0;hpb=26347a4c5538008318188118872490128f43fcd3 diff --git a/libudev/libudev-util.c b/libudev/libudev-util.c index c0209f9cc..51dd01746 100644 --- a/libudev/libudev-util.c +++ b/libudev/libudev-util.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "libudev.h" @@ -26,15 +27,16 @@ static ssize_t get_sys_link(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]; @@ -87,13 +89,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; } @@ -481,15 +483,86 @@ err: return -1; } +/* + * 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) +{ + /* + * '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 usec_monotonic(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; }