]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/access.2
s/filesystem/file system/
[thirdparty/man-pages.git] / man2 / access.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\" 1993 Michael Haardt, Ian Jackson.
5 .\" and Copyright (C) 2007 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\"
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
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
41 .\" 2007-06-10, mtk, various parts rewritten, and added BUGS section.
42 .\"
43 .TH ACCESS 2 2007-07-10 "Linux" "Linux Programmer's Manual"
44 .SH NAME
45 access \- check real user's permissions for a file
46 .SH SYNOPSIS
47 .nf
48 .B #include <unistd.h>
49 .sp
50 .BI "int access(const char *" pathname ", int " mode );
51 .fi
52 .SH DESCRIPTION
53 .BR access ()
54 checks whether the calling process can access the file
55 .IR pathname .
56 If
57 .I pathname
58 is a symbolic link, it is dereferenced.
59
60 The
61 .I mode
62 specifies the accessibility check(s) to be performed,
63 and is either the value
64 .BR F_OK ,
65 or a mask consisting of the bitwise OR of one or more of
66 .BR R_OK ", " W_OK ", and " X_OK .
67 .\" FIXME . the kernel currently allows F_OK to be specified
68 .\" in conjunction with any of the other flags.
69 .\" Test whether other implementations permit this and if not
70 .\" file a bug report.
71 .B F_OK
72 tests for the existence of the file.
73 .BR R_OK ", " W_OK ", and " X_OK
74 test whether the file exists and grants read, write, and
75 execute permissions, respectively.
76
77 The check is done using the calling process's
78 .I real
79 UID and GID, rather than the effective IDs as is done when
80 actually attempting an operation (e.g.,
81 .BR open (2))
82 on the file.
83 This allows set-user-ID programs to
84 easily determine the invoking user's authority.
85
86 If the calling process is privileged (i.e., its real UID is zero),
87 then an
88 .B X_OK
89 check is successful for a regular file if execute permission
90 is enabled for any of the file owner, group, or other.
91 .SH "RETURN VALUE"
92 On success (all requested permissions granted), zero is returned.
93 On error (at least one bit in
94 .I mode
95 asked for a permission that is denied, or some other error occurred),
96 \-1 is returned, and
97 .I errno
98 is set appropriately.
99 .SH ERRORS
100 .BR access ()
101 shall fail if:
102 .TP
103 .B EACCES
104 The requested access would be denied to the file, or search permission
105 is denied for one of the directories in the path prefix of
106 .IR pathname .
107 (See also
108 .BR path_resolution (7).)
109 .TP
110 .B ELOOP
111 Too many symbolic links were encountered in resolving
112 .IR pathname .
113 .TP
114 .B ENAMETOOLONG
115 .I pathname
116 is too long.
117 .TP
118 .B ENOENT
119 A component of
120 .I pathname
121 does not exist or is a dangling symbolic link.
122 .TP
123 .B ENOTDIR
124 A component used as a directory in
125 .I pathname
126 is not, in fact, a directory.
127 .TP
128 .B EROFS
129 Write permission was requested for a file on a read-only file system.
130 .PP
131 .BR access ()
132 may fail if:
133 .TP
134 .B EFAULT
135 .I pathname
136 points outside your accessible address space.
137 .TP
138 .B EINVAL
139 .I mode
140 was incorrectly specified.
141 .TP
142 .B EIO
143 An I/O error occurred.
144 .TP
145 .B ENOMEM
146 Insufficient kernel memory was available.
147 .TP
148 .B ETXTBSY
149 Write access was requested to an executable which is being
150 executed.
151 .SH "CONFORMING TO"
152 SVr4, 4.3BSD, POSIX.1-2001.
153 .SH NOTES
154 .PP
155 .BR Warning :
156 Using
157 .BR access ()
158 to check if a user is authorized to, for example,
159 open a file before actually doing so using
160 .BR open (2)
161 creates a security hole, because the user might exploit the short time
162 interval between checking and opening the file to manipulate it.
163 .BR "For this reason, the use of this system call should be avoided" .
164 .PP
165 .BR access ()
166 returns an error if any of the access types in
167 .I mode
168 is denied, even if some of the other access types in
169 .I mode
170 are permitted.
171 .PP
172 If the calling process has appropriate privileges (i.e., is superuser),
173 POSIX.1-2001 permits implementation to indicate success for an
174 .B X_OK
175 check even if none of the execute file permission bits are set.
176 .\" HPU-UX 11 and Tru64 5.1 do this.
177 Linux does not do this.
178 .PP
179 A file is only accessible if the permissions on each of the
180 directories in the path prefix of
181 .I pathname
182 grant search (i.e., execute) access.
183 If any directory is inaccessible, then the
184 .BR access ()
185 call will fail, regardless of the permissions on the file itself.
186 .PP
187 Only access bits are checked, not the file type or contents.
188 Therefore, if a directory is found to be writable,
189 it probably means that files can be created in the directory,
190 and not that the directory can be written as a file.
191 Similarly, a DOS file may be found to be "executable," but the
192 .BR execve (2)
193 call will still fail.
194 .PP
195 .BR access ()
196 may not work correctly on NFS file systems with UID mapping enabled,
197 because UID mapping is done on the server and hidden from the client,
198 which checks permissions.
199 .SH BUGS
200 In kernel 2.4 (and earlier) there is some strangeness in the handling of
201 .B X_OK
202 tests for superuser.
203 If all categories of execute permission are disabled
204 for a non-directory file, then the only
205 .BR access ()
206 test that returns \-1 is when
207 .I mode
208 is specified as just
209 .BR X_OK ;
210 if
211 .B R_OK
212 or
213 .B W_OK
214 is also specified in
215 .IR mode ,
216 then
217 .BR access ()
218 returns 0 for such files.
219 .\" This behavior appears to have been an implementation accident.
220 Early 2.6 kernels (up to and including 2.6.3)
221 also behaved in the same way as kernel 2.4.
222
223 In kernels before 2.6.20,
224 .BR access ()
225 ignored the effect of the
226 .B MS_NOEXEC
227 flag if it was used to
228 .BR mount (2)
229 the underlying file system.
230 Since kernel 2.6.20,
231 .BR access ()
232 honors this flag.
233 .SH "SEE ALSO"
234 .BR chmod (2),
235 .BR chown (2),
236 .BR faccessat (2),
237 .BR open (2),
238 .BR setgid (2),
239 .BR setuid (2),
240 .BR stat (2),
241 .BR euidaccess (3),
242 .BR credentials (7),
243 .BR path_resolution (7)