]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/access.2
link.2: Glibc falls back to link() on kernels that lack linkat()
[thirdparty/man-pages.git] / man2 / access.2
CommitLineData
fea681da 1.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
ac56b6a8 2.\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
687dcec2 3.\" and Copyright (C) 2004, 2006, 2007, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 4.\"
93015253 5.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
6.\" Permission is granted to make and distribute verbatim copies of this
7.\" manual provided the copyright notice and this permission notice are
8.\" preserved on all copies.
9.\"
10.\" Permission is granted to copy and distribute modified versions of this
11.\" manual under the conditions for verbatim copying, provided that the
12.\" entire resulting derived work is distributed under the terms of a
13.\" permission notice identical to this one.
c13182ef 14.\"
fea681da
MK
15.\" Since the Linux kernel and libraries are constantly changing, this
16.\" manual page may be incorrect or out-of-date. The author(s) assume no
17.\" responsibility for errors or omissions, or for damages resulting from
18.\" the use of the information contained herein. The author(s) may not
19.\" have taken the same level of care in the production of this manual,
20.\" which is licensed free of charge, as they might when working
21.\" professionally.
c13182ef 22.\"
fea681da
MK
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 25.\" %%%LICENSE_END
fea681da
MK
26.\"
27.\" Modified 1993-07-21 Rik Faith (faith@cs.unc.edu)
28.\" Modified 1994-08-21 by Michael Chastain (mec@shell.portal.com):
29.\" Removed note about old kernel (pre-1.1.44) using wrong id on path.
30.\" Modified 1996-03-18 by Martin Schulze (joey@infodrom.north.de):
31.\" Stated more clearly how it behaves with symbolic links.
32.\" Added correction due to Nick Duffek (nsd@bbc.com), aeb, 960426
33.\" Modified 1996-09-07 by Michael Haardt:
34.\" Restrictions for NFS
35.\" Modified 1997-09-09 by Joseph S. Myers <jsm28@cam.ac.uk>
36.\" Modified 1998-01-13 by Michael Haardt:
37.\" Using access is often insecure
38.\" Modified 2001-10-16 by aeb
39.\" Modified 2002-04-23 by Roger Luethi <rl@hellgate.ch>
40.\" Modified 2004-06-23 by Michael Kerrisk
02d79ece 41.\" 2007-06-10, mtk, various parts rewritten, and added BUGS section.
fea681da 42.\"
687dcec2 43.TH ACCESS 2 2014-02-21 "Linux" "Linux Programmer's Manual"
fea681da 44.SH NAME
687dcec2 45access, faccessat \- check user's permissions for a file
fea681da
MK
46.SH SYNOPSIS
47.nf
48.B #include <unistd.h>
49.sp
50.BI "int access(const char *" pathname ", int " mode );
687dcec2 51
2626921f 52.BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
687dcec2
MK
53.B #include <unistd.h>
54.sp
55.BI "int faccessat(int " dirfd ", const char *" pathname ", int " \
56mode ", int " flags );
57.fi
58.sp
59.in -4n
60Feature Test Macro Requirements for glibc (see
61.BR feature_test_macros (7)):
62.in
63.sp
64.BR faccessat ():
65.PD 0
66.ad l
67.RS 4
68.TP 4
69Since glibc 2.10:
70_XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
71.TP
72Before glibc 2.10:
73_ATFILE_SOURCE
74.RE
75.ad
76.PD
fea681da
MK
77.fi
78.SH DESCRIPTION
e511ffb6 79.BR access ()
02d79ece 80checks whether the calling process can access the file
fea681da
MK
81.IR pathname .
82If
83.I pathname
02d79ece 84is a symbolic link, it is dereferenced.
fea681da 85
02d79ece 86The
fea681da 87.I mode
02d79ece
MK
88specifies the accessibility check(s) to be performed,
89and is either the value
90.BR F_OK ,
60948d68 91.\" F_OK is defined as 0 on every system that I know of.
b0742c81 92or a mask consisting of the bitwise OR of one or more of
02d79ece 93.BR R_OK ", " W_OK ", and " X_OK .
fea681da 94.B F_OK
02d79ece
MK
95tests for the existence of the file.
96.BR R_OK ", " W_OK ", and " X_OK
97test whether the file exists and grants read, write, and
98execute permissions, respectively.
fea681da 99
02d79ece 100The check is done using the calling process's
fea681da 101.I real
02d79ece
MK
102UID and GID, rather than the effective IDs as is done when
103actually attempting an operation (e.g.,
104.BR open (2))
105on the file.
106This allows set-user-ID programs to
fea681da
MK
107easily determine the invoking user's authority.
108
02d79ece
MK
109If the calling process is privileged (i.e., its real UID is zero),
110then an
fea681da 111.B X_OK
02d79ece
MK
112check is successful for a regular file if execute permission
113is enabled for any of the file owner, group, or other.
e04fe041 114.SS faccessat()
687dcec2
MK
115The
116.BR faccessat ()
117system call operates in exactly the same way as
cadd38ba 118.BR access (),
687dcec2
MK
119except for the differences described here.
120
121If the pathname given in
122.I pathname
123is relative, then it is interpreted relative to the directory
124referred to by the file descriptor
125.I dirfd
126(rather than relative to the current working directory of
127the calling process, as is done by
9d3ea7a2 128.BR access ()
687dcec2
MK
129for a relative pathname).
130
131If
132.I pathname
133is relative and
134.I dirfd
135is the special value
136.BR AT_FDCWD ,
137then
138.I pathname
139is interpreted relative to the current working
140directory of the calling process (like
98d3114d 141.BR access ()).
687dcec2
MK
142
143If
144.I pathname
145is absolute, then
146.I dirfd
147is ignored.
148
149.I flags
150is constructed by ORing together zero or more of the following values:
151.TP
152.B AT_EACCESS
153Perform access checks using the effective user and group IDs.
154By default,
155.BR faccessat ()
156uses the real IDs (like
98d3114d 157.BR access ()).
687dcec2
MK
158.TP
159.B AT_SYMLINK_NOFOLLOW
160If
161.I pathname
162is a symbolic link, do not dereference it:
163instead return information about the link itself.
2907fa3e
MK
164.PP
165See
166.BR openat (2)
167for an explanation of the need for
168.BR faccessat ().
e0a699c1 169.SH "RETURN VALUE"
72d34312
MK
170On success (all requested permissions granted, or
171.I mode
172is
173.B F_OK
174and the file exists), zero is returned.
fea681da
MK
175On error (at least one bit in
176.I mode
72d34312
MK
177asked for a permission that is denied, or
178.I mode
179is
180.B F_OK
181and the file does not exist, or some other error occurred),
fea681da
MK
182\-1 is returned, and
183.I errno
184is set appropriately.
185.SH ERRORS
e511ffb6 186.BR access ()
32e370f2
MK
187and
188.BR faccessat ()
fea681da
MK
189shall fail if:
190.TP
191.B EACCES
dfaae362 192The requested access would be denied to the file, or search permission
fea681da
MK
193is denied for one of the directories in the path prefix of
194.IR pathname .
195(See also
ad7cc990 196.BR path_resolution (7).)
fea681da
MK
197.TP
198.B ELOOP
199Too many symbolic links were encountered in resolving
200.IR pathname .
201.TP
202.B ENAMETOOLONG
203.I pathname
204is too long.
205.TP
206.B ENOENT
33e820cb 207A component of
fea681da 208.I pathname
33e820cb 209does not exist or is a dangling symbolic link.
fea681da
MK
210.TP
211.B ENOTDIR
212A component used as a directory in
213.I pathname
214is not, in fact, a directory.
215.TP
216.B EROFS
9ee4a2b6 217Write permission was requested for a file on a read-only filesystem.
fea681da 218.PP
e511ffb6 219.BR access ()
32e370f2
MK
220and
221.BR faccessat ()
fea681da
MK
222may fail if:
223.TP
224.B EFAULT
225.I pathname
226points outside your accessible address space.
227.TP
228.B EINVAL
229.I mode
230was incorrectly specified.
231.TP
232.B EIO
233An I/O error occurred.
234.TP
235.B ENOMEM
236Insufficient kernel memory was available.
237.TP
238.B ETXTBSY
239Write access was requested to an executable which is being
240executed.
687dcec2
MK
241.PP
242The following additional errors can occur for
243.BR faccessat ():
244.TP
245.B EBADF
246.I dirfd
247is not a valid file descriptor.
248.TP
249.B EINVAL
250Invalid flag specified in
251.IR flags .
252.TP
253.B ENOTDIR
254.I pathname
255is relative and
256.I dirfd
257is a file descriptor referring to a file other than a directory.
258.SH VERSIONS
259.BR faccessat ()
260was added to Linux in kernel 2.6.16;
261library support was added to glibc in version 2.4.
e0a699c1 262.SH "CONFORMING TO"
9d3ea7a2 263.BR access ():
b9f4755d 264SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
687dcec2 265
cadd38ba 266.BR faccessat ():
687dcec2 267POSIX.1-2008.
b07cd0a9 268.SH NOTES
fea681da 269.PP
4df883b9 270.BR Warning :
32e370f2 271Using these calls to check if a user is authorized to, for example,
dfaae362 272open a file before actually doing so using
fea681da
MK
273.BR open (2)
274creates a security hole, because the user might exploit the short time
275interval between checking and opening the file to manipulate it.
02d79ece 276.BR "For this reason, the use of this system call should be avoided" .
47135643
MK
277(In the example just described,
278a safer alternative would be to temporarily switch the process's
279effective user ID to the real ID and then call
280.BR open (2).)
02d79ece 281.PP
27d47e71 282.BR access ()
05b92251
MK
283always dereferences symbolic links.
284If you need to check the permissions on a symbolic link, use
6fdbc779 285.BR faccessat (2)
05b92251
MK
286with the flag
287.BR AT_SYMLINK_NOFOLLOW .
288.PP
32e370f2 289These calls return an error if any of the access types in
02d79ece
MK
290.I mode
291is denied, even if some of the other access types in
292.I mode
293are permitted.
294.PP
295If the calling process has appropriate privileges (i.e., is superuser),
7a35b981 296POSIX.1-2001 permits an implementation to indicate success for an
02d79ece
MK
297.B X_OK
298check even if none of the execute file permission bits are set.
d8ccd692 299.\" HPU-UX 11 and Tru64 5.1 do this.
02d79ece
MK
300Linux does not do this.
301.PP
33a0ccb2 302A file is accessible only if the permissions on each of the
02d79ece
MK
303directories in the path prefix of
304.I pathname
305grant search (i.e., execute) access.
306If any directory is inaccessible, then the
307.BR access ()
308call will fail, regardless of the permissions on the file itself.
309.PP
310Only access bits are checked, not the file type or contents.
311Therefore, if a directory is found to be writable,
312it probably means that files can be created in the directory,
313and not that the directory can be written as a file.
314Similarly, a DOS file may be found to be "executable," but the
315.BR execve (2)
316call will still fail.
317.PP
32e370f2 318These calls
1a7d4eb7 319may not work correctly on NFSv2 filesystems with UID mapping enabled,
02d79ece 320because UID mapping is done on the server and hidden from the client,
1a7d4eb7
BF
321which checks permissions. (NFS versions 3 and higher perform the check on
322the server.)
e0a699c1 323Similar problems can occur to FUSE mounts.
92692952
MK
324.\"
325.\"
8f692313 326.SS C library/kernel ABI differences
2907fa3e
MK
327The raw
328.BR faccessat ()
329system call takes only the first three arguments.
687dcec2
MK
330The
331.B AT_EACCESS
332and
333.B AT_SYMLINK_NOFOLLOW
334flags are actually implemented within the glibc wrapper function for
335.BR faccessat ().
336If either of these flags is specified, then the wrapper function employs
337.BR fstatat (2)
338to determine access permissions.
02d79ece
MK
339.SH BUGS
340In kernel 2.4 (and earlier) there is some strangeness in the handling of
341.B X_OK
342tests for superuser.
343If all categories of execute permission are disabled
24b74457 344for a nondirectory file, then the only
02d79ece
MK
345.BR access ()
346test that returns \-1 is when
347.I mode
348is specified as just
349.BR X_OK ;
350if
351.B R_OK
352or
353.B W_OK
354is also specified in
355.IR mode ,
356then
357.BR access ()
358returns 0 for such files.
359.\" This behavior appears to have been an implementation accident.
360Early 2.6 kernels (up to and including 2.6.3)
361also behaved in the same way as kernel 2.4.
362
a1d5f77c 363In kernels before 2.6.20,
32e370f2 364these calls ignored the effect of the
a1d5f77c
MK
365.B MS_NOEXEC
366flag if it was used to
367.BR mount (2)
9ee4a2b6 368the underlying filesystem.
32e370f2
MK
369Since kernel 2.6.20, the
370.B MS_NOEXEC
371is honored
e0a699c1 372.SH "SEE ALSO"
fea681da
MK
373.BR chmod (2),
374.BR chown (2),
375.BR open (2),
fea681da
MK
376.BR setgid (2),
377.BR setuid (2),
d975050c 378.BR stat (2),
70d1cb2d 379.BR euidaccess (3),
53a1443c 380.BR credentials (7),
687dcec2
MK
381.BR path_resolution (7),
382.BR symlink (7)