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