ACCESS(3POSIX)
π¦ ACCESS(3POSIX) π¦
okay bestie, so this one lives in the POSIX Programmer's Manual, and it's giving very official. π
Heads up fr fr: the Linux implementation of this interface may differ (so go check the corresponding Linux manual page for the real tea), or honestly the interface might just not be implemented on Linux at all. π
β¨ NAME β¨
access, faccessat β determine accessibility of a file descriptor π
β¨ SYNOPSIS β¨
#include <unistd.h>
int access(const char *path, int amode);
#include <fcntl.h>
int faccessat(int fd, const char *path, int amode, int flag);
β¨ DESCRIPTION β¨
So access() shall check the file named by the pathname pointed to by the path argument, for accessibility according to the bit pattern contained in amode. πΈ
And here's the iconic part: the checks for accessibility (including directory permissions checked during pathname resolution) shall be performed using the real user ID in place of the effective user ID, and the real group ID in place of the effective group ID. Not me pointing out that this is the whole plot twist of the page. π
The value of amode is either the bitwise-inclusive OR of the access permissions to be checked (R_OK, W_OK, X_OK) or the existence test (F_OK). β¨
If any access permissions are checked, each shall be checked individually, as described in the Base Definitions volume of POSIX.1-2017, Section 4.5, File Access Permissions β except that where that description refers to execute permission for a process with appropriate privileges, an implementation may indicate success for X_OK even if execute permission is not granted to any user. (iykyk π)
The faccessat() function, when called with a flag value of zero, shall be equivalent to the access() function β except in the case where path specifies a relative path. In that case the file whose accessibility is to be determined shall be located relative to the directory associated with the file descriptor fd instead of the current working directory. π
If the access mode of the open file description associated with the file descriptor is not O_SEARCH, the function shall check whether directory searches are permitted using the current permissions of the directory underlying the file descriptor. If the access mode is O_SEARCH, the function shall not perform the check. (Slay, no check needed.)
If faccessat() is passed the special value AT_FDCWD in the fd parameter, the current working directory shall be used and, if flag is zero, the behavior shall be identical to a call to access(). π
Values for flag are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:
AT_EACCESSβ The checks for accessibility (including directory permissions checked during pathname resolution) shall be performed using the effective user ID and group ID instead of the real user ID and group ID as required in a call toaccess(). π«Ά
β¨ RETURN VALUE β¨
Upon successful completion, these functions shall return 0. Otherwise, these functions shall return -1 and set errno to indicate the error. π
β¨ ERRORS β¨
These functions shall fail if:
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. πELOOPβ A loop exists in symbolic links encountered during resolution of thepathargument. πENAMETOOLONGβ The length of a component of a pathname is longer than{NAME_MAX}. πENOENTβ A component ofpathdoes not name an existing file orpathis an empty string. π«₯ENOTDIRβ A component of the path prefix names an existing file that is neither a directory nor a symbolic link to a directory, or thepathargument contains at least one non-<slash>character and ends with one or more trailing<slash>characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory. π«EROFSβ Write access is requested for a file on a read-only file system. π§
The faccessat() function shall fail if:
EACCESβ The access mode of the open file description associated withfdis notO_SEARCHand the permissions of the directory underlyingfddo not permit directory searches. πEBADFβ Thepathargument does not specify an absolute path and thefdargument is neitherAT_FDCWDnor a valid file descriptor open for reading or searching. π΅ENOTDIRβ Thepathargument is not an absolute path andfdis a file descriptor associated with a non-directory file. π
These functions may fail if:
EINVALβ The value of theamodeargument is invalid. βELOOPβ More than{SYMLOOP_MAX}symbolic links were encountered during resolution of thepathargument. πENAMETOOLONGβ The length of a pathname exceeds{PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds{PATH_MAX}. πETXTBSYβ Write access is requested for a pure procedure (shared text) file that is being executed. π₯
The faccessat() function may fail if:
EINVALβ The value of theflagargument is not valid. β οΈ
The following sections are informative. π·οΈ
β¨ EXAMPLES β¨
Testing for the Existence of a File π
The following example tests whether a file named myfile exists in the /tmp directory.
#include <unistd.h>
...
int result;
const char *pathname = "/tmp/myfile";
result = access (pathname, F_OK);
β¨ APPLICATION USAGE β¨
ngl bestie, use of these functions is discouraged π¬ β since by the time the returned information is acted upon, it is out-of-date. (That is, acting upon the information always leads to a time-of-check-to-time-of-use race condition. Yes, the classic TOCTOU jumpscare.) π«
An application should instead attempt the action itself and handle the [EACCES] error that occurs if the file is not accessible (with a change of effective user and group IDs beforehand, and perhaps a change back afterwards, in the case where access() or faccessat() without AT_EACCES would have been used). π
Historically, one of the uses of access() was in set-user-ID root programs to check whether the user running the program had access to a file. This relied on "super-user" privileges which were granted based on the effective user ID being zero, so that when access() used the real user ID to check accessibility those privileges were not taken into account. π
On newer systems where privileges can be assigned which have no association with user or group IDs, if a program with such privileges calls access(), the change of IDs has no effect on the privileges and therefore they are taken into account in the accessibility checks. Thus, access() (and faccessat() with flag zero) cannot be used for this historical purpose in such programs. Likewise, if a system provides any additional or alternate file access control mechanisms that are not user ID-based, they will still be taken into account. π
If a relative pathname is used, no account is taken of whether the current directory (or the directory associated with the file descriptor fd) is accessible via any absolute pathname. Applications using access(), or faccessat() without AT_EACCES, may consequently act as if the file would be accessible to a user with the real user ID and group ID of the process β when such a user would not in practice be able to access the file, because access would be denied at some point above the current directory (or the directory associated with the file descriptor fd) in the file hierarchy. π
If access() or faccessat() is used with W_OK to check for write access to a directory which has the S_ISVTX bit set, a return value indicating the directory is writable can be misleading, since some operations on files in the directory would not be permitted based on the ownership of those files (see the Base Definitions volume of POSIX.1-2017, Section 4.3, Directory Protection). π©
Additional values of amode other than the set defined in the description may be valid; for example, if a system has extended access controls. β¨
The use of the AT_EACCESS value for flag enables functionality not available in access(). π
β¨ RATIONALE β¨
In early proposals, some inadequacies in the access() function led to the creation of an eaccess() function because:
- Historical implementations of
access()do not test file access correctly when the process' real user ID is superuser. In particular, they always return zero when testing execute permissions without regard to whether the file is executable. π΅ - The superuser has complete access to all files on a system. As a consequence, programs started by the superuser and switched to the effective user ID with lesser privileges cannot use
access()to test their file access permissions. π
However, the historical model of eaccess() does not resolve problem (1), so this volume of POSIX.1-2017 now allows access() to behave in the desired way because several implementations have corrected the problem. π οΈ
It was also argued that problem (2) is more easily solved by using open(), chdir(), or one of the exec functions as appropriate and responding to the error, rather than creating a new function that would not be as reliable. Therefore, eaccess() is not included in this volume of POSIX.1-2017. periodt. π
The sentence concerning appropriate privileges and execute permission bits reflects the two possibilities implemented by historical implementations when checking superuser access for X_OK. π
New implementations are discouraged from returning X_OK unless at least one execution permission bit is set. π
The purpose of the faccessat() function is to enable the checking of the accessibility of files in directories other than the current working directory without exposure to race conditions. Any part of the path of a file could be changed in parallel to a call to access(), resulting in unspecified behavior. By opening a file descriptor for the target directory and using the faccessat() function it can be guaranteed that the file tested for accessibility is located relative to the desired directory. π―
β¨ FUTURE DIRECTIONS β¨
These functions may be formally deprecated (for example, by shading them OB) in a future version of this standard. π
β¨ SEE ALSO β¨
chmod(), fstatat()
The Base Definitions volume of POSIX.1-2017, Section 4.5, File Access Permissions, <fcntl.h>, <unistd.h> π
π COPYRIGHT π
okay bestie, legal era incoming, but it's actually kinda important so stay with me π«Ά
So like, chunks of this whole text got reprinted and reproduced in electronic form, straight from IEEE Std 1003.1-2017, which is the Standard for Information Technology -- Portable Operating System Interface (POSIX), aka The Open Group Base Specifications Issue 7, 2018 Edition. And the copyright on that? Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. Both of them. Together. Iconic co-ownership tbh π
Now, if there's ever any discrepancy between this version and the original IEEE and The Open Group Standard β like, if they don't match up β the original IEEE and The Open Group Standard is the referee document. It's the final boss. It decides. No cap. π
And you can literally get the original Standard online at http://www.opengroup.org/unix/online.html β go get it, it's giving primary source energy β¨
One more thing, and this is the relatable part π: any typographical or formatting errors that show up in this page are most likely to have been introduced during the conversion of the source files to man page format. So like, not the original's fault, it's a glow-up-gone-wrong situation during the conversion process. If you wanna report those errors, head to https://www.kernel.org/doc/man-pages/reporting_bugs.html and tell them π
and that's the whole page, bestie!! π we made it, we ate, we left no crumbs π this was ACCESS(3POSIX) from IEEE/The Open Group, 2017 β like, the source and version and date all in one little footer moment, so cute of them π go forth and check your file access permissions responsibly, mwah ππ¦
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms