xref: /freebsd-13.1/lib/libc/sys/stat.2 (revision ea22b5bd)
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
53The
54.Fn stat
55system call obtains information about the file pointed to by
56.Fa path .
57Read, write or execute
58permission of the named file is not required, but all directories
59listed in the path name leading to the file must be searchable.
60.Pp
61The
62.Fn lstat
63system call is like
64.Fn stat
65except when the named file is a symbolic link,
66in which case
67.Fn lstat
68returns information about the link,
69while
70.Fn stat
71returns information about the file the link references.
72.Pp
73The
74.Fn fstat
75system call obtains the same information about an open file
76known by the file descriptor
77.Fa fd .
78.Pp
79The
80.Fn fstatat
81system call is equivalent to
82.Fn stat
83and
84.Fn lstat
85except when the
86.Fa path
87specifies a relative path, or the
88.Dv AT_BENEATH
89flag is provided.
90For
91.Fn fstatat
92and relative
93.Fa path ,
94the status is retrieved from a file relative to
95the directory associated with the file descriptor
96.Fa fd
97instead of the current working directory.
98For
99.Dv AT_BENEATH
100and absolute
101.Fa path ,
102the status is retrieved from a file specified by the
103.Fa path ,
104but additional permission checks are performed, see below.
105.Pp
106The values for the
107.Fa flag
108are constructed by a bitwise-inclusive OR of flags from this list,
109defined in
110.In fcntl.h :
111.Bl -tag -width indent
112.It Dv AT_SYMLINK_NOFOLLOW
113If
114.Fa path
115names a symbolic link, the status of the symbolic link is returned.
116.It Dv AT_BENEATH
117Only stat files and directories below the topping directory.
118See the description of the
119.Dv O_BENEATH
120flag in the
121.Xr open 2
122manual page.
123.El
124.Pp
125If
126.Fn fstatat
127is passed the special value
128.Dv AT_FDCWD
129in the
130.Fa fd
131parameter, the current working directory is used and the behavior is
132identical to a call to
133.Fn stat
134or
135.Fn lstat
136respectively, depending on whether or not the
137.Dv AT_SYMLINK_NOFOLLOW
138bit is set in
139.Fa flag .
140.Pp
141When
142.Fn fstatat
143is called with an absolute
144.Fa path
145without the
146.Dv AT_BENEATH
147flag, it ignores the
148.Fa fd
149argument.
150When
151.Dv AT_BENEATH
152is specified with an absolute
153.Fa path ,
154a directory passed by the
155.Fa fd
156argument is used as the topping point for the resolution.
157.Pp
158The
159.Fa sb
160argument is a pointer to a
161.Vt stat
162structure
163as defined by
164.In sys/stat.h
165and into which information is placed concerning the file.
166.Pp
167The fields of
168.Vt "struct stat"
169related to the file system are:
170.Bl -tag -width ".Va st_nlink"
171.It Va st_dev
172Numeric ID of the device containing the file.
173.It Va st_ino
174The file's inode number.
175.It Va st_nlink
176Number of hard links to the file.
177.It Va st_flags
178Flags enabled for the file.
179See
180.Xr chflags 2
181for the list of flags and their description.
182.El
183.Pp
184The
185.Va st_dev
186and
187.Va st_ino
188fields together identify the file uniquely within the system.
189.Pp
190The time-related fields of
191.Vt "struct stat"
192are:
193.Bl -tag -width ".Va st_birthtim"
194.It Va st_atim
195Time when file data was last accessed.
196Changed by the
197.Xr mknod 2 ,
198.Xr utimes 2 ,
199.Xr read 2
200and
201.Xr readv 2
202system calls.
203.It Va st_mtim
204Time when file data was last modified.
205Changed by the
206.Xr mkdir 2 ,
207.Xr mkfifo 2 ,
208.Xr mknod 2 ,
209.Xr utimes 2 ,
210.Xr write 2
211and
212.Xr writev 2
213system calls.
214.It Va st_ctim
215Time when file status was last changed (inode data modification).
216Changed 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
232and
233.Xr writev 2
234system calls.
235.It Va st_birthtim
236Time when the inode was created.
237.El
238.Pp
239These 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
256Size-related fields of the
257.Vt "struct stat"
258are:
259.Bl -tag -width ".Va st_blksize"
260.It Va st_size
261File size in bytes.
262.It Va st_blksize
263Optimal I/O block size for the file.
264.It Va st_blocks
265Actual number of blocks allocated for the file in 512-byte units.
266As short symbolic links are stored in the inode, this number may
267be zero.
268.El
269.Pp
270The access-related fields of
271.Vt "struct stat"
272are:
273.Bl -tag -width ".Va st_mode"
274.It Va st_uid
275User ID of the file's owner.
276.It Va st_gid
277Group ID of the file.
278.It Va st_mode
279Status of the file (see below).
280.El
281.Pp
282The status information word
283.Fa st_mode
284has 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
312For a list of access modes, see
313.In sys/stat.h ,
314.Xr access 2
315and
316.Xr chmod 2 .
317These macros are available to test whether a
318.Va st_mode
319value passed in the
320.Fa m
321argument corresponds to a file of the specified type:
322.Bl -tag -width ".Fn S_ISFIFO m"
323.It Fn S_ISBLK m
324Test for a block special file.
325.It Fn S_ISCHR m
326Test for a character special file.
327.It Fn S_ISDIR m
328Test for a directory.
329.It Fn S_ISFIFO m
330Test for a pipe or FIFO special file.
331.It Fn S_ISLNK m
332Test for a symbolic link.
333.It Fn S_ISREG m
334Test for a regular file.
335.It Fn S_ISSOCK m
336Test for a socket.
337.It Fn S_ISWHT m
338Test for a whiteout.
339.El
340.Pp
341The macros evaluate to a non-zero value if the test is true
342or to the value 0 if the test is false.
343.Sh RETURN VALUES
344.Rv -std
345.Sh COMPATIBILITY
346Previous 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
353and
354.Va st_blocks
355fields.
356.Sh ERRORS
357The
358.Fn stat
359and
360.Fn lstat
361system calls will fail if:
362.Bl -tag -width Er
363.It Bq Er EACCES
364Search permission is denied for a component of the path prefix.
365.It Bq Er EFAULT
366The
367.Fa sb
368or
369.Fa path
370argument
371points to an invalid address.
372.It Bq Er EIO
373An I/O error occurred while reading from or writing to the file system.
374.It Bq Er ELOOP
375Too many symbolic links were encountered in translating the pathname.
376.It Bq Er ENAMETOOLONG
377A component of a pathname exceeded 255 characters,
378or an entire path name exceeded 1023 characters.
379.It Bq Er ENOENT
380The named file does not exist.
381.It Bq Er ENOTDIR
382A component of the path prefix is not a directory.
383.It Bq Er EOVERFLOW
384The file size in bytes cannot be
385represented correctly in the structure pointed to by
386.Fa sb .
387.El
388.Pp
389The
390.Fn fstat
391system call will fail if:
392.Bl -tag -width Er
393.It Bq Er EBADF
394The
395.Fa fd
396argument
397is not a valid open file descriptor.
398.It Bq Er EFAULT
399The
400.Fa sb
401argument
402points to an invalid address.
403.It Bq Er EIO
404An I/O error occurred while reading from or writing to the file system.
405.It Bq Er EOVERFLOW
406The file size in bytes cannot be
407represented correctly in the structure pointed to by
408.Fa sb .
409.El
410.Pp
411In addition to the errors returned by the
412.Fn lstat ,
413the
414.Fn fstatat
415may fail if:
416.Bl -tag -width Er
417.It Bq Er EBADF
418The
419.Fa path
420argument does not specify an absolute path and the
421.Fa fd
422argument is neither
423.Dv AT_FDCWD
424nor a valid file descriptor open for searching.
425.It Bq Er EINVAL
426The value of the
427.Fa flag
428argument is not valid.
429.It Bq Er ENOTDIR
430The
431.Fa path
432argument is not an absolute path and
433.Fa fd
434is neither
435.Dv AT_FDCWD
436nor a file descriptor associated with a directory.
437.It Bq Er ENOTCAPABLE
438.Fa path
439is an absolute path,
440or contained a ".." component leading to a
441directory outside of the directory hierarchy specified by
442.Fa fd ,
443and the process is in capability mode.
444.It Bq Er ENOTCAPABLE
445The
446.Dv AT_BENEATH
447flag was provided to
448.Fn fstatat ,
449and the absolute
450.Fa path
451does not have its tail fully contained under the topping directory,
452or the relative
453.Fa path
454escapes 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
466The
467.Fn stat
468and
469.Fn fstat
470system calls are expected to conform to
471.St -p1003.1-90 .
472The
473.Fn fstatat
474system call follows The Open Group Extended API Set 2 specification.
475.Sh HISTORY
476The
477.Fn stat
478and
479.Fn fstat
480system calls appeared in
481.At v1 .
482The
483.Fn lstat
484system call appeared in
485.Bx 4.2 .
486The
487.Fn fstatat
488system call appeared in
489.Fx 8.0 .
490.Sh BUGS
491Applying
492.Fn fstat
493to a socket
494returns a zeroed buffer,
495except for the blocksize field,
496and a unique device and inode number.
497