]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/stat.2
First draft of documentation for AT/O_BENEATH handling of the absolute
[FreeBSD/FreeBSD.git] / lib / libc / sys / stat.2
1 .\" Copyright (c) 1980, 1991, 1993, 1994
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 .\"     @(#)stat.2      8.4 (Berkeley) 5/1/95
29 .\" $FreeBSD$
30 .\"
31 .Dd November 11, 2018
32 .Dt STAT 2
33 .Os
34 .Sh NAME
35 .Nm stat ,
36 .Nm lstat ,
37 .Nm fstat ,
38 .Nm fstatat
39 .Nd get file status
40 .Sh LIBRARY
41 .Lb libc
42 .Sh SYNOPSIS
43 .In sys/stat.h
44 .Ft int
45 .Fn stat "const char * restrict path" "struct stat * restrict sb"
46 .Ft int
47 .Fn lstat "const char * restrict path" "struct stat * restrict sb"
48 .Ft int
49 .Fn fstat "int fd" "struct stat *sb"
50 .Ft int
51 .Fn fstatat "int fd" "const char *path" "struct stat *buf" "int flag"
52 .Sh DESCRIPTION
53 The
54 .Fn stat
55 system call obtains information about the file pointed to by
56 .Fa path .
57 Read, write or execute
58 permission of the named file is not required, but all directories
59 listed in the path name leading to the file must be searchable.
60 .Pp
61 The
62 .Fn lstat
63 system call is like
64 .Fn stat
65 except when the named file is a symbolic link,
66 in which case
67 .Fn lstat
68 returns information about the link,
69 while
70 .Fn stat
71 returns information about the file the link references.
72 .Pp
73 The
74 .Fn fstat
75 system call obtains the same information about an open file
76 known by the file descriptor
77 .Fa fd .
78 .Pp
79 The
80 .Fn fstatat
81 system call is equivalent to
82 .Fn stat
83 and
84 .Fn lstat
85 except when the
86 .Fa path
87 specifies a relative path, or the
88 .Dv AT_BENEATH
89 flag is provided.
90 For
91 .Fn fstatat
92 and relative
93 .Fa path ,
94 the status is retrieved from a file relative to
95 the directory associated with the file descriptor
96 .Fa fd
97 instead of the current working directory.
98 For
99 .Dv AT_BENEATH
100 and absolute
101 .Fa path ,
102 the status is retrieved from a file specified by the
103 .Fa path ,
104 but additional permission checks are performed, see below.
105 .Pp
106 The values for the
107 .Fa flag
108 are constructed by a bitwise-inclusive OR of flags from this list,
109 defined in
110 .In fcntl.h :
111 .Bl -tag -width indent
112 .It Dv AT_SYMLINK_NOFOLLOW
113 If
114 .Fa path
115 names a symbolic link, the status of the symbolic link is returned.
116 .It Dv AT_BENEATH
117 Only stat files and directories below the topping directory.
118 See the description of the
119 .Dv O_BENEATH
120 flag in the
121 .Xr open 2
122 manual page.
123 .El
124 .Pp
125 If
126 .Fn fstatat
127 is passed the special value
128 .Dv AT_FDCWD
129 in the
130 .Fa fd
131 parameter, the current working directory is used and the behavior is
132 identical to a call to
133 .Fn stat
134 or
135 .Fn lstat
136 respectively, depending on whether or not the
137 .Dv AT_SYMLINK_NOFOLLOW
138 bit is set in
139 .Fa flag .
140 .Pp
141 When
142 .Fn fstatat
143 is called with an absolute
144 .Fa path
145 without the
146 .Dv AT_BENEATH
147 flag, it ignores the
148 .Fa fd
149 argument.
150 When
151 .Dv AT_BENEATH
152 is specified with an absolute
153 .Fa path ,
154 a directory passed by the
155 .Fa fd
156 argument is used as the topping point for the resolution.
157 .Pp
158 The
159 .Fa sb
160 argument is a pointer to a
161 .Vt stat
162 structure
163 as defined by
164 .In sys/stat.h
165 and into which information is placed concerning the file.
166 .Pp
167 The fields of
168 .Vt "struct stat"
169 related to the file system are:
170 .Bl -tag -width ".Va st_nlink"
171 .It Va st_dev
172 Numeric ID of the device containing the file.
173 .It Va st_ino
174 The file's inode number.
175 .It Va st_nlink
176 Number of hard links to the file.
177 .It Va st_flags
178 Flags enabled for the file.
179 See
180 .Xr chflags 2
181 for the list of flags and their description.
182 .El
183 .Pp
184 The
185 .Va st_dev
186 and
187 .Va st_ino
188 fields together identify the file uniquely within the system.
189 .Pp
190 The time-related fields of
191 .Vt "struct stat"
192 are:
193 .Bl -tag -width ".Va st_birthtim"
194 .It Va st_atim
195 Time when file data was last accessed.
196 Changed by the
197 .Xr mknod 2 ,
198 .Xr utimes 2 ,
199 .Xr read 2
200 and
201 .Xr readv 2
202 system calls.
203 .It Va st_mtim
204 Time when file data was last modified.
205 Changed by the
206 .Xr mkdir 2 ,
207 .Xr mkfifo 2 ,
208 .Xr mknod 2 ,
209 .Xr utimes 2 ,
210 .Xr write 2
211 and
212 .Xr writev 2
213 system calls.
214 .It Va st_ctim
215 Time when file status was last changed (inode data modification).
216 Changed by the
217 .Xr chflags 2 ,
218 .Xr chmod 2 ,
219 .Xr chown 2 ,
220 .Xr creat 2 ,
221 .Xr link 2 ,
222 .Xr mkdir 2 ,
223 .Xr mkfifo 2 ,
224 .Xr mknod 2 ,
225 .Xr rename 2 ,
226 .Xr rmdir 2 ,
227 .Xr symlink 2 ,
228 .Xr truncate 2 ,
229 .Xr unlink 2 ,
230 .Xr utimes 2 ,
231 .Xr write 2
232 and
233 .Xr writev 2
234 system calls.
235 .It Va st_birthtim
236 Time when the inode was created.
237 .El
238 .Pp
239 These time-related macros are defined for compatibility:
240 .Bd -literal
241 #define st_atime                st_atim.tv_sec
242 #define st_mtime                st_mtim.tv_sec
243 #define st_ctime                st_ctim.tv_sec
244 #ifndef _POSIX_SOURCE
245 #define st_birthtime            st_birthtim.tv_sec
246 #endif
247
248 #ifndef _POSIX_SOURCE
249 #define st_atimespec            st_atim
250 #define st_mtimespec            st_mtim
251 #define st_ctimespec            st_ctim
252 #define st_birthtimespec        st_birthtim
253 #endif
254 .Ed
255 .Pp
256 Size-related fields of the
257 .Vt "struct stat"
258 are:
259 .Bl -tag -width ".Va st_blksize"
260 .It Va st_size
261 File size in bytes.
262 .It Va st_blksize
263 Optimal I/O block size for the file.
264 .It Va st_blocks
265 Actual number of blocks allocated for the file in 512-byte units.
266 As short symbolic links are stored in the inode, this number may
267 be zero.
268 .El
269 .Pp
270 The access-related fields of
271 .Vt "struct stat"
272 are:
273 .Bl -tag -width ".Va st_mode"
274 .It Va st_uid
275 User ID of the file's owner.
276 .It Va st_gid
277 Group ID of the file.
278 .It Va st_mode
279 Status of the file (see below).
280 .El
281 .Pp
282 The status information word
283 .Fa st_mode
284 has these bits:
285 .Bd -literal
286 #define S_IFMT   0170000  /* type of file mask */
287 #define S_IFIFO  0010000  /* named pipe (fifo) */
288 #define S_IFCHR  0020000  /* character special */
289 #define S_IFDIR  0040000  /* directory */
290 #define S_IFBLK  0060000  /* block special */
291 #define S_IFREG  0100000  /* regular */
292 #define S_IFLNK  0120000  /* symbolic link */
293 #define S_IFSOCK 0140000  /* socket */
294 #define S_IFWHT  0160000  /* whiteout */
295 #define S_ISUID  0004000  /* set user id on execution */
296 #define S_ISGID  0002000  /* set group id on execution */
297 #define S_ISVTX  0001000  /* save swapped text even after use */
298 #define S_IRWXU  0000700  /* RWX mask for owner */
299 #define S_IRUSR  0000400  /* read permission, owner */
300 #define S_IWUSR  0000200  /* write permission, owner */
301 #define S_IXUSR  0000100  /* execute/search permission, owner */
302 #define S_IRWXG  0000070  /* RWX mask for group */
303 #define S_IRGRP  0000040  /* read permission, group */
304 #define S_IWGRP  0000020  /* write permission, group */
305 #define S_IXGRP  0000010  /* execute/search permission, group */
306 #define S_IRWXO  0000007  /* RWX mask for other */
307 #define S_IROTH  0000004  /* read permission, other */
308 #define S_IWOTH  0000002  /* write permission, other */
309 #define S_IXOTH  0000001  /* execute/search permission, other */
310 .Ed
311 .Pp
312 For a list of access modes, see
313 .In sys/stat.h ,
314 .Xr access 2
315 and
316 .Xr chmod 2 .
317 These macros are available to test whether a
318 .Va st_mode
319 value passed in the
320 .Fa m
321 argument corresponds to a file of the specified type:
322 .Bl -tag -width ".Fn S_ISFIFO m"
323 .It Fn S_ISBLK m
324 Test for a block special file.
325 .It Fn S_ISCHR m
326 Test for a character special file.
327 .It Fn S_ISDIR m
328 Test for a directory.
329 .It Fn S_ISFIFO m
330 Test for a pipe or FIFO special file.
331 .It Fn S_ISLNK m
332 Test for a symbolic link.
333 .It Fn S_ISREG m
334 Test for a regular file.
335 .It Fn S_ISSOCK m
336 Test for a socket.
337 .It Fn S_ISWHT m
338 Test for a whiteout.
339 .El
340 .Pp
341 The macros evaluate to a non-zero value if the test is true
342 or to the value 0 if the test is false.
343 .Sh RETURN VALUES
344 .Rv -std
345 .Sh COMPATIBILITY
346 Previous versions of the system used different types for the
347 .Va st_dev ,
348 .Va st_uid ,
349 .Va st_gid ,
350 .Va st_rdev ,
351 .Va st_size ,
352 .Va st_blksize
353 and
354 .Va st_blocks
355 fields.
356 .Sh ERRORS
357 The
358 .Fn stat
359 and
360 .Fn lstat
361 system calls will fail if:
362 .Bl -tag -width Er
363 .It Bq Er EACCES
364 Search permission is denied for a component of the path prefix.
365 .It Bq Er EFAULT
366 The
367 .Fa sb
368 or
369 .Fa path
370 argument
371 points to an invalid address.
372 .It Bq Er EIO
373 An I/O error occurred while reading from or writing to the file system.
374 .It Bq Er ELOOP
375 Too many symbolic links were encountered in translating the pathname.
376 .It Bq Er ENAMETOOLONG
377 A component of a pathname exceeded 255 characters,
378 or an entire path name exceeded 1023 characters.
379 .It Bq Er ENOENT
380 The named file does not exist.
381 .It Bq Er ENOTDIR
382 A component of the path prefix is not a directory.
383 .It Bq Er EOVERFLOW
384 The file size in bytes cannot be
385 represented correctly in the structure pointed to by
386 .Fa sb .
387 .El
388 .Pp
389 The
390 .Fn fstat
391 system call will fail if:
392 .Bl -tag -width Er
393 .It Bq Er EBADF
394 The
395 .Fa fd
396 argument
397 is not a valid open file descriptor.
398 .It Bq Er EFAULT
399 The
400 .Fa sb
401 argument
402 points to an invalid address.
403 .It Bq Er EIO
404 An I/O error occurred while reading from or writing to the file system.
405 .It Bq Er EOVERFLOW
406 The file size in bytes cannot be
407 represented correctly in the structure pointed to by
408 .Fa sb .
409 .El
410 .Pp
411 In addition to the errors returned by the
412 .Fn lstat ,
413 the
414 .Fn fstatat
415 may fail if:
416 .Bl -tag -width Er
417 .It Bq Er EBADF
418 The
419 .Fa path
420 argument does not specify an absolute path and the
421 .Fa fd
422 argument is neither
423 .Dv AT_FDCWD
424 nor a valid file descriptor open for searching.
425 .It Bq Er EINVAL
426 The value of the
427 .Fa flag
428 argument is not valid.
429 .It Bq Er ENOTDIR
430 The
431 .Fa path
432 argument is not an absolute path and
433 .Fa fd
434 is neither
435 .Dv AT_FDCWD
436 nor a file descriptor associated with a directory.
437 .It Bq Er ENOTCAPABLE
438 .Fa path
439 is an absolute path,
440 or contained a ".." component leading to a
441 directory outside of the directory hierarchy specified by
442 .Fa fd ,
443 and the process is in capability mode.
444 .It Bq Er ENOTCAPABLE
445 The
446 .Dv AT_BENEATH
447 flag was provided to
448 .Fn fstatat ,
449 and the absolute
450 .Fa path
451 does not have its tail fully contained under the topping directory,
452 or the relative
453 .Fa path
454 escapes it.
455 .El
456 .Sh SEE ALSO
457 .Xr access 2 ,
458 .Xr chmod 2 ,
459 .Xr chown 2 ,
460 .Xr fhstat 2 ,
461 .Xr statfs 2 ,
462 .Xr utimes 2 ,
463 .Xr sticky 7 ,
464 .Xr symlink 7
465 .Sh STANDARDS
466 The
467 .Fn stat
468 and
469 .Fn fstat
470 system calls are expected to conform to
471 .St -p1003.1-90 .
472 The
473 .Fn fstatat
474 system call follows The Open Group Extended API Set 2 specification.
475 .Sh HISTORY
476 The
477 .Fn stat
478 and
479 .Fn fstat
480 system calls appeared in
481 .At v1 .
482 The
483 .Fn lstat
484 system call appeared in
485 .Bx 4.2 .
486 The
487 .Fn fstatat
488 system call appeared in
489 .Fx 8.0 .
490 .Sh BUGS
491 Applying
492 .Fn fstat
493 to a socket
494 returns a zeroed buffer,
495 except for the blocksize field,
496 and a unique device and inode number.