xref: /freebsd-13-stable/lib/libc/sys/access.2 (revision 55cd52a4418d79962ebd81f34544a3747db9e031)
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.\"     @(#)access.2	8.2 (Berkeley) 4/1/94
29.\"
30.Dd May 21, 2024
31.Dt ACCESS 2
32.Os
33.Sh NAME
34.Nm access ,
35.Nm eaccess ,
36.Nm faccessat
37.Nd check accessibility of a file
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In unistd.h
42.Ft int
43.Fn access "const char *path" "int mode"
44.Ft int
45.Fn eaccess "const char *path" "int mode"
46.Ft int
47.Fn faccessat "int fd" "const char *path" "int mode" "int flag"
48.Sh DESCRIPTION
49The
50.Fn access ,
51.Fn eaccess
52and
53.Fn faccessat
54system calls report whether an attempt to access the file designated
55by their
56.Fa path
57in the manner described by their
58.Fa mode
59argument is likely to succeed.
60The value of
61.Fa mode
62is either the bitwise-inclusive OR of the desired permissions
63.Po
64.Dv R_OK
65for read permission,
66.Dv W_OK
67for write permission, and
68.Dv X_OK
69for execute / search permission
70.Pc
71or
72.Dv F_OK
73to simply check whether the file exists.
74.Pp
75For a number of reasons, these system calls cannot be relied upon to
76give a correct and definitive answer.
77They can at best provide an early indication of the expected outcome,
78to be confirmed by actually attempting the operation.
79For existence checks, either
80.Xr stat 2
81or
82.Xr lstat 2
83should be used instead.
84See also
85.Sx SECURITY CONSIDERATIONS
86below.
87.Pp
88The
89.Fn eaccess
90system call uses
91the effective user ID and the group access list
92to authorize the request;
93the
94.Fn access
95system call uses
96the real user ID in place of the effective user ID,
97the real group ID in place of the effective group ID,
98and the rest of the group access list.
99.Pp
100See the
101.Sx DEFINITIONS
102section of
103.Xr intro 2
104for additional information on file access permissions and real
105vs. effective user and group IDs.
106.Pp
107The
108.Fn faccessat
109system call is equivalent to
110.Fn access
111except in the case where
112.Fa path
113specifies a relative path.
114In this case the file whose accessibility is to be determined is
115located relative to the directory associated with the file descriptor
116.Fa fd
117instead of the current working directory.
118If
119.Fn faccessat
120is passed the special value
121.Dv AT_FDCWD
122in the
123.Fa fd
124parameter, the current working directory is used and the behavior is
125identical to a call to
126.Fn access .
127Values for
128.Fa flag
129are constructed by a bitwise-inclusive OR of flags from the following
130list, defined in
131.In fcntl.h :
132.Bl -tag -width indent
133.It Dv AT_EACCESS
134The checks are performed using the effective user and group IDs,
135like
136.Fn eaccess ,
137instead of the real user and group ID, like
138.Fn access .
139.It Dv AT_RESOLVE_BENEATH
140Only walk paths below the directory specified by the
141.Ar fd
142descriptor.
143See the description of the
144.Dv O_RESOLVE_BENEATH
145flag in the
146.Xr open 2
147manual page.
148.It Dv AT_EMPTY_PATH
149If the
150.Fa path
151argument is an empty string, operate on the file or directory
152referenced by the descriptor
153.Fa fd .
154If
155.Fa fd
156is equal to
157.Dv AT_FDCWD ,
158operate on the current working directory.
159.El
160.Pp
161Even if a process's real or effective user has appropriate privileges
162and indicates success for
163.Dv X_OK ,
164the file may not actually have execute permission bits set.
165Likewise for
166.Dv R_OK
167and
168.Dv W_OK .
169.Sh RETURN VALUES
170.Rv -std
171.Sh ERRORS
172The
173.Fn access ,
174.Fn eaccess ,
175and
176.Fn faccessat
177system calls may fail if:
178.Bl -tag -width Er
179.It Bq Er EINVAL
180The value of the
181.Fa mode
182argument is invalid.
183.It Bq Er ENOTDIR
184A component of the path prefix is not a directory.
185.It Bq Er ENAMETOOLONG
186A component of a pathname exceeded 255 characters,
187or an entire path name exceeded 1023 characters.
188.It Bq Er ENOENT
189The named file does not exist.
190.It Bq Er ELOOP
191Too many symbolic links were encountered in translating the pathname.
192.It Bq Er EROFS
193Write access is requested for a file on a read-only file system.
194.It Bq Er ETXTBSY
195Write access is requested for a pure procedure (shared text)
196file presently being executed.
197.It Bq Er EACCES
198Permission bits of the file mode do not permit the requested
199access, or search permission is denied on a component of the
200path prefix.
201.It Bq Er EFAULT
202The
203.Fa path
204argument
205points outside the process's allocated address space.
206.It Bq Er EIO
207An I/O error occurred while reading from or writing to the file system.
208.It Bq Er EINTEGRITY
209Corrupted data was detected while reading from the file system.
210.El
211.Pp
212Also, the
213.Fn faccessat
214system call may fail if:
215.Bl -tag -width Er
216.It Bq Er EBADF
217The
218.Fa path
219argument does not specify an absolute path and the
220.Fa fd
221argument is
222neither
223.Dv AT_FDCWD
224nor a valid file descriptor.
225.It Bq Er EINVAL
226The value of the
227.Fa flag
228argument is not valid.
229.It Bq Er ENOTDIR
230The
231.Fa path
232argument is not an absolute path and
233.Fa fd
234is neither
235.Dv AT_FDCWD
236nor a file descriptor associated with a directory.
237.It Bq Er ENOTCAPABLE
238.Fa path
239is an absolute path,
240or contained a ".." component leading to a
241directory outside of the directory hierarchy specified by
242.Fa fd ,
243and the process is in capability mode.
244.El
245.Sh SEE ALSO
246.Xr chmod 2 ,
247.Xr intro 2 ,
248.Xr stat 2
249.Sh STANDARDS
250The
251.Fn access
252system call is expected to conform to
253.St -p1003.1-90 .
254The
255.Fn faccessat
256system call follows The Open Group Extended API Set 2 specification.
257.Sh HISTORY
258The
259.Fn access
260function appeared in
261.At v7 .
262The
263.Fn faccessat
264system call appeared in
265.Fx 8.0 .
266.Sh SECURITY CONSIDERATIONS
267The
268.Fn access ,
269.Fn eaccess ,
270and
271.Fn faccessat
272system calls are subject to time-of-check-to-time-of-use races and
273should not be relied upon for file permission enforcement purposes.
274Instead, applications should perform the desired action using the
275requesting user's credentials.
276