xref: /freebsd/lib/libsys/access.2 (revision 5ab6ed93cd3680f8b69dd4d05823f4740a2bdef9)
18269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993
28269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
38269e767SBrooks Davis.\"
48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
68269e767SBrooks Davis.\" are met:
78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
88269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
108269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
118269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
138269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
148269e767SBrooks Davis.\"    without specific prior written permission.
158269e767SBrooks Davis.\"
168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268269e767SBrooks Davis.\" SUCH DAMAGE.
278269e767SBrooks Davis.\"
28*5ab6ed93SFernando Apesteguía.Dd August 11, 2024
298269e767SBrooks Davis.Dt ACCESS 2
308269e767SBrooks Davis.Os
318269e767SBrooks Davis.Sh NAME
328269e767SBrooks Davis.Nm access ,
338269e767SBrooks Davis.Nm eaccess ,
348269e767SBrooks Davis.Nm faccessat
358269e767SBrooks Davis.Nd check accessibility of a file
368269e767SBrooks Davis.Sh LIBRARY
378269e767SBrooks Davis.Lb libc
388269e767SBrooks Davis.Sh SYNOPSIS
398269e767SBrooks Davis.In unistd.h
408269e767SBrooks Davis.Ft int
418269e767SBrooks Davis.Fn access "const char *path" "int mode"
428269e767SBrooks Davis.Ft int
438269e767SBrooks Davis.Fn eaccess "const char *path" "int mode"
448269e767SBrooks Davis.Ft int
458269e767SBrooks Davis.Fn faccessat "int fd" "const char *path" "int mode" "int flag"
468269e767SBrooks Davis.Sh DESCRIPTION
478269e767SBrooks DavisThe
48a4be1eb2SDag-Erling Smørgrav.Fn access ,
498269e767SBrooks Davis.Fn eaccess
50a4be1eb2SDag-Erling Smørgravand
51a4be1eb2SDag-Erling Smørgrav.Fn faccessat
52a4be1eb2SDag-Erling Smørgravsystem calls report whether an attempt to access the file designated
53a4be1eb2SDag-Erling Smørgravby their
548269e767SBrooks Davis.Fa path
55a4be1eb2SDag-Erling Smørgravin the manner described by their
568269e767SBrooks Davis.Fa mode
57a4be1eb2SDag-Erling Smørgravargument is likely to succeed.
588269e767SBrooks DavisThe value of
598269e767SBrooks Davis.Fa mode
60a4be1eb2SDag-Erling Smørgravis either the bitwise-inclusive OR of the desired permissions
61a4be1eb2SDag-Erling Smørgrav.Po
62a4be1eb2SDag-Erling Smørgrav.Dv R_OK
638269e767SBrooks Davisfor read permission,
648269e767SBrooks Davis.Dv W_OK
658269e767SBrooks Davisfor write permission, and
668269e767SBrooks Davis.Dv X_OK
67a4be1eb2SDag-Erling Smørgravfor execute / search permission
68a4be1eb2SDag-Erling Smørgrav.Pc
69a4be1eb2SDag-Erling Smørgravor
70a4be1eb2SDag-Erling Smørgrav.Dv F_OK
71a4be1eb2SDag-Erling Smørgravto simply check whether the file exists.
728269e767SBrooks Davis.Pp
73a4be1eb2SDag-Erling SmørgravFor a number of reasons, these system calls cannot be relied upon to
74a4be1eb2SDag-Erling Smørgravgive a correct and definitive answer.
75a4be1eb2SDag-Erling SmørgravThey can at best provide an early indication of the expected outcome,
76a4be1eb2SDag-Erling Smørgravto be confirmed by actually attempting the operation.
77a4be1eb2SDag-Erling SmørgravFor existence checks, either
78a4be1eb2SDag-Erling Smørgrav.Xr stat 2
79a4be1eb2SDag-Erling Smørgravor
80a4be1eb2SDag-Erling Smørgrav.Xr lstat 2
81a4be1eb2SDag-Erling Smørgravshould be used instead.
82a4be1eb2SDag-Erling SmørgravSee also
83a4be1eb2SDag-Erling Smørgrav.Sx SECURITY CONSIDERATIONS
84a4be1eb2SDag-Erling Smørgravbelow.
858269e767SBrooks Davis.Pp
868269e767SBrooks DavisThe
878269e767SBrooks Davis.Fn eaccess
888269e767SBrooks Davissystem call uses
898269e767SBrooks Davisthe effective user ID and the group access list
908269e767SBrooks Davisto authorize the request;
918269e767SBrooks Davisthe
928269e767SBrooks Davis.Fn access
938269e767SBrooks Davissystem call uses
948269e767SBrooks Davisthe real user ID in place of the effective user ID,
958269e767SBrooks Davisthe real group ID in place of the effective group ID,
968269e767SBrooks Davisand the rest of the group access list.
978269e767SBrooks Davis.Pp
98a4be1eb2SDag-Erling SmørgravSee the
99a4be1eb2SDag-Erling Smørgrav.Sx DEFINITIONS
100a4be1eb2SDag-Erling Smørgravsection of
101a4be1eb2SDag-Erling Smørgrav.Xr intro 2
102a4be1eb2SDag-Erling Smørgravfor additional information on file access permissions and real
103a4be1eb2SDag-Erling Smørgravvs. effective user and group IDs.
104a4be1eb2SDag-Erling Smørgrav.Pp
1058269e767SBrooks DavisThe
1068269e767SBrooks Davis.Fn faccessat
1078269e767SBrooks Davissystem call is equivalent to
1088269e767SBrooks Davis.Fn access
1098269e767SBrooks Davisexcept in the case where
1108269e767SBrooks Davis.Fa path
1118269e767SBrooks Davisspecifies a relative path.
1128269e767SBrooks DavisIn this case the file whose accessibility is to be determined is
1138269e767SBrooks Davislocated relative to the directory associated with the file descriptor
1148269e767SBrooks Davis.Fa fd
1158269e767SBrooks Davisinstead of the current working directory.
1168269e767SBrooks DavisIf
1178269e767SBrooks Davis.Fn faccessat
1188269e767SBrooks Davisis passed the special value
1198269e767SBrooks Davis.Dv AT_FDCWD
1208269e767SBrooks Davisin the
1218269e767SBrooks Davis.Fa fd
1228269e767SBrooks Davisparameter, the current working directory is used and the behavior is
1238269e767SBrooks Davisidentical to a call to
1248269e767SBrooks Davis.Fn access .
1258269e767SBrooks DavisValues for
1268269e767SBrooks Davis.Fa flag
1278269e767SBrooks Davisare constructed by a bitwise-inclusive OR of flags from the following
1288269e767SBrooks Davislist, defined in
1298269e767SBrooks Davis.In fcntl.h :
1308269e767SBrooks Davis.Bl -tag -width indent
1318269e767SBrooks Davis.It Dv AT_EACCESS
132a4be1eb2SDag-Erling SmørgravThe checks are performed using the effective user and group IDs,
133a4be1eb2SDag-Erling Smørgravlike
134a4be1eb2SDag-Erling Smørgrav.Fn eaccess ,
135a4be1eb2SDag-Erling Smørgravinstead of the real user and group ID, like
1368269e767SBrooks Davis.Fn access .
1378269e767SBrooks Davis.It Dv AT_RESOLVE_BENEATH
1388269e767SBrooks DavisOnly walk paths below the directory specified by the
1398269e767SBrooks Davis.Ar fd
1408269e767SBrooks Davisdescriptor.
1418269e767SBrooks DavisSee the description of the
1428269e767SBrooks Davis.Dv O_RESOLVE_BENEATH
1438269e767SBrooks Davisflag in the
1448269e767SBrooks Davis.Xr open 2
1458269e767SBrooks Davismanual page.
1468269e767SBrooks Davis.It Dv AT_EMPTY_PATH
1478269e767SBrooks DavisIf the
1488269e767SBrooks Davis.Fa path
1498269e767SBrooks Davisargument is an empty string, operate on the file or directory
1508269e767SBrooks Davisreferenced by the descriptor
1518269e767SBrooks Davis.Fa fd .
1528269e767SBrooks DavisIf
1538269e767SBrooks Davis.Fa fd
1548269e767SBrooks Davisis equal to
1558269e767SBrooks Davis.Dv AT_FDCWD ,
1568269e767SBrooks Davisoperate on the current working directory.
157*5ab6ed93SFernando Apesteguía.It Dv AT_SYMLINK_NOFOLLOW
158*5ab6ed93SFernando ApesteguíaIf
159*5ab6ed93SFernando Apesteguía.Fa path
160*5ab6ed93SFernando Apesteguíanames a symbolic link, access of the symbolic link is evaluated.
1618269e767SBrooks Davis.El
1628269e767SBrooks Davis.Pp
1638269e767SBrooks DavisEven if a process's real or effective user has appropriate privileges
1648269e767SBrooks Davisand indicates success for
1658269e767SBrooks Davis.Dv X_OK ,
1668269e767SBrooks Davisthe file may not actually have execute permission bits set.
1678269e767SBrooks DavisLikewise for
1688269e767SBrooks Davis.Dv R_OK
1698269e767SBrooks Davisand
1708269e767SBrooks Davis.Dv W_OK .
1718269e767SBrooks Davis.Sh RETURN VALUES
1728269e767SBrooks Davis.Rv -std
1738269e767SBrooks Davis.Sh ERRORS
174a4be1eb2SDag-Erling SmørgravThe
1758269e767SBrooks Davis.Fn access ,
1768269e767SBrooks Davis.Fn eaccess ,
177a4be1eb2SDag-Erling Smørgravand
1788269e767SBrooks Davis.Fn faccessat
179a4be1eb2SDag-Erling Smørgravsystem calls may fail if:
1808269e767SBrooks Davis.Bl -tag -width Er
1818269e767SBrooks Davis.It Bq Er EINVAL
1828269e767SBrooks DavisThe value of the
1838269e767SBrooks Davis.Fa mode
1848269e767SBrooks Davisargument is invalid.
1858269e767SBrooks Davis.It Bq Er ENOTDIR
1868269e767SBrooks DavisA component of the path prefix is not a directory.
1878269e767SBrooks Davis.It Bq Er ENAMETOOLONG
1888269e767SBrooks DavisA component of a pathname exceeded 255 characters,
1898269e767SBrooks Davisor an entire path name exceeded 1023 characters.
1908269e767SBrooks Davis.It Bq Er ENOENT
1918269e767SBrooks DavisThe named file does not exist.
1928269e767SBrooks Davis.It Bq Er ELOOP
1938269e767SBrooks DavisToo many symbolic links were encountered in translating the pathname.
1948269e767SBrooks Davis.It Bq Er EROFS
1958269e767SBrooks DavisWrite access is requested for a file on a read-only file system.
1968269e767SBrooks Davis.It Bq Er ETXTBSY
1978269e767SBrooks DavisWrite access is requested for a pure procedure (shared text)
1988269e767SBrooks Davisfile presently being executed.
1998269e767SBrooks Davis.It Bq Er EACCES
2008269e767SBrooks DavisPermission bits of the file mode do not permit the requested
2018269e767SBrooks Davisaccess, or search permission is denied on a component of the
2028269e767SBrooks Davispath prefix.
2038269e767SBrooks Davis.It Bq Er EFAULT
2048269e767SBrooks DavisThe
2058269e767SBrooks Davis.Fa path
2068269e767SBrooks Davisargument
2078269e767SBrooks Davispoints outside the process's allocated address space.
2088269e767SBrooks Davis.It Bq Er EIO
2098269e767SBrooks DavisAn I/O error occurred while reading from or writing to the file system.
2108269e767SBrooks Davis.It Bq Er EINTEGRITY
2118269e767SBrooks DavisCorrupted data was detected while reading from the file system.
2128269e767SBrooks Davis.El
2138269e767SBrooks Davis.Pp
2148269e767SBrooks DavisAlso, the
2158269e767SBrooks Davis.Fn faccessat
2168269e767SBrooks Davissystem call may fail if:
2178269e767SBrooks Davis.Bl -tag -width Er
2188269e767SBrooks Davis.It Bq Er EBADF
2198269e767SBrooks DavisThe
2208269e767SBrooks Davis.Fa path
2218269e767SBrooks Davisargument does not specify an absolute path and the
2228269e767SBrooks Davis.Fa fd
2238269e767SBrooks Davisargument is
2248269e767SBrooks Davisneither
2258269e767SBrooks Davis.Dv AT_FDCWD
2268269e767SBrooks Davisnor a valid file descriptor.
2278269e767SBrooks Davis.It Bq Er EINVAL
2288269e767SBrooks DavisThe value of the
2298269e767SBrooks Davis.Fa flag
2308269e767SBrooks Davisargument is not valid.
2318269e767SBrooks Davis.It Bq Er ENOTDIR
2328269e767SBrooks DavisThe
2338269e767SBrooks Davis.Fa path
2348269e767SBrooks Davisargument is not an absolute path and
2358269e767SBrooks Davis.Fa fd
2368269e767SBrooks Davisis neither
2378269e767SBrooks Davis.Dv AT_FDCWD
2388269e767SBrooks Davisnor a file descriptor associated with a directory.
2398269e767SBrooks Davis.It Bq Er ENOTCAPABLE
2408269e767SBrooks Davis.Fa path
2418269e767SBrooks Davisis an absolute path,
2428269e767SBrooks Davisor contained a ".." component leading to a
2438269e767SBrooks Davisdirectory outside of the directory hierarchy specified by
2448269e767SBrooks Davis.Fa fd ,
2458269e767SBrooks Davisand the process is in capability mode.
2468269e767SBrooks Davis.El
2478269e767SBrooks Davis.Sh SEE ALSO
2488269e767SBrooks Davis.Xr chmod 2 ,
2498269e767SBrooks Davis.Xr intro 2 ,
2508269e767SBrooks Davis.Xr stat 2
2518269e767SBrooks Davis.Sh STANDARDS
2528269e767SBrooks DavisThe
2538269e767SBrooks Davis.Fn access
2548269e767SBrooks Davissystem call is expected to conform to
2558269e767SBrooks Davis.St -p1003.1-90 .
2568269e767SBrooks DavisThe
2578269e767SBrooks Davis.Fn faccessat
2588269e767SBrooks Davissystem call follows The Open Group Extended API Set 2 specification.
2598269e767SBrooks Davis.Sh HISTORY
2608269e767SBrooks DavisThe
2618269e767SBrooks Davis.Fn access
2628269e767SBrooks Davisfunction appeared in
2638269e767SBrooks Davis.At v7 .
2648269e767SBrooks DavisThe
2658269e767SBrooks Davis.Fn faccessat
2668269e767SBrooks Davissystem call appeared in
2678269e767SBrooks Davis.Fx 8.0 .
2688269e767SBrooks Davis.Sh SECURITY CONSIDERATIONS
2698269e767SBrooks DavisThe
270a4be1eb2SDag-Erling Smørgrav.Fn access ,
271a4be1eb2SDag-Erling Smørgrav.Fn eaccess ,
272a4be1eb2SDag-Erling Smørgravand
273a4be1eb2SDag-Erling Smørgrav.Fn faccessat
274a4be1eb2SDag-Erling Smørgravsystem calls are subject to time-of-check-to-time-of-use races and
275a4be1eb2SDag-Erling Smørgravshould not be relied upon for file permission enforcement purposes.
276a4be1eb2SDag-Erling SmørgravInstead, applications should perform the desired action using the
277a4be1eb2SDag-Erling Smørgravrequesting user's credentials.
278