]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/stat.2
stat.2: Add note about stat() being racy
[thirdparty/man-pages.git] / man2 / stat.2
CommitLineData
fea681da 1'\" t
fea681da
MK
2.\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
3.\" Parts Copyright (c) 1995 Nicolai Langfeldt (janl@ifi.uio.no), 1/1/95
40084043 4.\" and Copyright (c) 2006, 2007, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 5.\"
93015253 6.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 15.\"
fea681da
MK
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.
c13182ef 23.\"
fea681da
MK
24.\" Formatted or processed versions of this manual, if unaccompanied by
25.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 26.\" %%%LICENSE_END
fea681da
MK
27.\"
28.\" Modified by Michael Haardt <michael@moria.de>
29.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
30.\" Modified 1995-05-18 by Todd Larason <jtl@molehill.org>
31.\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
32.\" Modified 1995-01-09 by Richard Kettlewell <richard@greenend.org.uk>
33.\" Modified 1998-05-13 by Michael Haardt <michael@cantor.informatik.rwth-aachen.de>
34.\" Modified 1999-07-06 by aeb & Albert Cahalan
35.\" Modified 2000-01-07 by aeb
c11b1abf 36.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
bc7ff20e 37.\" 2007-06-08 mtk: Added example program
cd667553 38.\" 2007-07-05 mtk: Added details on underlying system call interfaces
c13182ef 39.\"
c73595c2 40.TH STAT 2 2015-04-19 "Linux" "Linux Programmer's Manual"
fea681da 41.SH NAME
40084043 42stat, fstat, lstat, fstatat \- get file status
fea681da 43.SH SYNOPSIS
40084043 44.nf
fea681da
MK
45.B #include <sys/types.h>
46.br
47.B #include <sys/stat.h>
48.br
49.B #include <unistd.h>
50.sp
66cbeaf4 51.BI "int stat(const char *" pathname ", struct stat *" buf );
fea681da 52.br
d3b03141 53.BI "int fstat(int " fd ", struct stat *" buf );
fea681da 54.br
66cbeaf4 55.BI "int lstat(const char *" pathname ", struct stat *" buf );
8179def1 56.sp
40084043
MK
57.BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
58.B #include <sys/stat.h>
59.sp
60.BI "int fstatat(int " dirfd ", const char *" pathname ", struct stat *" \
61buf ,
62.BI " int " flags );
63.fi
64.sp
8179def1
MK
65.in -4n
66Feature Test Macro Requirements for glibc (see
67.BR feature_test_macros (7)):
68.in
43e0fe7d
MK
69.ad l
70.PD 0
8179def1
MK
71.sp
72.BR lstat ():
43e0fe7d 73.RS 4
afb9bd5d
MK
74/* glibc 2.19 and earlier */ _BSD_SOURCE ||
75.br
76/* Since glibc 2.20 */_DEFAULT_SOURCE ||
77.br
78_XOPEN_SOURCE\ >=\ 500 ||
43e0fe7d 79_XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
3ba63d80
MK
80.br
81|| /* Since glibc 2.10: */ _POSIX_C_SOURCE\ >=\ 200112L
43e0fe7d 82.RE
40084043
MK
83.sp
84.BR fstatat ():
85.PD 0
86.ad l
87.RS 4
88.TP 4
89Since glibc 2.10:
90_XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
91.TP
92Before glibc 2.10:
93_ATFILE_SOURCE
94.RE
43e0fe7d
MK
95.PD
96.ad
fea681da
MK
97.SH DESCRIPTION
98.PP
e096bd70
MK
99These functions return information about a file, in the buffer pointed to by
100.IR stat .
5503c85e 101No permissions are required on the file itself, but\(emin the case of
071d1c4f
MK
102.BR stat (),
103.BR fstatat (),
c13182ef 104and
827edbd2
MK
105.BR lstat ()\(emexecute
106(search) permission is required on all of the directories in
66cbeaf4 107.I pathname
da2d9dad 108that lead to the file.
fea681da 109.PP
da2d9dad 110.BR stat ()
071d1c4f
MK
111and
112.BR fstatat ()
e096bd70
MK
113retrieve information about the file pointed to by
114.IR pathname ;
071d1c4f
MK
115the differences for
116.BR fstatat ()
117are described below.
fea681da 118
da2d9dad 119.BR lstat ()
fea681da 120is identical to
da2d9dad
MK
121.BR stat (),
122except that if
66cbeaf4 123.I pathname
e096bd70 124is a symbolic link, then it returns information about the link itself,
fea681da
MK
125not the file that it refers to.
126
da2d9dad 127.BR fstat ()
fea681da 128is identical to
da2d9dad 129.BR stat (),
5201bb40 130except that the file about which information is to be retrieved
e096bd70 131is specified by the file descriptor
d3b03141 132.IR fd .
fea681da 133.PP
da2d9dad 134All of these system calls return a
fea681da
MK
135.I stat
136structure, which contains the following fields:
137.PP
bd191423 138.in +4n
fea681da
MK
139.nf
140struct stat {
e9e9e87e
MK
141 dev_t st_dev; /* ID of device containing file */
142 ino_t st_ino; /* inode number */
143 mode_t st_mode; /* protection */
144 nlink_t st_nlink; /* number of hard links */
145 uid_t st_uid; /* user ID of owner */
146 gid_t st_gid; /* group ID of owner */
147 dev_t st_rdev; /* device ID (if special file) */
148 off_t st_size; /* total size, in bytes */
149 blksize_t st_blksize; /* blocksize for filesystem I/O */
150 blkcnt_t st_blocks; /* number of 512B blocks allocated */
151
152 /* Since Linux 2.6, the kernel supports nanosecond
153 precision for the following timestamp fields.
154 For the details before Linux 2.6, see NOTES. */
155
156 struct timespec st_atim; /* time of last access */
157 struct timespec st_mtim; /* time of last modification */
158 struct timespec st_ctim; /* time of last status change */
159
160#define st_atime st_atim.tv_sec /* Backward compatibility */
161#define st_mtime st_mtim.tv_sec
162#define st_ctime st_ctim.tv_sec
fea681da
MK
163};
164.fi
bd191423 165.in
496bdb56
MK
166
167.I Note:
168the order of fields in the
169.I stat
170structure varies somewhat
171across architectures.
172In addition,
173the definition above does not show the padding bytes
174that may be present between some fields on various architectures.
409b0278 175Consult the glibc and kernel source code
496bdb56
MK
176if you need to know the details.
177
f240296d
JH
178\# Background: inode attributes are modified with i_mutex held, but
179\# read by stat() without taking the mutex.
180.I Note:
181For performance and simplicity reasons, different fields in the
182.I stat
183structure may contain state information from different moments
184during the execution of the syscall. For example, if
185.IR st_mode ,
186.IR st_uid
187and
188.IR st_gid
189are changed by another process by calling
190.BR chown (2) ,
191.BR stat ()
192might return the old
193.I st_mode
194together with the new
195.IR st_uid ,
196or the old
197.I st_uid
198together with the new
199.IR st_mode .
200
da2d9dad 201The
29de83af 202.I st_dev
da2d9dad 203field describes the device on which this file resides.
e9ef777f
MK
204(The
205.BR major (3)
206and
207.BR minor (3)
208macros may be useful to decompose the device ID in this field.)
da2d9dad
MK
209
210The
211.I st_rdev
212field describes the device that this file (inode) represents.
213
214The
fea681da 215.I st_size
c13182ef
MK
216field gives the size of the file (if it is a regular
217file or a symbolic link) in bytes.
280ff209
MK
218The size of a symbolic link is the length of the pathname
219it contains, without a terminating null byte.
fea681da 220
da2d9dad 221The
fea681da 222.I st_blocks
32f30015 223field indicates the number of blocks allocated to the file, 512-byte units.
fea681da 224(This may be smaller than
eee0a2ec
MK
225.IR st_size /512
226when the file has holes.)
da2d9dad
MK
227
228The
0daa9e92 229.I st_blksize
9ee4a2b6 230field gives the "preferred" blocksize for efficient filesystem I/O.
fea681da
MK
231(Writing to a file in smaller chunks may cause
232an inefficient read-modify-rewrite.)
233.PP
9ee4a2b6
MK
234Not all of the Linux filesystems implement all of the time fields.
235Some filesystem types allow mounting in such a way that file
86d89e4c 236and/or directory accesses do not cause an update of the
fea681da 237.I st_atime
c13182ef 238field.
86d89e4c
MK
239(See
240.IR noatime ,
241.IR nodiratime ,
242and
243.I relatime
244in
245.BR mount (8),
246and related information in
247.BR mount (2).)
14adeb6d
MK
248In addition,
249.I st_atime
250is not updated if a file is opened with the
251.BR O_NOATIME ;
252see
253.BR open (2).
fea681da
MK
254
255The field
256.I st_atime
75b94dc3 257is changed by file accesses, for example, by
fea681da
MK
258.BR execve (2),
259.BR mknod (2),
260.BR pipe (2),
9af134cd 261.BR utime (2),
fea681da
MK
262and
263.BR read (2)
7c93fec0
MK
264(of more than zero bytes).
265Other routines, like
fea681da
MK
266.BR mmap (2),
267may or may not update
268.IR st_atime .
269
270The field
271.I st_mtime
75b94dc3 272is changed by file modifications, for example, by
fea681da
MK
273.BR mknod (2),
274.BR truncate (2),
9af134cd 275.BR utime (2),
fea681da
MK
276and
277.BR write (2)
278(of more than zero bytes).
279Moreover,
280.I st_mtime
281of a directory is changed by the creation or deletion of files
282in that directory.
283The
284.I st_mtime
285field is
286.I not
287changed for changes in owner, group, hard link count, or mode.
288
289The field
290.I st_ctime
291is changed by writing or by setting inode information
292(i.e., owner, group, link count, mode, etc.).
293.PP
ff3976ce
MK
294The following mask values are defined for the file type component of the
295.I st_mode
296field:
297.in +4n
298.TS
299lB l l.
300S_IFMT 0170000 bit mask for the file type bit fields
301
302S_IFSOCK 0140000 socket
303S_IFLNK 0120000 symbolic link
304S_IFREG 0100000 regular file
305S_IFBLK 0060000 block device
306S_IFDIR 0040000 directory
307S_IFCHR 0020000 character device
308S_IFIFO 0010000 FIFO
309.TE
310.in
311.PP
3779bbef
MK
312Thus, to test for a regular file (for example), one could write:
313
314.nf
315.in +4n
316stat(pathname, &sb);
317if ((sb.st_mode & S_IFMT) == S_IFREG) {
318 /* Handle regular file */
319}
320.in
321.fi
322.PP
323Because tests of the above form are common, additional
324macros are defined by POSIX to allow the test of the file type in
168df940 325.I st_mode
3779bbef 326to be written more concisely:
bd191423 327.RS 4
fea681da 328.TP 1.2i
c91e381d 329.BR S_ISREG (m)
fea681da
MK
330is it a regular file?
331.TP
c91e381d 332.BR S_ISDIR (m)
fea681da
MK
333directory?
334.TP
c91e381d 335.BR S_ISCHR (m)
fea681da
MK
336character device?
337.TP
c91e381d 338.BR S_ISBLK (m)
fea681da
MK
339block device?
340.TP
c91e381d 341.BR S_ISFIFO (m)
da2d9dad 342FIFO (named pipe)?
fea681da 343.TP
c91e381d 344.BR S_ISLNK (m)
77b99918 345symbolic link? (Not in POSIX.1-1996.)
fea681da 346.TP
c91e381d 347.BR S_ISSOCK (m)
77b99918 348socket? (Not in POSIX.1-1996.)
fea681da
MK
349.RE
350.PP
3779bbef
MK
351The preceding code snippet could thus be rewritten as:
352
353.nf
354.in +4n
355stat(pathname, &sb);
356if (S_ISREG(sb.st_mode)) {
357 /* Handle regular file */
358}
359.in
360.fi
361.PP
362The definitions of most of the above file type test macros
cc2b44eb 363are provided if any of the following feature test macros is defined:
3779bbef
MK
364.BR _BSD_SOURCE
365(in glibc 2.19 and earlier),
366.BR _SVID_SOURCE
367(in glibc 2.19 and earlier),
368or
369.BR _DEFAULT_SOURCE
370(in glibc 2.20 and later).
371In addition, definitions of all of the above macros except
372.BR S_IFSOCK
373and
374.BR S_ISSOCK ()
375are provided if
376.BR _XOPEN_SOURCE
377is defined.
378The definition of
379.BR S_IFSOCK
380can also be exposed by defining
381.BR _XOPEN_SOURCE
382with a value of 500 or greater.
383
384The definition of
385.BR S_ISSOCK ()
386is exposed if any of the following feature test macros is defined:
387.BR _BSD_SOURCE
388(in glibc 2.19 and earlier),
389.BR _DEFAULT_SOURCE
390(in glibc 2.20 and later),
391.BR _XOPEN_SOURCE
392with a value of 500 or greater, or
393.BR _POSIX_C_SOURCE
394with a value of 200112L or greater.
395.PP
ff0afc3f 396The following mask values are defined for
7901c9c3 397the file mode component of the
ff0afc3f
MK
398.I st_mode
399field:
400.in +4n
401.TS
402lB l l.
0d24f47f 403S_ISUID 0004000 set-user-ID bit
da2d9dad 404S_ISGID 0002000 set-group-ID bit (see below)
fea681da 405S_ISVTX 0001000 sticky bit (see below)
ff0afc3f 406
b0c5363f 407S_IRWXU 00700 owner has read, write and execute permission
51c6aa6e
MK
408S_IRUSR 00400 owner has read permission
409S_IWUSR 00200 owner has write permission
410S_IXUSR 00100 owner has execute permission
ff0afc3f 411
b0c5363f 412S_IRWXG 00070 group has read, write and execute permission
51c6aa6e
MK
413S_IRGRP 00040 group has read permission
414S_IWGRP 00020 group has write permission
415S_IXGRP 00010 group has execute permission
ff0afc3f 416
51c6aa6e 417S_IRWXO 00007 T{
b0c5363f 418others (not in group) have read, write and execute permission
a211773a 419T}
51c6aa6e
MK
420S_IROTH 00004 others have read permission
421S_IWOTH 00002 others have write permission
422S_IXOTH 00001 others have execute permission
fea681da 423.TE
bd191423 424.in
fea681da 425.P
2f0af33b
MK
426The set-group-ID bit
427.RB ( S_ISGID )
428has several special uses.
5c3122b1 429For a directory, it indicates that BSD semantics is to be used
fea681da
MK
430for that directory: files created there inherit their group ID from
431the directory, not from the effective group ID of the creating process,
2f0af33b
MK
432and directories created there will also get the
433.B S_ISGID
434bit set.
66ee0c7e 435For a file that does not have the group execution bit
2f0af33b
MK
436.RB ( S_IXGRP )
437set,
da2d9dad 438the set-group-ID bit indicates mandatory file/record locking.
fea681da 439.P
2d986c92 440The sticky bit
2f0af33b
MK
441.RB ( S_ISVTX )
442on a directory means that a file
fea681da
MK
443in that directory can be renamed or deleted only by the owner
444of the file, by the owner of the directory, and by a privileged
445process.
40084043
MK
446.\"
447.\"
1fef0fa3 448.SS fstatat()
40084043
MK
449The
450.BR fstatat ()
451system call operates in exactly the same way as
cadd38ba 452.BR stat (),
40084043
MK
453except for the differences described here.
454
455If the pathname given in
456.I pathname
457is relative, then it is interpreted relative to the directory
458referred to by the file descriptor
459.I dirfd
460(rather than relative to the current working directory of
461the calling process, as is done by
cadd38ba 462.BR stat ()
40084043
MK
463for a relative pathname).
464
465If
466.I pathname
467is relative and
468.I dirfd
469is the special value
470.BR AT_FDCWD ,
471then
472.I pathname
473is interpreted relative to the current working
474directory of the calling process (like
cadd38ba 475.BR stat ()).
40084043
MK
476
477If
478.I pathname
479is absolute, then
480.I dirfd
481is ignored.
482
483.I flags
484can either be 0, or include one or more of the following flags ORed:
485.TP
486.BR AT_EMPTY_PATH " (since Linux 2.6.39)"
487.\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
488If
489.I pathname
490is an empty string, operate on the file referred to by
491.IR dirfd
492(which may have been obtained using the
493.BR open (2)
494.B O_PATH
495flag).
a6fcbdf8
MK
496If
497.I dirfd
498is
499.BR AT_FDCWD ,
500the call operates on the current working directory.
40084043
MK
501In this case,
502.I dirfd
503can refer to any type of file, not just a directory.
504This flag is Linux-specific; define
505.B _GNU_SOURCE
506.\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
507to obtain its definition.
508.TP
509.BR AT_NO_AUTOMOUNT " (since Linux 2.6.38)"
510Don't automount the terminal ("basename") component of
511.I pathname
512if it is a directory that is an automount point.
513This allows the caller to gather attributes of an automount point
514(rather than the location it would mount).
515This flag can be used in tools that scan directories
516to prevent mass-automounting of a directory of automount points.
517The
518.B AT_NO_AUTOMOUNT
519flag has no effect if the mount point has already been mounted over.
520This flag is Linux-specific; define
521.B _GNU_SOURCE
522.\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
523to obtain its definition.
524.TP
525.B AT_SYMLINK_NOFOLLOW
526If
527.I pathname
528is a symbolic link, do not dereference it:
529instead return information about the link itself, like
cadd38ba 530.BR lstat ().
40084043
MK
531(By default,
532.BR fstatat ()
533dereferences symbolic links, like
cadd38ba 534.BR stat ().)
40084043
MK
535.PP
536See
537.BR openat (2)
538for an explanation of the need for
539.BR fstatat ().
47297adb 540.SH RETURN VALUE
c13182ef
MK
541On success, zero is returned.
542On error, \-1 is returned, and
fea681da
MK
543.I errno
544is set appropriately.
545.SH ERRORS
546.TP
547.B EACCES
548Search permission is denied for one of the directories
549in the path prefix of
66cbeaf4 550.IR pathname .
fea681da 551(See also
ad7cc990 552.BR path_resolution (7).)
fea681da
MK
553.TP
554.B EBADF
d3b03141 555.I fd
fea681da
MK
556is bad.
557.TP
558.B EFAULT
559Bad address.
560.TP
561.B ELOOP
562Too many symbolic links encountered while traversing the path.
563.TP
564.B ENAMETOOLONG
66cbeaf4 565.I pathname
c066b169 566is too long.
fea681da
MK
567.TP
568.B ENOENT
5cf0b3b4 569A component of
66cbeaf4 570.I pathname
5cf0b3b4 571does not exist, or
66cbeaf4 572.I pathname
5cf0b3b4 573is an empty string.
fea681da
MK
574.TP
575.B ENOMEM
75b94dc3 576Out of memory (i.e., kernel memory).
fea681da
MK
577.TP
578.B ENOTDIR
94ea1e9d 579A component of the path prefix of
66cbeaf4 580.I pathname
5cf0b3b4 581is not a directory.
9dcc4605
MK
582.TP
583.B EOVERFLOW
66cbeaf4 584.I pathname
fec75159
SP
585or
586.I fd
587refers to a file whose size, inode number,
588or number of blocks cannot be represented in, respectively, the types
589.IR off_t ,
590.IR ino_t ,
591or
592.IR blkcnt_t .
593This error can occur when, for example,
594an application compiled on a 32-bit platform without
5e4dc269 595.I -D_FILE_OFFSET_BITS=64
9dcc4605
MK
596calls
597.BR stat ()
598on a file whose size exceeds
4d69ed2e 599.I (1<<31)-1
fec75159 600bytes.
40084043 601.PP
40084043
MK
602The following additional errors can occur for
603.BR fstatat ():
604.TP
605.B EBADF
606.I dirfd
607is not a valid file descriptor.
608.TP
609.B EINVAL
610Invalid flag specified in
611.IR flags .
612.TP
613.B ENOTDIR
614.I pathname
615is relative and
616.I dirfd
617is a file descriptor referring to a file other than a directory.
618.SH VERSIONS
619.BR fstatat ()
620was added to Linux in kernel 2.6.16;
621library support was added to glibc in version 2.4.
47297adb 622.SH CONFORMING TO
40084043
MK
623.BR stat (),
624.BR fstat (),
625.BR lstat ():
282daba9 626SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
97c1eac8
MK
627.\" SVr4 documents additional
628.\" .BR fstat ()
629.\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
630.\" documents additional
631.\" .BR stat ()
632.\" and
633.\" .BR lstat ()
634.\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
635
40084043
MK
636.BR fstatat ():
637POSIX.1-2008.
40084043 638
d3e8b141
MK
639According to POSIX.1-2001,
640.BR lstat ()
641on a symbolic link need return valid information only in the
642.I st_size
643field and the file-type component of the
644.IR st_mode
645field of the
646.IR stat
647structure.
a03c016c 648POSIX.1-2008 tightens the specification, requiring
d3e8b141 649.BR lstat ()
7901c9c3 650to return valid information in all fields except the mode bits in
d3e8b141
MK
651.IR st_mode .
652
fea681da
MK
653Use of the
654.I st_blocks
655and
656.I st_blksize
c13182ef
MK
657fields may be less portable.
658(They were introduced in BSD.
659The interpretation differs between systems,
660and possibly on a single system when NFS mounts are involved.)
34c909ac
MK
661If you need to obtain the definition of the
662.IR blkcnt_t
663or
664.IR blksize_t
665types from
666.IR <sys/stat.h> ,
667then define
668.BR _XOPEN_SOURCE
669with the value 500 or greater (before including
670.I any
671header files).
fea681da 672.LP
beb0ebc8 673POSIX.1-1990 did not describe the
66ee0c7e
MK
674.BR S_IFMT ,
675.BR S_IFSOCK ,
676.BR S_IFLNK ,
677.BR S_IFREG ,
c91e381d 678.BR S_IFBLK ,
66ee0c7e
MK
679.BR S_IFDIR ,
680.BR S_IFCHR ,
681.BR S_IFIFO ,
0daa9e92 682.B S_ISVTX
beb0ebc8 683constants, but instead demanded the use of
c91e381d
MK
684the macros
685.BR S_ISDIR (),
f78ed33a 686and so on.
beb0ebc8
MK
687The
688.BR S_IF*
318c8142 689constants are present in POSIX.1-2001 and later.
beb0ebc8 690
1274071a 691The
c91e381d 692.BR S_ISLNK ()
1274071a 693and
c91e381d 694.BR S_ISSOCK ()
1274071a 695macros are not in
97c1eac8
MK
696POSIX.1-1996, but both are present in POSIX.1-2001;
697the former is from SVID 4, the latter from SUSv2.
fea681da 698.LP
b4112efb 699UNIX\ V7 (and later systems) had
66ee0c7e
MK
700.BR S_IREAD ,
701.BR S_IWRITE ,
c91e381d
MK
702.BR S_IEXEC ,
703where POSIX
66ee0c7e
MK
704prescribes the synonyms
705.BR S_IRUSR ,
706.BR S_IWUSR ,
c91e381d 707.BR S_IXUSR .
73d8cece 708.SS Other systems
fea681da 709Values that have been (or are) in use on various systems:
77b99918 710.ad l
fea681da
MK
711.TS
712l l l l l.
713hex name ls octal description
714f000 S_IFMT 170000 mask for file type
77b99918
BIG
7150000 000000 T{
716SCO out-of-service inode; BSD unknown type; SVID-v2 and XPG2
717have both 0 and 0100000 for ordinary file
718T}
da2d9dad 7191000 S_IFIFO p| 010000 FIFO (named pipe)
fea681da
MK
7202000 S_IFCHR c 020000 character special (V7)
7213000 S_IFMPC 030000 multiplexed character special (V7)
7224000 S_IFDIR d/ 040000 directory (V7)
77b99918
BIG
7235000 S_IFNAM 050000 T{
724XENIX named special file with two subtypes, distinguished by
725\fIst_rdev\fP values 1, 2
726T}
fea681da
MK
7270001 S_INSEM s 000001 XENIX semaphore subtype of IFNAM
7280002 S_INSHD m 000002 XENIX shared data subtype of IFNAM
7296000 S_IFBLK b 060000 block special (V7)
7307000 S_IFMPB 070000 multiplexed block special (V7)
7318000 S_IFREG - 100000 regular (V7)
7329000 S_IFCMP 110000 VxFS compressed
7339000 S_IFNWK n 110000 network special (HP-UX)
734a000 S_IFLNK l@ 120000 symbolic link (BSD)
77b99918 735b000 S_IFSHAD 130000 T{
7fac88a9 736Solaris shadow inode for ACL (not seen by user space)
77b99918 737T}
fea681da
MK
738c000 S_IFSOCK s= 140000 socket (BSD; also "S_IFSOC" on VxFS)
739d000 S_IFDOOR D> 150000 Solaris door
740e000 S_IFWHT w% 160000 BSD whiteout (not used for inode)
77b99918
BIG
7410200 S_ISVTX 001000 T{
742sticky bit: save swapped text even after use (V7)
743.br
744reserved (SVID-v2)
745.br
746On nondirectories: don't cache this file (SunOS)
747.br
748On directories: restricted deletion flag (SVID-v4.2)
749T}
7500400 S_ISGID 002000 T{
751set-group-ID on execution (V7)
752.br
753for directories: use BSD semantics for propagation of GID
754T}
7550400 S_ENFMT 002000 T{
756System V file locking enforcement (shared with S_ISGID)
757T}
da2d9dad 7580800 S_ISUID 004000 set-user-ID on execution (V7)
77b99918
BIG
7590800 S_CDF 004000 T{
760directory is a context dependent file (HP-UX)
761T}
fea681da 762.TE
77b99918 763.ad
fea681da
MK
764
765A sticky command appeared in Version 32V AT&T UNIX.
2b2581ee 766.SH NOTES
1ef5b280
MK
767On Linux,
768.BR lstat ()
769will generally not trigger automounter action, whereas
770.BR stat ()
771will (but see
772.BR fstatat (2)).
773
774For most files under the
775.I /proc
776directory,
777.BR stat ()
778does not return the file size in the
779.I st_size
780field; instead the field is returned with the value 0.
781.SS Timestamp fields
e9e9e87e
MK
782Older kernels and older standards did not support nanosecond timestamp
783fields.
b8efc3ed 784Instead, there were three timestamp
e9e9e87e
MK
785.RI fields\(em st_atime ,
786.IR st_mtime ,
787and
788.IR st_ctime \(emtyped
b8efc3ed 789as
e9e9e87e
MK
790.IR time_t
791that recorded timestamps with one-second precision.
1ef5b280 792
2b2581ee
MK
793Since kernel 2.5.48, the
794.I stat
f5935752 795structure supports nanosecond resolution for the three file timestamp fields.
e9e9e87e
MK
796The nanosecond components of each timestamp are available
797via names of the form
f5935752 798.IR st_atim.tv_nsec
db4e96b7
MK
799if the
800.B _BSD_SOURCE
801or
802.B _SVID_SOURCE
f5935752 803feature test macro is defined.
e9e9e87e
MK
804Nanosecond timestamps are nowadays standardized,
805starting with POSIX.1-2008, and, starting with version 2.12,
806glibc also exposes the nanosecond component names if
f5935752
MK
807.BR _POSIX_C_SOURCE
808is defined with the value 200809L or greater, or
809.BR _XOPEN_SOURCE
810is defined with the value 700 or greater.
811If none of the aforementioned macros are defined,
812then the nanosecond values are exposed with names of the form
813.IR st_atimensec .
1ef5b280 814
96f92a7c
MK
815Nanosecond timestamps are supported on XFS, JFS, Btrfs, and
816ext4 (since Linux 2.6.23).
817.\" commit ef7f38359ea8b3e9c7f2cae9a4d4935f55ca9e80
c2033cfe 818Nanosecond timestamps are not supported in ext2, ext3, and Reiserfs.
1ef5b280
MK
819On filesystems that do not support subsecond timestamps,
820the nanosecond fields are returned with the value 0.
1fbe05e0 821.SS C library/kernel ABI differences
b8f9d9dd
MK
822Over time, increases in the size of the
823.I stat
0c8dd254 824structure have led to three successive versions of
b8f9d9dd
MK
825.BR stat ():
826.IR sys_stat ()
827(slot
828.IR __NR_oldstat ),
829.IR sys_newstat ()
830(slot
831.IR __NR_stat ),
832and
0daa9e92 833.I sys_stat64()
a39f2f4d 834(slot
94971420
MK
835.IR __NR_stat64 )
836on 32-bit platforms such as i386.
837The first two versions were already present in Linux 1.0
838(albeit with different names);
cd358be3 839.\" See include/asm-i386/stat.h in the Linux 2.4 source code for the
94971420
MK
840.\" various versions of the structure definitions
841the last was added in Linux 2.4.
842Similar remarks apply for
843.BR fstat ()
844and
845.BR lstat ().
846
847The kernel-internal versions of the
848.I stat
849structure dealt with by the different versions are, respectively:
850.RS
851.TP
852.IR __old_kernel_stat
853The original structure, with rather narrow fields, and no padding.
854.TP
855.IR stat
856Larger
857.I st_ino
858field and padding added to various parts of the structure to
859allow for future expansion.
860.TP
861.IR stat64
862Even larger
863.I st_ino
864field,
865larger
866.I st_uid
867and
868.I st_gid
869fields to accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits,
870and various other enlarged fields and further padding in the structure.
871(Various padding bytes were eventually consumed in Linux 2.6,
872with the advent of 32-bit device IDs and nanosecond components
873for the timestamp fields.)
874.RE
875.PP
d28a0b77 876The glibc
b8f9d9dd 877.BR stat ()
d28a0b77 878wrapper function hides these details from applications,
6f1a1e61
MK
879invoking the most recent version of the system call provided by the kernel,
880and repacking the returned information if required for old binaries.
8179def1
MK
881.\"
882.\" A note from Andries Brouwer, July 2007
e0bf9127
MK
883.\"
884.\" > Is the story not rather more complicated for some calls like
8179def1 885.\" > stat(2)?
e0bf9127 886.\"
8179def1 887.\" Yes and no, mostly no. See /usr/include/sys/stat.h .
e0bf9127 888.\"
8179def1
MK
889.\" The idea is here not so much that syscalls change, but that
890.\" the definitions of struct stat and of the types dev_t and mode_t change.
891.\" This means that libc (even if it does not call the kernel
892.\" but only calls some internal function) must know what the
893.\" format of dev_t or of struct stat is.
894.\" The communication between the application and libc goes via
895.\" the include file <sys/stat.h> that defines a _STAT_VER and
896.\" _MKNOD_VER describing the layout of the data that user space
897.\" uses. Each (almost each) occurrence of stat() is replaced by
898.\" an occurrence of xstat() where the first parameter of xstat()
899.\" is this version number _STAT_VER.
e0bf9127 900.\"
8179def1
MK
901.\" Now, also the definitions used by the kernel change.
902.\" But glibc copes with this in the standard way, and the
903.\" struct stat as returned by the kernel is repacked into
904.\" the struct stat as expected by the application.
905.\" Thus, _STAT_VER and this setup cater for the application-libc
906.\" interface, rather than the libc-kernel interface.
e0bf9127 907.\"
8179def1 908.\" (Note that the details depend on gcc being used as c compiler.)
40084043 909
94971420
MK
910On modern 64-bit systems, life is simpler: there is a single
911.BR stat ()
912system call and the kernel deals with a
913.I stat
914structure that contains fields of a sufficient size.
915
40084043
MK
916The underlying system call employed by the glibc
917.BR fstatat ()
918wrapper function is actually called
cad6df32
MK
919.BR fstatat64 ()
920or, on some architectures,
921.\" strace(1) shows the name "newfstatat" on x86-64
922.BR newfstatat ().
bc7ff20e 923.SH EXAMPLE
988db661 924The following program calls
2777b1ca 925.BR stat ()
bc7ff20e
MK
926and displays selected fields in the returned
927.I stat
928structure.
929.nf
930
931#include <sys/types.h>
932#include <sys/stat.h>
933#include <time.h>
934#include <stdio.h>
988db661 935#include <stdlib.h>
bc7ff20e
MK
936
937int
938main(int argc, char *argv[])
939{
940 struct stat sb;
941
942 if (argc != 2) {
943 fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]);
944 exit(EXIT_FAILURE);
945 }
946
29059a65 947 if (stat(argv[1], &sb) == \-1) {
bc7ff20e 948 perror("stat");