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