ACCESS(2)
๐ ACCESS(2) โ okay so, checking if a file will let you in โจ
bestie welcome to the <syscall> where we vibe check a file before we commit ๐ this one lives in the System Calls Manual, and it's genuinely one of the most "wait, is this thing even telling me the truth" pages in the whole manual. buckle up ๐
๐ฆ NAME ๐ฆ
access, eaccess, faccessat โ check accessibility of a file ๐
๐ธ LIBRARY ๐ธ
Standard C Library (libc, -lc) โ she's a libc girlie, no cap.
โจ SYNOPSIS โจ
#include <unistd.h>
int
access(const char *path, int mode);
int
eaccess(const char *path, int mode);
int
faccessat(int fd, const char *path, int mode, int flag);
like, three whole entry points, all doing the same "can I get in tho" energy ๐
๐ซถ DESCRIPTION ๐ซถ
So the access(), eaccess() and faccessat() system calls report whether an attempt to access the file designated by their path in the manner described by their mode argument is likely to succeed. Note the word likely, because this is not a promise, it's a vibe ๐ฎ
The value of mode is either:
- the bitwise-inclusive OR of the desired permissions โ
R_OKfor read permission,W_OKfor write permission, andX_OKfor execute / search permission - or
F_OKto simply check whether the file exists
<img>literally just "does this exist" energy, she's so real for that.
Now here's the part where I need you to listen, bestie, because this is the iconique caveat of the whole page ๐ญ For a number of reasons, these system calls cannot be relied upon to give a correct and definitive answer. They can at best provide an early indication of the expected outcome, to be confirmed by actually attempting the operation. That's it. That's the whole deal.
For existence checks, eitherstat(2)orlstat(2)should be used instead. See also "SECURITY CONSIDERATIONS" below. โ ๏ธ
๐ง the access() vs eaccess() of it all ๐ง
The eaccess() system call uses the effective user ID and the group access list to authorize the request ๐
The access() system call uses the real user ID in place of the effective user ID, the real group ID in place of the effective group ID, and the rest of the group access list.
Like... they're siblings, but one of them is using her real ID and the other is using her effective ID and if you mix them up you're gonna have a bad time ๐
See the "DEFINITIONS" section of intro(2) for additional information on file access permissions and real vs. effective user and group IDs โ genuinely worth the read, iykyk.
๐ faccessat() and the relative path situation ๐
The faccessat() system call is equivalent to access() except in the case where path specifies a relative path. In this case the file whose accessibility is to be determined is located relative to the directory associated with the file descriptor fd instead of the current working directory. main character energy: she doesn't do "current working directory", she does "this directory, actually" ๐
If faccessat() is passed the special value AT_FDCWD in the fd parameter, the current working directory is used and the behavior is identical to a call to access() โ so, the classic behaviour, no notes ๐
Values for flag are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:
โญ the flag list โญ
AT_EACCESSโ The checks are performed using the effective user and group IDs, likeeaccess(), instead of the real user and group ID, likeaccess(). so it's the effective-ID girlie version of the check ๏ฟฝAT_RESOLVE_BENEATHโ Only walk paths below the directory specified by thefddescriptor. See the description of theO_RESOLVE_BENEATHflag in theopen(2)manual page. no escaping the enclosure, she stays in her lane ๐งAT_EMPTY_PATHโ If thepathargument is an empty string, operate on the file or directory referenced by the descriptorfd. Iffdis equal toAT_FDCWD, operate on the current working directory. empty string? we're going off the fd then, periodt ๐AT_SYMLINK_NOFOLLOWโ Ifpathnames a symbolic link, access of the symbolic link is evaluated. we're judging the link itself, not where she points, and honestly? iconic ๐ฆ
And one last thing before we move on, bestie, because this is the one that always trips people up ๐ญ
Even if a process's real or effective user has appropriate privileges and indicates success for X_OK, the file may not actually have execute permission bits set. Likewise for R_OK and W_OK. so like... the check says yes and the file says no and there's nothing you can do about it but respect her honesty ๐ซถ
๐ RETURN VALUES ๐
Upon successful completion, the value 0 is returned ๐
Otherwise the value -1 is returned and the global variable errno is set to indicate the error. sad girl hours, check your errno ๐
๐ฅ ERRORS ๐ฅ
The access(), eaccess(), and faccessat() system calls may fail if:
[EINVAL]โ The value of themodeargument is invalid. you gave her a mode she doesn't recognize, be so fr ๐[ENOTDIR]โ A component of the path prefix is not a directory. that's a file in your path bestie, it's giving confused ๐[ENAMETOOLONG]โ A component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters. girl that path is long ๐ญ[ENOENT]โ The named file does not exist. she's not there. she never was. rip ๐ชฆ[ELOOP]โ Too many symbolic links were encountered in translating the pathname. the symlinks are in a loop and they're not getting out ๐[EROFS]โ Write access is requested for a file on a read-only file system. read-only means read-only, she said what she said ๐ซ[ETXTBSY]โ Write access is requested for a pure procedure (shared text) file presently being executed. you want to write to a file that's literally running right now?? no ma'am ๐ค[EACCES]โ Permission bits of the file mode do not permit the requested access, or search permission is denied on a component of the path prefix. the classic "no" ๐[EFAULT]โ Thepathargument points outside the process's allocated address space. she's pointing into the void, that's not even your memory ๐ซ[EIO]โ An I/O error occurred while reading from or writing to the file system. the disk is having a moment ๐[EINTEGRITY]โ Corrupted data was detected while reading from the file system. something is wrong in there, do not trust it โ ๏ธ
๐ and also, faccessat() may fail if ๐
[EBADF]โ Thepathargument does not specify an absolute path and thefdargument is neitherAT_FDCWDnor a valid file descriptor. you forgot to give her a real fd, bestie ๐ญ[EINVAL]โ The value of theflagargument is not valid. those flags aren't flags she knows ๐[ENOTDIR]โ Thepathargument is not an absolute path andfdis neitherAT_FDCWDnor a file descriptor associated with a directory. that fd isn't a directory fd, that's the whole problem ๐ซ[ENOTCAPABLE]โpathis an absolute path, or contained a".."component leading to a directory outside of the directory hierarchy specified byfd, and the process is in capability mode. you tried to sneak out with a".."and the capability mode said absolutely not ๐ง๐ฅ
๐ SEE ALSO ๐
chmod(2), intro(2), stat(2) โ go read these too, they're the supporting cast and they ate ๐
โญ STANDARDS โญ
The access() system call is expected to conform to ISO/IEC 9945-1:1990 ("POSIX.1") ๐
The faccessat() system call follows The Open Group Extended API Set 2 specification ๐
๐ฆ HISTORY ๐ฆ
The access() function appeared in Version 7 AT&T UNIX โ she's an elder, she's been doing this since forever, respect her ๐ซถ
The faccessat() system call appeared in FreeBSD 8.0 โ the newer sibling, still iconic โจ
๐ SECURITY CONSIDERATIONS ๐
okay bestie this is the serious one, pay attention โ ๏ธ
The access(), eaccess(), and faccessat() system calls are subject to time-of-check-to-time-of-use races and should not be relied upon for file permission enforcement purposes. like genuinely, do not build your security on this, it will fold ๐ญ
Instead, applications should perform the desired action using the requesting user's credentials โ that's the move, that's the whole thing ๐ซถ
fr fr: check-then-use is a race condition and races are how you get got. don't do it ๐
and that's the page!! she's from Debian, dated August 11, 2024, and she's filed under ACCESS(2) in the System Calls Manual โ go check those permissions responsibly, bestie, ily, bye ๐๐โจ
๐ฆ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms