]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/stat.2
mlock.2: tfix
[thirdparty/man-pages.git] / man2 / stat.2
1 '\" t
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
4 .\" and Copyright (c) 2006, 2007, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
6 .\" %%%LICENSE_START(VERBATIM)
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 .\" %%%LICENSE_END
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
36 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" 2007-06-08 mtk: Added example program
38 .\" 2007-07-05 mtk: Added details on underlying system call interfaces
39 .\"
40 .TH STAT 2 2017-09-15 "Linux" "Linux Programmer's Manual"
41 .SH NAME
42 stat, fstat, lstat, fstatat \- get file status
43 .SH SYNOPSIS
44 .nf
45 .B #include <sys/types.h>
46 .B #include <sys/stat.h>
47 .B #include <unistd.h>
48 .PP
49 .BI "int stat(const char *" pathname ", struct stat *" statbuf );
50 .BI "int fstat(int " fd ", struct stat *" statbuf );
51 .BI "int lstat(const char *" pathname ", struct stat *" statbuf );
52
53 .BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
54 .B #include <sys/stat.h>
55 .PP
56 .BI "int fstatat(int " dirfd ", const char *" pathname ", struct stat *" \
57 statbuf ,
58 .BI " int " flags );
59 .fi
60 .PP
61 .in -4n
62 Feature Test Macro Requirements for glibc (see
63 .BR feature_test_macros (7)):
64 .in
65 .PP
66 .ad l
67 .BR lstat ():
68 .RS 4
69 /* glibc 2.19 and earlier */ _BSD_SOURCE
70 .br
71 || /* Since glibc 2.20 */ _DEFAULT_SOURCE
72 .br
73 || _XOPEN_SOURCE\ >=\ 500
74 .\" _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
75 .br
76 || /* Since glibc 2.10: */ _POSIX_C_SOURCE\ >=\ 200112L
77 .RE
78 .PP
79 .BR fstatat ():
80 .ad l
81 .RS 4
82 .PD 0
83 .TP 4
84 Since glibc 2.10:
85 _POSIX_C_SOURCE\ >=\ 200809L
86 .TP
87 Before glibc 2.10:
88 _ATFILE_SOURCE
89 .RE
90 .PD
91 .ad
92 .SH DESCRIPTION
93 .PP
94 These functions return information about a file, in the buffer pointed to by
95 .IR statbuf .
96 No permissions are required on the file itself, but\(emin the case of
97 .BR stat (),
98 .BR fstatat (),
99 and
100 .BR lstat ()\(emexecute
101 (search) permission is required on all of the directories in
102 .I pathname
103 that lead to the file.
104 .PP
105 .BR stat ()
106 and
107 .BR fstatat ()
108 retrieve information about the file pointed to by
109 .IR pathname ;
110 the differences for
111 .BR fstatat ()
112 are described below.
113 .PP
114 .BR lstat ()
115 is identical to
116 .BR stat (),
117 except that if
118 .I pathname
119 is a symbolic link, then it returns information about the link itself,
120 not the file that it refers to.
121 .PP
122 .BR fstat ()
123 is identical to
124 .BR stat (),
125 except that the file about which information is to be retrieved
126 is specified by the file descriptor
127 .IR fd .
128 .\"
129 .SS The stat structure
130 All of these system calls return a
131 .I stat
132 structure, which contains the following fields:
133 .PP
134 .in +4n
135 .EX
136 struct stat {
137 dev_t st_dev; /* ID of device containing file */
138 ino_t st_ino; /* Inode number */
139 mode_t st_mode; /* File type and mode */
140 nlink_t st_nlink; /* Number of hard links */
141 uid_t st_uid; /* User ID of owner */
142 gid_t st_gid; /* Group ID of owner */
143 dev_t st_rdev; /* Device ID (if special file) */
144 off_t st_size; /* Total size, in bytes */
145 blksize_t st_blksize; /* Block size for filesystem I/O */
146 blkcnt_t st_blocks; /* Number of 512B blocks allocated */
147
148 /* Since Linux 2.6, the kernel supports nanosecond
149 precision for the following timestamp fields.
150 For the details before Linux 2.6, see NOTES. */
151
152 struct timespec st_atim; /* Time of last access */
153 struct timespec st_mtim; /* Time of last modification */
154 struct timespec st_ctim; /* Time of last status change */
155
156 #define st_atime st_atim.tv_sec /* Backward compatibility */
157 #define st_mtime st_mtim.tv_sec
158 #define st_ctime st_ctim.tv_sec
159 };
160 .EE
161 .in
162 .PP
163 .IR Note :
164 the order of fields in the
165 .I stat
166 structure varies somewhat
167 across architectures.
168 In addition,
169 the definition above does not show the padding bytes
170 that may be present between some fields on various architectures.
171 Consult the glibc and kernel source code
172 if you need to know the details.
173 .PP
174 .\" Background: inode attributes are modified with i_mutex held, but
175 .\" read by stat() without taking the mutex.
176 .IR Note :
177 for performance and simplicity reasons, different fields in the
178 .I stat
179 structure may contain state information from different moments
180 during the execution of the system call.
181 For example, if
182 .IR st_mode
183 or
184 .IR st_uid
185 is changed by another process by calling
186 .BR chmod (2)
187 or
188 .BR chown (2),
189 .BR stat ()
190 might return the old
191 .I st_mode
192 together with the new
193 .IR st_uid ,
194 or the old
195 .I st_uid
196 together with the new
197 .IR st_mode .
198 .PP
199 The fields in the
200 .I stat
201 structure are as follows:
202 .TP
203 .I st_dev
204 This field describes the device on which this file resides.
205 (The
206 .BR major (3)
207 and
208 .BR minor (3)
209 macros may be useful to decompose the device ID in this field.)
210 .TP
211 .I st_ino
212 This field contains the file's inode number.
213 .TP
214 .I st_mode
215 This field contains the file type and mode.
216 See
217 .BR inode (7)
218 for further information.
219 .TP
220 .I st_nlink
221 This field contains the number of hard links to the file.
222 .TP
223 .I st_uid
224 This field contains the user ID of the owner of the file.
225 .TP
226 .I st_gid
227 This field contains the ID of the group owner of the file.
228 .TP
229 .I st_rdev
230 This field describes the device that this file (inode) represents.
231 .TP
232 .I st_size
233 This field gives the size of the file (if it is a regular
234 file or a symbolic link) in bytes.
235 The size of a symbolic link is the length of the pathname
236 it contains, without a terminating null byte.
237 .TP
238 .I st_blksize
239 This field gives the "preferred" block size for efficient filesystem I/O.
240 .TP
241 .I st_blocks
242 This field indicates the number of blocks allocated to the file,
243 in 512-byte units.
244 (This may be smaller than
245 .IR st_size /512
246 when the file has holes.)
247 .TP
248 .I st_atime
249 This is the file's last access timestamp.
250 .TP
251 .I st_mtime
252 This is the file's last modification timestamp.
253 .TP
254 .I st_ctime
255 This is the file's last status change timestamp.
256 .PP
257 For further information on the above fields, see
258 .BR inode (7).
259 .\"
260 .SS fstatat()
261 The
262 .BR fstatat ()
263 system call is a more general interface for accessing file information
264 which can still provide exactly the behavior of each of
265 .BR stat (),
266 .BR lstat (),
267 and
268 .BR fstat ().
269 .PP
270 If the pathname given in
271 .I pathname
272 is relative, then it is interpreted relative to the directory
273 referred to by the file descriptor
274 .I dirfd
275 (rather than relative to the current working directory of
276 the calling process, as is done by
277 .BR stat ()
278 and
279 .BR lstat ()
280 for a relative pathname).
281 .PP
282 If
283 .I pathname
284 is relative and
285 .I dirfd
286 is the special value
287 .BR AT_FDCWD ,
288 then
289 .I pathname
290 is interpreted relative to the current working
291 directory of the calling process (like
292 .BR stat ()
293 and
294 .BR lstat ()).
295 .PP
296 If
297 .I pathname
298 is absolute, then
299 .I dirfd
300 is ignored.
301 .PP
302 .I flags
303 can either be 0, or include one or more of the following flags ORed:
304 .TP
305 .BR AT_EMPTY_PATH " (since Linux 2.6.39)"
306 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
307 If
308 .I pathname
309 is an empty string, operate on the file referred to by
310 .IR dirfd
311 (which may have been obtained using the
312 .BR open (2)
313 .B O_PATH
314 flag).
315 In this case,
316 .I dirfd
317 can refer to any type of file, not just a directory, and
318 the behavior of
319 .BR fstatat ()
320 is similar to that of
321 .BR fstat ().
322 If
323 .I dirfd
324 is
325 .BR AT_FDCWD ,
326 the call operates on the current working directory.
327 This flag is Linux-specific; define
328 .B _GNU_SOURCE
329 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
330 to obtain its definition.
331 .TP
332 .BR AT_NO_AUTOMOUNT " (since Linux 2.6.38)"
333 Don't automount the terminal ("basename") component of
334 .I pathname
335 if it is a directory that is an automount point.
336 This allows the caller to gather attributes of an automount point
337 (rather than the location it would mount).
338 Since Linux 4.14,
339 .\" commit 42f46148217865a545e129612075f3d828a2c4e4
340 also don't instantiate a nonexistent name in an
341 on-demand directory such as used for automounter indirect maps.
342 This flag can be used in tools that scan directories
343 to prevent mass-automounting of a directory of automount points.
344 The
345 .B AT_NO_AUTOMOUNT
346 flag has no effect if the mount point has already been mounted over.
347 This flag is Linux-specific; define
348 .B _GNU_SOURCE
349 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
350 to obtain its definition.
351 Both
352 .BR stat ()
353 and
354 .BR lstat ()
355 act as though
356 .B AT_NO_AUTOMOUNT
357 was set.
358 .TP
359 .B AT_SYMLINK_NOFOLLOW
360 If
361 .I pathname
362 is a symbolic link, do not dereference it:
363 instead return information about the link itself, like
364 .BR lstat ().
365 (By default,
366 .BR fstatat ()
367 dereferences symbolic links, like
368 .BR stat ().)
369 .PP
370 See
371 .BR openat (2)
372 for an explanation of the need for
373 .BR fstatat ().
374 .SH RETURN VALUE
375 On success, zero is returned.
376 On error, \-1 is returned, and
377 .I errno
378 is set appropriately.
379 .SH ERRORS
380 .TP
381 .B EACCES
382 Search permission is denied for one of the directories
383 in the path prefix of
384 .IR pathname .
385 (See also
386 .BR path_resolution (7).)
387 .TP
388 .B EBADF
389 .I fd
390 is not a valid open file descriptor.
391 .TP
392 .B EFAULT
393 Bad address.
394 .TP
395 .B ELOOP
396 Too many symbolic links encountered while traversing the path.
397 .TP
398 .B ENAMETOOLONG
399 .I pathname
400 is too long.
401 .TP
402 .B ENOENT
403 A component of
404 .I pathname
405 does not exist or is a dangling symbolic link.
406 .TP
407 .B ENOENT
408 .I pathname
409 is an empty string and
410 .B AT_EMPTY_PATH
411 was not specified in
412 .IR flags .
413 .TP
414 .B ENOMEM
415 Out of memory (i.e., kernel memory).
416 .TP
417 .B ENOTDIR
418 A component of the path prefix of
419 .I pathname
420 is not a directory.
421 .TP
422 .B EOVERFLOW
423 .I pathname
424 or
425 .I fd
426 refers to a file whose size, inode number,
427 or number of blocks cannot be represented in, respectively, the types
428 .IR off_t ,
429 .IR ino_t ,
430 or
431 .IR blkcnt_t .
432 This error can occur when, for example,
433 an application compiled on a 32-bit platform without
434 .I -D_FILE_OFFSET_BITS=64
435 calls
436 .BR stat ()
437 on a file whose size exceeds
438 .I (1<<31)-1
439 bytes.
440 .PP
441 The following additional errors can occur for
442 .BR fstatat ():
443 .TP
444 .B EBADF
445 .I dirfd
446 is not a valid file descriptor.
447 .TP
448 .B EINVAL
449 Invalid flag specified in
450 .IR flags .
451 .TP
452 .B ENOTDIR
453 .I pathname
454 is relative and
455 .I dirfd
456 is a file descriptor referring to a file other than a directory.
457 .SH VERSIONS
458 .BR fstatat ()
459 was added to Linux in kernel 2.6.16;
460 library support was added to glibc in version 2.4.
461 .SH CONFORMING TO
462 .BR stat (),
463 .BR fstat (),
464 .BR lstat ():
465 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
466 .\" SVr4 documents additional
467 .\" .BR fstat ()
468 .\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
469 .\" documents additional
470 .\" .BR stat ()
471 .\" and
472 .\" .BR lstat ()
473 .\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
474 .PP
475 .BR fstatat ():
476 POSIX.1-2008.
477 .PP
478 According to POSIX.1-2001,
479 .BR lstat ()
480 on a symbolic link need return valid information only in the
481 .I st_size
482 field and the file type of the
483 .IR st_mode
484 field of the
485 .IR stat
486 structure.
487 POSIX.1-2008 tightens the specification, requiring
488 .BR lstat ()
489 to return valid information in all fields except the mode bits in
490 .IR st_mode .
491 .PP
492 Use of the
493 .I st_blocks
494 and
495 .I st_blksize
496 fields may be less portable.
497 (They were introduced in BSD.
498 The interpretation differs between systems,
499 and possibly on a single system when NFS mounts are involved.)
500 .SH NOTES
501 .SS Timestamp fields
502 Older kernels and older standards did not support nanosecond timestamp
503 fields.
504 Instead, there were three timestamp
505 .RI fields\(em st_atime ,
506 .IR st_mtime ,
507 and
508 .IR st_ctime \(emtyped
509 as
510 .IR time_t
511 that recorded timestamps with one-second precision.
512 .PP
513 Since kernel 2.5.48, the
514 .I stat
515 structure supports nanosecond resolution for the three file timestamp fields.
516 The nanosecond components of each timestamp are available
517 via names of the form
518 .IR st_atim.tv_nsec ,
519 if suitable feature test macros are defined.
520 Nanosecond timestamps were standardized in POSIX.1-2008,
521 and, starting with version 2.12,
522 glibc exposes the nanosecond component names if
523 .BR _POSIX_C_SOURCE
524 is defined with the value 200809L or greater, or
525 .BR _XOPEN_SOURCE
526 is defined with the value 700 or greater.
527 Up to and including glibc 2.19,
528 the definitions of the nanoseconds components are also defined if
529 .B _BSD_SOURCE
530 or
531 .B _SVID_SOURCE
532 is defined.
533 If none of the aforementioned macros are defined,
534 then the nanosecond values are exposed with names of the form
535 .IR st_atimensec .
536 .\"
537 .SS C library/kernel differences
538 Over time, increases in the size of the
539 .I stat
540 structure have led to three successive versions of
541 .BR stat ():
542 .IR sys_stat ()
543 (slot
544 .IR __NR_oldstat ),
545 .IR sys_newstat ()
546 (slot
547 .IR __NR_stat ),
548 and
549 .I sys_stat64()
550 (slot
551 .IR __NR_stat64 )
552 on 32-bit platforms such as i386.
553 The first two versions were already present in Linux 1.0
554 (albeit with different names);
555 .\" See include/asm-i386/stat.h in the Linux 2.4 source code for the
556 .\" various versions of the structure definitions
557 the last was added in Linux 2.4.
558 Similar remarks apply for
559 .BR fstat ()
560 and
561 .BR lstat ().
562 .PP
563 The kernel-internal versions of the
564 .I stat
565 structure dealt with by the different versions are, respectively:
566 .TP
567 .IR __old_kernel_stat
568 The original structure, with rather narrow fields, and no padding.
569 .TP
570 .IR stat
571 Larger
572 .I st_ino
573 field and padding added to various parts of the structure to
574 allow for future expansion.
575 .TP
576 .IR stat64
577 Even larger
578 .I st_ino
579 field,
580 larger
581 .I st_uid
582 and
583 .I st_gid
584 fields to accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits,
585 and various other enlarged fields and further padding in the structure.
586 (Various padding bytes were eventually consumed in Linux 2.6,
587 with the advent of 32-bit device IDs and nanosecond components
588 for the timestamp fields.)
589 .PP
590 The glibc
591 .BR stat ()
592 wrapper function hides these details from applications,
593 invoking the most recent version of the system call provided by the kernel,
594 and repacking the returned information if required for old binaries.
595 .\"
596 .\" A note from Andries Brouwer, July 2007
597 .\"
598 .\" > Is the story not rather more complicated for some calls like
599 .\" > stat(2)?
600 .\"
601 .\" Yes and no, mostly no. See /usr/include/sys/stat.h .
602 .\"
603 .\" The idea is here not so much that syscalls change, but that
604 .\" the definitions of struct stat and of the types dev_t and mode_t change.
605 .\" This means that libc (even if it does not call the kernel
606 .\" but only calls some internal function) must know what the
607 .\" format of dev_t or of struct stat is.
608 .\" The communication between the application and libc goes via
609 .\" the include file <sys/stat.h> that defines a _STAT_VER and
610 .\" _MKNOD_VER describing the layout of the data that user space
611 .\" uses. Each (almost each) occurrence of stat() is replaced by
612 .\" an occurrence of xstat() where the first parameter of xstat()
613 .\" is this version number _STAT_VER.
614 .\"
615 .\" Now, also the definitions used by the kernel change.
616 .\" But glibc copes with this in the standard way, and the
617 .\" struct stat as returned by the kernel is repacked into
618 .\" the struct stat as expected by the application.
619 .\" Thus, _STAT_VER and this setup cater for the application-libc
620 .\" interface, rather than the libc-kernel interface.
621 .\"
622 .\" (Note that the details depend on gcc being used as c compiler.)
623 .PP
624 On modern 64-bit systems, life is simpler: there is a single
625 .BR stat ()
626 system call and the kernel deals with a
627 .I stat
628 structure that contains fields of a sufficient size.
629 .PP
630 The underlying system call employed by the glibc
631 .BR fstatat ()
632 wrapper function is actually called
633 .BR fstatat64 ()
634 or, on some architectures,
635 .\" strace(1) shows the name "newfstatat" on x86-64
636 .BR newfstatat ().
637 .SH EXAMPLE
638 The following program calls
639 .BR lstat ()
640 and displays selected fields in the returned
641 .I stat
642 structure.
643 .PP
644 .EX
645 #include <sys/types.h>
646 #include <sys/stat.h>
647 #include <time.h>
648 #include <stdio.h>
649 #include <stdlib.h>
650 #include <sys/sysmacros.h>
651
652 int
653 main(int argc, char *argv[])
654 {
655 struct stat sb;
656
657 if (argc != 2) {
658 fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]);
659 exit(EXIT_FAILURE);
660 }
661
662 if (lstat(argv[1], &sb) == \-1) {
663 perror("lstat");
664 exit(EXIT_FAILURE);
665 }
666
667 printf("ID of containing device: [%lx,%lx]\\n",
668 (long) major(sb.st_dev), (long) minor(sb.st_dev));
669
670 printf("File type: ");
671
672 switch (sb.st_mode & S_IFMT) {
673 case S_IFBLK: printf("block device\\n"); break;
674 case S_IFCHR: printf("character device\\n"); break;
675 case S_IFDIR: printf("directory\\n"); break;
676 case S_IFIFO: printf("FIFO/pipe\\n"); break;
677 case S_IFLNK: printf("symlink\\n"); break;
678 case S_IFREG: printf("regular file\\n"); break;
679 case S_IFSOCK: printf("socket\\n"); break;
680 default: printf("unknown?\\n"); break;
681 }
682
683 printf("I\-node number: %ld\\n", (long) sb.st_ino);
684
685 printf("Mode: %lo (octal)\\n",
686 (unsigned long) sb.st_mode);
687
688 printf("Link count: %ld\\n", (long) sb.st_nlink);
689 printf("Ownership: UID=%ld GID=%ld\\n",
690 (long) sb.st_uid, (long) sb.st_gid);
691
692 printf("Preferred I/O block size: %ld bytes\\n",
693 (long) sb.st_blksize);
694 printf("File size: %lld bytes\\n",
695 (long long) sb.st_size);
696 printf("Blocks allocated: %lld\\n",
697 (long long) sb.st_blocks);
698
699 printf("Last status change: %s", ctime(&sb.st_ctime));
700 printf("Last file access: %s", ctime(&sb.st_atime));
701 printf("Last file modification: %s", ctime(&sb.st_mtime));
702
703 exit(EXIT_SUCCESS);
704 }
705 .EE
706 .SH SEE ALSO
707 .BR ls (1),
708 .BR stat (1),
709 .BR access (2),
710 .BR chmod (2),
711 .BR chown (2),
712 .BR readlink (2),
713 .BR utime (2),
714 .BR capabilities (7),
715 .BR inode (7),
716 .BR symlink (7)