1 /* Check the access rights of a file relative to an open directory. 2 Copyright (C) 2009-2021 Free Software Foundation, Inc. 3 4 This program is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; either version 3 of the License, or 7 (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 16 17 /* written by Eric Blake */ 18 19 /* If the user's config.h happens to include <unistd.h>, let it include only 20 the system's <unistd.h> here, so that orig_faccessat doesn't recurse to 21 rpl_faccessat. */ 22 #define _GL_INCLUDING_UNISTD_H 23 #include <config.h> 24 25 /* Specification. */ 26 #include <unistd.h> 27 28 #include <errno.h> 29 #include <fcntl.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <sys/stat.h> 33 #undef _GL_INCLUDING_UNISTD_H 34 35 #if HAVE_FACCESSAT 36 static int 37 orig_faccessat (int fd, char const *name, int mode, int flag) /* */ 38 { 39 return faccessat (fd, name, mode, flag); 40 } 41 #endif 42 43 /* Write "unistd.h" here, not <unistd.h>, otherwise OSF/1 5.1 DTK cc 44 eliminates this include because of the preliminary #include <unistd.h> 45 above. */ 46 #include "unistd.h" 47 48 #ifndef HAVE_ACCESS 49 /* Mingw lacks access, but it also lacks real vs. effective ids, so 50 the gnulib euidaccess module is good enough. */ 51 # undef access 52 # define access euidaccess 53 #endif 54 55 #if HAVE_FACCESSAT 56 57 int 58 rpl_faccessat (int fd, char const *file, int mode, int flag) /* */ 59 { 60 int result = orig_faccessat (fd, file, mode, flag); 61 62 if (result == 0 && file[strlen (file) - 1] == '/') 63 { 64 struct stat st; 65 result = fstatat (fd, file, &st, 0); 66 if (result == 0 && !S_ISDIR (st.st_mode)) 67 { 68 errno = ENOTDIR; 69 return -1; 70 } 71 } 72 73 return result; 74 } 75 76 #else /* !HAVE_FACCESSAT */ 77 78 /* Invoke access or euidaccess on file, FILE, using mode MODE, in the directory 79 open on descriptor FD. If possible, do it without changing the 80 working directory. Otherwise, resort to using save_cwd/fchdir, then 81 (access|euidaccess)/restore_cwd. If either the save_cwd or the 82 restore_cwd fails, then give a diagnostic and exit nonzero. 83 Note that this implementation only supports AT_EACCESS, although some 84 native versions also support AT_SYMLINK_NOFOLLOW. */ 85 86 # define AT_FUNC_NAME faccessat 87 # define AT_FUNC_F1 euidaccess 88 # define AT_FUNC_F2 access 89 # define AT_FUNC_USE_F1_COND AT_EACCESS 90 # define AT_FUNC_POST_FILE_PARAM_DECLS , int mode, int flag 91 # define AT_FUNC_POST_FILE_ARGS , mode 92 # include "at-func.c" 93 94 #endif