1.\" Copyright (c) 1980, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd May 13, 2024 29.Dt ACCESS 2 30.Os 31.Sh NAME 32.Nm access , 33.Nm eaccess , 34.Nm faccessat 35.Nd check accessibility of a file 36.Sh LIBRARY 37.Lb libc 38.Sh SYNOPSIS 39.In unistd.h 40.Ft int 41.Fn access "const char *path" "int mode" 42.Ft int 43.Fn eaccess "const char *path" "int mode" 44.Ft int 45.Fn faccessat "int fd" "const char *path" "int mode" "int flag" 46.Sh DESCRIPTION 47The 48.Fn access 49and 50.Fn eaccess 51system calls check the accessibility of the 52file named by 53the 54.Fa path 55argument 56for the access permissions indicated by 57the 58.Fa mode 59argument. 60The value of 61.Fa mode 62is either the bitwise-inclusive OR of the access permissions to be 63checked 64.Dv ( R_OK 65for read permission, 66.Dv W_OK 67for write permission, and 68.Dv X_OK 69for execute/search permission), 70or the existence test 71.Pq Dv F_OK . 72.Pp 73For additional information, see the 74.Sx "File Access Permission" 75section of 76.Xr intro 2 . 77.Pp 78The 79.Fn eaccess 80system call uses 81the effective user ID and the group access list 82to authorize the request; 83the 84.Fn access 85system call uses 86the real user ID in place of the effective user ID, 87the real group ID in place of the effective group ID, 88and the rest of the group access list. 89.Pp 90The 91.Fn faccessat 92system call is equivalent to 93.Fn access 94except in the case where 95.Fa path 96specifies a relative path. 97In this case the file whose accessibility is to be determined is 98located relative to the directory associated with the file descriptor 99.Fa fd 100instead of the current working directory. 101If 102.Fn faccessat 103is passed the special value 104.Dv AT_FDCWD 105in the 106.Fa fd 107parameter, the current working directory is used and the behavior is 108identical to a call to 109.Fn access . 110Values for 111.Fa flag 112are constructed by a bitwise-inclusive OR of flags from the following 113list, defined in 114.In fcntl.h : 115.Bl -tag -width indent 116.It Dv AT_EACCESS 117The checks for accessibility are performed using the effective user and group 118IDs instead of the real user and group ID as required in a call to 119.Fn access . 120.It Dv AT_RESOLVE_BENEATH 121Only walk paths below the directory specified by the 122.Ar fd 123descriptor. 124See the description of the 125.Dv O_RESOLVE_BENEATH 126flag in the 127.Xr open 2 128manual page. 129.It Dv AT_EMPTY_PATH 130If the 131.Fa path 132argument is an empty string, operate on the file or directory 133referenced by the descriptor 134.Fa fd . 135If 136.Fa fd 137is equal to 138.Dv AT_FDCWD , 139operate on the current working directory. 140.El 141.Pp 142Even if a process's real or effective user has appropriate privileges 143and indicates success for 144.Dv X_OK , 145the file may not actually have execute permission bits set. 146Likewise for 147.Dv R_OK 148and 149.Dv W_OK . 150.Pp 151.Fn access , 152.Fn eaccess 153and 154.Fn faccessat 155will always dereference symbolic links. 156If the symbolic link itself needs to be referenced, 157.Xr lstat 2 158should be used instead. 159.Sh RETURN VALUES 160.Rv -std 161.Sh ERRORS 162.Fn access , 163.Fn eaccess , 164or 165.Fn faccessat 166will fail if: 167.Bl -tag -width Er 168.It Bq Er EINVAL 169The value of the 170.Fa mode 171argument is invalid. 172.It Bq Er ENOTDIR 173A component of the path prefix is not a directory. 174.It Bq Er ENAMETOOLONG 175A component of a pathname exceeded 255 characters, 176or an entire path name exceeded 1023 characters. 177.It Bq Er ENOENT 178The named file does not exist. 179.It Bq Er ELOOP 180Too many symbolic links were encountered in translating the pathname. 181.It Bq Er EROFS 182Write access is requested for a file on a read-only file system. 183.It Bq Er ETXTBSY 184Write access is requested for a pure procedure (shared text) 185file presently being executed. 186.It Bq Er EACCES 187Permission bits of the file mode do not permit the requested 188access, or search permission is denied on a component of the 189path prefix. 190.It Bq Er EFAULT 191The 192.Fa path 193argument 194points outside the process's allocated address space. 195.It Bq Er EIO 196An I/O error occurred while reading from or writing to the file system. 197.It Bq Er EINTEGRITY 198Corrupted data was detected while reading from the file system. 199.El 200.Pp 201Also, the 202.Fn faccessat 203system call may fail if: 204.Bl -tag -width Er 205.It Bq Er EBADF 206The 207.Fa path 208argument does not specify an absolute path and the 209.Fa fd 210argument is 211neither 212.Dv AT_FDCWD 213nor a valid file descriptor. 214.It Bq Er EINVAL 215The value of the 216.Fa flag 217argument is not valid. 218.It Bq Er ENOTDIR 219The 220.Fa path 221argument is not an absolute path and 222.Fa fd 223is neither 224.Dv AT_FDCWD 225nor a file descriptor associated with a directory. 226.It Bq Er ENOTCAPABLE 227.Fa path 228is an absolute path, 229or contained a ".." component leading to a 230directory outside of the directory hierarchy specified by 231.Fa fd , 232and the process is in capability mode. 233.El 234.Sh SEE ALSO 235.Xr chmod 2 , 236.Xr intro 2 , 237.Xr stat 2 238.Sh STANDARDS 239The 240.Fn access 241system call is expected to conform to 242.St -p1003.1-90 . 243The 244.Fn faccessat 245system call follows The Open Group Extended API Set 2 specification. 246.Sh HISTORY 247The 248.Fn access 249function appeared in 250.At v7 . 251The 252.Fn faccessat 253system call appeared in 254.Fx 8.0 . 255.Sh SECURITY CONSIDERATIONS 256The 257.Fn access 258system call 259is a potential security hole due to race conditions and 260should never be used. 261Set-user-ID and set-group-ID applications should restore the 262effective user or group ID, 263and perform actions directly rather than use 264.Fn access 265to simulate access checks for the real user or group ID. 266The 267.Fn eaccess 268system call 269likewise may be subject to races if used inappropriately. 270.Pp 271.Fn access 272remains useful for providing clues to users as to whether operations 273make sense for particular filesystem objects (e.g. 'delete' menu 274item only highlighted in a writable folder ... avoiding interpretation 275of the st_mode bits that the application might not understand -- 276e.g. in the case of AFS). 277It also allows a cheaper file existence test than 278.Xr stat 2 . 279