libcachesave-105.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* BEGIN LICENSE
  2. * Copyright (C) 2015 Fedja Beader <fedja@protonmail.ch>
  3. * Copyright (C) 2008-2014 Stewart Smith <stewart@flamingspork.com>
  4. * This program is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 3, as published
  6. * by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranties of
  10. * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
  11. * PURPOSE. See the GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program. If not, see <http://www.gnu.org/licenses/>.
  15. * END LICENSE */
  16. #include "config.h"
  17. #include "libcachesave/visibility.h"
  18. #undef _FILE_OFFSET_BITS // Hack to get open and open64 on 32bit
  19. #undef __USE_FILE_OFFSET64
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <errno.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <dlfcn.h>
  26. #include <stdarg.h>
  27. #include <pthread.h>
  28. #include <stdio.h>
  29. typedef ssize_t (*libc_read_t)(int, void*, size_t);
  30. static libc_read_t libc_read= NULL;
  31. #define ASSIGN_DLSYM_OR_DIE(name) \
  32. libc_##name = (libc_##name##_##t)(intptr_t)dlsym(RTLD_NEXT, #name); \
  33. if (!libc_##name || dlerror()) \
  34. _exit(1);
  35. #define ASSIGN_DLSYM_IF_EXIST(name) \
  36. libc_##name = (libc_##name##_##t)(intptr_t)dlsym(RTLD_NEXT, #name); \
  37. dlerror();
  38. void __attribute__ ((constructor)) cachesave_init(void);
  39. void __attribute__ ((constructor)) cachesave_init(void)
  40. {
  41. ASSIGN_DLSYM_OR_DIE(read);
  42. }
  43. ssize_t LIBEATMYDATA_API read(int fd, void *buf, size_t count)
  44. {
  45. posix_fadvise (fd, 0, 0, POSIX_FADV_DONTNEED);
  46. posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
  47. return (*libc_read)(fd, buf, count);
  48. }