Repository: redis/redis

Issue:
[BUG] Wrong error logged when `reclaimFilePageCache` fails
**Describe the bug**

`reclaimFilePageCache` doesn't set `errno`. As a consequence the logged error will be whatever `errno` was set to prior to the call.

**To reproduce**

1. Set `errno = EAGAIN`.
2. Make sure `rdbSaveInternal` is called with arguments that makes `reclaimFilePageCache` fail.
3. Watch the log. It will say: `Unable to reclaim cache after saving RDB: Resource temporarily unavailable` which is not the reason why the internal `posix_fadvise` failed.

**Expected behavior**

I'm expecting the `serverLog(LL_NOTICE,"Unable to reclaim cache after saving RDB: %s", strerror(errno));` line to print the real cause for the failure.

**Additional information**

`posix_*` functions don't set `errno` but return the error instead. If memory serves me right, it's because these functions came into existence before C11 (where `errno` was made `_Thread_local`) so setting and checking `errno` from different threads wasn't thread safe.



Discussion:

Patch:
diff --git a/src/util.c b/src/util.c
index f8c3321a1d3..98cd9ddf640 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1176,7 +1176,10 @@ int fsyncFileDir(const char *filename) {
 int reclaimFilePageCache(int fd, size_t offset, size_t length) {
 #ifdef HAVE_FADVISE
     int ret = posix_fadvise(fd, offset, length, POSIX_FADV_DONTNEED);
-    if (ret) return -1;
+    if (ret) {
+        errno = ret;
+        return -1;
+    }
     return 0;
 #else
     UNUSED(fd);