From 78de439c4c70b6340dab0865e3697fd9a9c9242e Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Wed, 21 Apr 2021 20:37:05 +0300 Subject: [PATCH] tempd: Use uncached data on retry If data read is retried due to error 85, we must read an uncached value to prevent pulling the same error back from the cache. Signed-off-by: Timo Kokkonen --- tempd.c | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/tempd.c b/tempd.c index 5120b00..56905e2 100644 --- a/tempd.c +++ b/tempd.c @@ -15,12 +15,24 @@ #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) -static int read_1wire_temp(const char *path, double *temp) +static inline char *_strlcat(char *dst, const char *src, size_t len) { + strncat(dst, src, len - strnlen(dst, len) - 1); + dst[len - 1] = '\0'; + + return dst; +} + +static int read_1wire_temp(const char *_path, double *temp) +{ + char uncached[512]; char buf[16]; int fd; int ret; int retries = 0; + const char *path = _path; + char *c; + int len; retry: fd = open(path, O_RDONLY); @@ -44,16 +56,32 @@ retry: out_close: close(fd); - if (*temp == 85) { - ret = -1; - if (retries < 3) { - retries++; - printf("Retrying 1wire read, try %d\n", retries); - goto retry; - } + if (*temp != 85 || retries >= 3) + return ret; + + ret = -1; + retries++; + printf("Retrying 1wire read, try %d\n", retries); + + if (retries > 1) + goto retry; + + /* Make an uncached path first */ + c = strstr(_path, "/28."); + if (!c) + c = strstr(_path, "/10."); + if (!c) { + printf("Unable to make uncached path of \"%s\", will not retry\n", _path); + return -1; } - return ret; + len = c - _path; + strncpy(uncached, _path, len); + uncached[len] = '\0'; + _strlcat(uncached, "/uncached/", sizeof(uncached)); + _strlcat(uncached, c, sizeof(uncached)); + path = uncached; + goto retry; } struct data_entry { -- 2.45.0