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