xref: /freebsd/lib/libsys/access.2 (revision a4be1eb21165d7aedae9dc6634528619ff10d025)
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*a4be1eb2SDag-Erling Smørgrav.Dd May 21, 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
48*a4be1eb2SDag-Erling Smørgrav.Fn access ,
498269e767SBrooks Davis.Fn eaccess
50*a4be1eb2SDag-Erling Smørgravand
51*a4be1eb2SDag-Erling Smørgrav.Fn faccessat
52*a4be1eb2SDag-Erling Smørgravsystem calls report whether an attempt to access the file designated
53*a4be1eb2SDag-Erling Smørgravby their
548269e767SBrooks Davis.Fa path
55*a4be1eb2SDag-Erling Smørgravin the manner described by their
568269e767SBrooks Davis.Fa mode
57*a4be1eb2SDag-Erling Smørgravargument is likely to succeed.
588269e767SBrooks DavisThe value of
598269e767SBrooks Davis.Fa mode
60*a4be1eb2SDag-Erling Smørgravis either the bitwise-inclusive OR of the desired permissions
61*a4be1eb2SDag-Erling Smørgrav.Po
62*a4be1eb2SDag-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
67*a4be1eb2SDag-Erling Smørgravfor execute / search permission
68*a4be1eb2SDag-Erling Smørgrav.Pc
69*a4be1eb2SDag-Erling Smørgravor
70*a4be1eb2SDag-Erling Smørgrav.Dv F_OK
71*a4be1eb2SDag-Erling Smørgravto simply check whether the file exists.
728269e767SBrooks Davis.Pp
73*a4be1eb2SDag-Erling SmørgravFor a number of reasons, these system calls cannot be relied upon to
74*a4be1eb2SDag-Erling Smørgravgive a correct and definitive answer.
75*a4be1eb2SDag-Erling SmørgravThey can at best provide an early indication of the expected outcome,
76*a4be1eb2SDag-Erling Smørgravto be confirmed by actually attempting the operation.
77*a4be1eb2SDag-Erling SmørgravFor existence checks, either
78*a4be1eb2SDag-Erling Smørgrav.Xr stat 2
79*a4be1eb2SDag-Erling Smørgravor
80*a4be1eb2SDag-Erling Smørgrav.Xr lstat 2
81*a4be1eb2SDag-Erling Smørgravshould be used instead.
82*a4be1eb2SDag-Erling SmørgravSee also
83*a4be1eb2SDag-Erling Smørgrav.Sx SECURITY CONSIDERATIONS
84*a4be1eb2SDag-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
98*a4be1eb2SDag-Erling SmørgravSee the
99*a4be1eb2SDag-Erling Smørgrav.Sx DEFINITIONS
100*a4be1eb2SDag-Erling Smørgravsection of
101*a4be1eb2SDag-Erling Smørgrav.Xr intro 2
102*a4be1eb2SDag-Erling Smørgravfor additional information on file access permissions and real
103*a4be1eb2SDag-Erling Smørgravvs. effective user and group IDs.
104*a4be1eb2SDag-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
132*a4be1eb2SDag-Erling SmørgravThe checks are performed using the effective user and group IDs,
133*a4be1eb2SDag-Erling Smørgravlike
134*a4be1eb2SDag-Erling Smørgrav.Fn eaccess ,
135*a4be1eb2SDag-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.
1578269e767SBrooks Davis.El
1588269e767SBrooks Davis.Pp
1598269e767SBrooks DavisEven if a process's real or effective user has appropriate privileges
1608269e767SBrooks Davisand indicates success for
1618269e767SBrooks Davis.Dv X_OK ,
1628269e767SBrooks Davisthe file may not actually have execute permission bits set.
1638269e767SBrooks DavisLikewise for
1648269e767SBrooks Davis.Dv R_OK
1658269e767SBrooks Davisand
1668269e767SBrooks Davis.Dv W_OK .
1678269e767SBrooks Davis.Sh RETURN VALUES
1688269e767SBrooks Davis.Rv -std
1698269e767SBrooks Davis.Sh ERRORS
170*a4be1eb2SDag-Erling SmørgravThe
1718269e767SBrooks Davis.Fn access ,
1728269e767SBrooks Davis.Fn eaccess ,
173*a4be1eb2SDag-Erling Smørgravand
1748269e767SBrooks Davis.Fn faccessat
175*a4be1eb2SDag-Erling Smørgravsystem calls may fail if:
1768269e767SBrooks Davis.Bl -tag -width Er
1778269e767SBrooks Davis.It Bq Er EINVAL
1788269e767SBrooks DavisThe value of the
1798269e767SBrooks Davis.Fa mode
1808269e767SBrooks Davisargument is invalid.
1818269e767SBrooks Davis.It Bq Er ENOTDIR
1828269e767SBrooks DavisA component of the path prefix is not a directory.
1838269e767SBrooks Davis.It Bq Er ENAMETOOLONG
1848269e767SBrooks DavisA component of a pathname exceeded 255 characters,
1858269e767SBrooks Davisor an entire path name exceeded 1023 characters.
1868269e767SBrooks Davis.It Bq Er ENOENT
1878269e767SBrooks DavisThe named file does not exist.
1888269e767SBrooks Davis.It Bq Er ELOOP
1898269e767SBrooks DavisToo many symbolic links were encountered in translating the pathname.
1908269e767SBrooks Davis.It Bq Er EROFS
1918269e767SBrooks DavisWrite access is requested for a file on a read-only file system.
1928269e767SBrooks Davis.It Bq Er ETXTBSY
1938269e767SBrooks DavisWrite access is requested for a pure procedure (shared text)
1948269e767SBrooks Davisfile presently being executed.
1958269e767SBrooks Davis.It Bq Er EACCES
1968269e767SBrooks DavisPermission bits of the file mode do not permit the requested
1978269e767SBrooks Davisaccess, or search permission is denied on a component of the
1988269e767SBrooks Davispath prefix.
1998269e767SBrooks Davis.It Bq Er EFAULT
2008269e767SBrooks DavisThe
2018269e767SBrooks Davis.Fa path
2028269e767SBrooks Davisargument
2038269e767SBrooks Davispoints outside the process's allocated address space.
2048269e767SBrooks Davis.It Bq Er EIO
2058269e767SBrooks DavisAn I/O error occurred while reading from or writing to the file system.
2068269e767SBrooks Davis.It Bq Er EINTEGRITY
2078269e767SBrooks DavisCorrupted data was detected while reading from the file system.
2088269e767SBrooks Davis.El
2098269e767SBrooks Davis.Pp
2108269e767SBrooks DavisAlso, the
2118269e767SBrooks Davis.Fn faccessat
2128269e767SBrooks Davissystem call may fail if:
2138269e767SBrooks Davis.Bl -tag -width Er
2148269e767SBrooks Davis.It Bq Er EBADF
2158269e767SBrooks DavisThe
2168269e767SBrooks Davis.Fa path
2178269e767SBrooks Davisargument does not specify an absolute path and the
2188269e767SBrooks Davis.Fa fd
2198269e767SBrooks Davisargument is
2208269e767SBrooks Davisneither
2218269e767SBrooks Davis.Dv AT_FDCWD
2228269e767SBrooks Davisnor a valid file descriptor.
2238269e767SBrooks Davis.It Bq Er EINVAL
2248269e767SBrooks DavisThe value of the
2258269e767SBrooks Davis.Fa flag
2268269e767SBrooks Davisargument is not valid.
2278269e767SBrooks Davis.It Bq Er ENOTDIR
2288269e767SBrooks DavisThe
2298269e767SBrooks Davis.Fa path
2308269e767SBrooks Davisargument is not an absolute path and
2318269e767SBrooks Davis.Fa fd
2328269e767SBrooks Davisis neither
2338269e767SBrooks Davis.Dv AT_FDCWD
2348269e767SBrooks Davisnor a file descriptor associated with a directory.
2358269e767SBrooks Davis.It Bq Er ENOTCAPABLE
2368269e767SBrooks Davis.Fa path
2378269e767SBrooks Davisis an absolute path,
2388269e767SBrooks Davisor contained a ".." component leading to a
2398269e767SBrooks Davisdirectory outside of the directory hierarchy specified by
2408269e767SBrooks Davis.Fa fd ,
2418269e767SBrooks Davisand the process is in capability mode.
2428269e767SBrooks Davis.El
2438269e767SBrooks Davis.Sh SEE ALSO
2448269e767SBrooks Davis.Xr chmod 2 ,
2458269e767SBrooks Davis.Xr intro 2 ,
2468269e767SBrooks Davis.Xr stat 2
2478269e767SBrooks Davis.Sh STANDARDS
2488269e767SBrooks DavisThe
2498269e767SBrooks Davis.Fn access
2508269e767SBrooks Davissystem call is expected to conform to
2518269e767SBrooks Davis.St -p1003.1-90 .
2528269e767SBrooks DavisThe
2538269e767SBrooks Davis.Fn faccessat
2548269e767SBrooks Davissystem call follows The Open Group Extended API Set 2 specification.
2558269e767SBrooks Davis.Sh HISTORY
2568269e767SBrooks DavisThe
2578269e767SBrooks Davis.Fn access
2588269e767SBrooks Davisfunction appeared in
2598269e767SBrooks Davis.At v7 .
2608269e767SBrooks DavisThe
2618269e767SBrooks Davis.Fn faccessat
2628269e767SBrooks Davissystem call appeared in
2638269e767SBrooks Davis.Fx 8.0 .
2648269e767SBrooks Davis.Sh SECURITY CONSIDERATIONS
2658269e767SBrooks DavisThe
266*a4be1eb2SDag-Erling Smørgrav.Fn access ,
267*a4be1eb2SDag-Erling Smørgrav.Fn eaccess ,
268*a4be1eb2SDag-Erling Smørgravand
269*a4be1eb2SDag-Erling Smørgrav.Fn faccessat
270*a4be1eb2SDag-Erling Smørgravsystem calls are subject to time-of-check-to-time-of-use races and
271*a4be1eb2SDag-Erling Smørgravshould not be relied upon for file permission enforcement purposes.
272*a4be1eb2SDag-Erling SmørgravInstead, applications should perform the desired action using the
273*a4be1eb2SDag-Erling Smørgravrequesting user's credentials.
274