xref: /openbsd-src/lib/libc/sys/access.2 (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1.\"	$OpenBSD: access.2,v 1.18 2011/07/19 20:58:05 matthew Exp $
2.\"	$NetBSD: access.2,v 1.7 1995/02/27 12:31:44 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)access.2	8.2 (Berkeley) 4/1/94
32.\"
33.Dd $Mdocdate: July 19 2011 $
34.Dt ACCESS 2
35.Os
36.Sh NAME
37.Nm access ,
38.Nm faccessat
39.Nd check access permissions of a file or pathname
40.Sh SYNOPSIS
41.Fd #include <fcntl.h>
42.Fd #include <unistd.h>
43.Ft int
44.Fn access "const char *path" "int amode"
45.Ft int
46.Fn faccessat "int fd" "const char *path" "int amode" "int flag"
47.Sh DESCRIPTION
48The
49.Fn access
50function checks the accessibility of the file named by
51.Fa path
52for the access permissions indicated by
53.Fa amode .
54The value of
55.Fa amode
56is the bitwise inclusive
57.Tn OR
58of the access permissions to be checked
59.Pf ( Dv R_OK
60for read permission,
61.Dv W_OK
62for write permission, and
63.Dv X_OK
64for execute/search permission) or the existence test,
65.Dv F_OK .
66All components of the pathname
67.Fa path
68are checked for access permissions (including
69.Dv F_OK ) .
70.Pp
71The real user ID is used in place of the effective user ID
72and the real group access list
73(including the real group ID) is
74used in place of the effective ID for verifying permission.
75.Pp
76If the invoking process has superuser privileges,
77.Fn access
78will always indicate success for
79.Dv R_OK
80and
81.Dv W_OK ,
82regardless of the actual file permission bits.
83Likewise, for
84.Dv X_OK ,
85if the file has any of the execute bits set and
86.Fa path
87is not a directory,
88.Fn access
89will indicate success.
90.Pp
91The
92.Fn faccessat
93function is equivalent to
94.Fn access
95except that where
96.Fa path
97specifies a relative path,
98the file whose accessibility is checked is determined relative to
99the directory associated with file descriptor
100.Fa fd
101instead of the current working directory.
102.Pp
103If
104.Fn faccessat
105is passed the special value
106.Dv AT_FDCWD
107(defined in
108.In fcntl.h )
109in the
110.Fa fd
111parameter, the current working directory is used.
112If
113.Fa flag
114is also zero, the behavior is identical to a call to
115.Fn access .
116.Pp
117Values for
118.Fa flag
119are constructed by bitwise-inclusive
120.Tn OR Ns ing
121flags from the following list defined in
122.In fcntl.h :
123.Pp
124.Bl -tag -width AT_EACCESS -offset indent -compact
125.It Dv AT_EACCESS
126The checks for accessibility are performed using the effective user
127and group IDs instead of the real user and group IDs.
128.El
129.Sh RETURN VALUES
130If
131.Fa path
132cannot be found or if any of the desired access modes would not be granted,
133then a \-1 value is returned; otherwise a 0 value is returned.
134.Sh ERRORS
135Access to the file is denied if:
136.Bl -tag -width Er
137.It Bq Er ENOTDIR
138A component of the path prefix is not a directory.
139.It Bq Er ENAMETOOLONG
140A component of a pathname exceeded
141.Dv {NAME_MAX}
142characters, or an entire path name exceeded
143.Dv {PATH_MAX}
144characters.
145.It Bq Er ENOENT
146The named file does not exist.
147.It Bq Er ELOOP
148Too many symbolic links were encountered in translating the pathname.
149.It Bq Er EROFS
150Write access is requested for a file on a read-only file system.
151.It Bq Er ETXTBSY
152Write access is requested for a pure procedure (shared text)
153file presently being executed.
154.It Bq Er EACCES
155Permission bits of the file mode do not permit the requested access,
156or search permission is denied on a component of the path prefix.
157The owner of a file has permission checked with respect to the
158.Dq owner
159read, write, and execute mode bits, members of the file's group other
160than the owner have permission checked with respect to the
161.Dq group
162mode bits, and all others have permissions checked with respect to the
163.Dq other
164mode bits.
165.It Bq Er EPERM
166Write access has been requested and the named file has its immutable
167flag set (see
168.Xr chflags 2 ) .
169.It Bq Er EFAULT
170.Fa path
171points outside the process's allocated address space.
172.It Bq Er EIO
173An I/O error occurred while reading from or writing to the file system.
174.It Bq Er EINVAL
175An invalid value was specified for
176.Ar amode .
177.El
178.Pp
179Additionally,
180.Fn faccessat
181will fail if:
182.Bl -tag -width Er
183.It Bq Er EBADF
184The
185.Fa path
186argument does not specify an absolute path and the
187.Fa fd
188argument is neither
189.Dv AT_FDCWD
190nor a valid file descriptor open for reading.
191.El
192.Sh SEE ALSO
193.Xr chmod 2 ,
194.Xr stat 2
195.Sh STANDARDS
196The
197.Fn access
198and
199.Fn faccessat
200functions conform to
201.St -p1003.1-2008 .
202.Sh HISTORY
203The
204.Fn faccessat
205function appeared in
206.Ox 5.0 .
207.Sh CAVEATS
208.Fn access
209and
210.Fn faccessat
211should never be used for actual access control.
212Doing so can result in a time of check vs. time of use security hole.
213