openat-priv.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Internals for openat-like functions.
  2. Copyright (C) 2005-2006, 2009-2015 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* written by Jim Meyering */
  14. #ifndef _GL_HEADER_OPENAT_PRIV
  15. #define _GL_HEADER_OPENAT_PRIV
  16. #include <errno.h>
  17. #include <limits.h>
  18. #include <stdlib.h>
  19. /* Maximum number of bytes that it is safe to allocate as a single
  20. array on the stack, and that is known as a compile-time constant.
  21. The assumption is that we'll touch the array very quickly, or a
  22. temporary very near the array, provoking an out-of-memory trap. On
  23. some operating systems, there is only one guard page for the stack,
  24. and a page size can be as small as 4096 bytes. Subtract 64 in the
  25. hope that this will let the compiler touch a nearby temporary and
  26. provoke a trap. */
  27. #define SAFER_ALLOCA_MAX (4096 - 64)
  28. #define SAFER_ALLOCA(m) ((m) < SAFER_ALLOCA_MAX ? (m) : SAFER_ALLOCA_MAX)
  29. #if defined PATH_MAX
  30. # define OPENAT_BUFFER_SIZE SAFER_ALLOCA (PATH_MAX)
  31. #elif defined _XOPEN_PATH_MAX
  32. # define OPENAT_BUFFER_SIZE SAFER_ALLOCA (_XOPEN_PATH_MAX)
  33. #else
  34. # define OPENAT_BUFFER_SIZE SAFER_ALLOCA (1024)
  35. #endif
  36. char *openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file);
  37. /* Trying to access a BUILD_PROC_NAME file will fail on systems without
  38. /proc support, and even on systems *with* ProcFS support. Return
  39. nonzero if the failure may be legitimate, e.g., because /proc is not
  40. readable, or the particular .../fd/N directory is not present. */
  41. #define EXPECTED_ERRNO(Errno) \
  42. ((Errno) == ENOTDIR || (Errno) == ENOENT \
  43. || (Errno) == EPERM || (Errno) == EACCES \
  44. || (Errno) == ENOSYS /* Solaris 8 */ \
  45. || (Errno) == EOPNOTSUPP /* FreeBSD */)
  46. /* Wrapper function shared among linkat and renameat. */
  47. int at_func2 (int fd1, char const *file1,
  48. int fd2, char const *file2,
  49. int (*func) (char const *file1, char const *file2));
  50. #endif /* _GL_HEADER_OPENAT_PRIV */