]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/stat.2
sched_setattr.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 2020-04-11 "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 the link 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 time of the last access of file data.
250 .TP
251 .I st_mtime
252 This is the time of last modification of file data.
253 .TP
254 .I st_ctime
255 This is the file's last status change timestamp
256 (time of last change to the inode).
257 .PP
258 For further information on the above fields, see
259 .BR inode (7).
260 .\"
261 .SS fstatat()
262 The
263 .BR fstatat ()
264 system call is a more general interface for accessing file information
265 which can still provide exactly the behavior of each of
266 .BR stat (),
267 .BR lstat (),
268 and
269 .BR fstat ().
270 .PP
271 If the pathname given in
272 .I pathname
273 is relative, then it is interpreted relative to the directory
274 referred to by the file descriptor
275 .I dirfd
276 (rather than relative to the current working directory of
277 the calling process, as is done by
278 .BR stat ()
279 and
280 .BR lstat ()
281 for a relative pathname).
282 .PP
283 If
284 .I pathname
285 is relative and
286 .I dirfd
287 is the special value
288 .BR AT_FDCWD ,
289 then
290 .I pathname
291 is interpreted relative to the current working
292 directory of the calling process (like
293 .BR stat ()
294 and
295 .BR lstat ()).
296 .PP
297 If
298 .I pathname
299 is absolute, then
300 .I dirfd
301 is ignored.
302 .PP
303 .I flags
304 can either be 0, or include one or more of the following flags ORed:
305 .TP
306 .BR AT_EMPTY_PATH " (since Linux 2.6.39)"
307 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
308 If
309 .I pathname
310 is an empty string, operate on the file referred to by
311 .IR dirfd
312 (which may have been obtained using the
313 .BR open (2)
314 .B O_PATH
315 flag).
316 In this case,
317 .I dirfd
318 can refer to any type of file, not just a directory, and
319 the behavior of
320 .BR fstatat ()
321 is similar to that of
322 .BR fstat ().
323 If
324 .I dirfd
325 is
326 .BR AT_FDCWD ,
327 the call operates on the current working directory.
328 This flag is Linux-specific; define
329 .B _GNU_SOURCE
330 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
331 to obtain its definition.
332 .TP
333 .BR AT_NO_AUTOMOUNT " (since Linux 2.6.38)"
334 Don't automount the terminal ("basename") component of
335 .I pathname
336 if it is a directory that is an automount point.
337 This allows the caller to gather attributes of an automount point
338 (rather than the location it would mount).
339 Since Linux 4.14,
340 .\" commit 42f46148217865a545e129612075f3d828a2c4e4
341 also don't instantiate a nonexistent name in an
342 on-demand directory such as used for automounter indirect maps.
343 This
344 flag has no effect if the mount point has already been mounted over.
345 .IP
346 Both
347 .BR stat ()
348 and
349 .BR lstat ()
350 act as though
351 .B AT_NO_AUTOMOUNT
352 was set.
353 .IP
354 The
355 .B AT_NO_AUTOMOUNT
356 can be used in tools that scan directories
357 to prevent mass-automounting of a directory of automount points.
358 .IP
359 .IP
360 This flag is Linux-specific; define
361 .B _GNU_SOURCE
362 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
363 to obtain its definition.
364 .TP
365 .B AT_SYMLINK_NOFOLLOW
366 If
367 .I pathname
368 is a symbolic link, do not dereference it:
369 instead return information about the link itself, like
370 .BR lstat ().
371 (By default,
372 .BR fstatat ()
373 dereferences symbolic links, like
374 .BR stat ().)
375 .PP
376 See
377 .BR openat (2)
378 for an explanation of the need for
379 .BR fstatat ().
380 .SH RETURN VALUE
381 On success, zero is returned.
382 On error, \-1 is returned, and
383 .I errno
384 is set appropriately.
385 .SH ERRORS
386 .TP
387 .B EACCES
388 Search permission is denied for one of the directories
389 in the path prefix of
390 .IR pathname .
391 (See also
392 .BR path_resolution (7).)
393 .TP
394 .B EBADF
395 .I fd
396 is not a valid open file descriptor.
397 .TP
398 .B EFAULT
399 Bad address.
400 .TP
401 .B ELOOP
402 Too many symbolic links encountered while traversing the path.
403 .TP
404 .B ENAMETOOLONG
405 .I pathname
406 is too long.
407 .TP
408 .B ENOENT
409 A component of
410 .I pathname
411 does not exist or is a dangling symbolic link.
412 .TP
413 .B ENOENT
414 .I pathname
415 is an empty string and
416 .B AT_EMPTY_PATH
417 was not specified in
418 .IR flags .
419 .TP
420 .B ENOMEM
421 Out of memory (i.e., kernel memory).
422 .TP
423 .B ENOTDIR
424 A component of the path prefix of
425 .I pathname
426 is not a directory.
427 .TP
428 .B EOVERFLOW
429 .I pathname
430 or
431 .I fd
432 refers to a file whose size, inode number,
433 or number of blocks cannot be represented in, respectively, the types
434 .IR off_t ,
435 .IR ino_t ,
436 or
437 .IR blkcnt_t .
438 This error can occur when, for example,
439 an application compiled on a 32-bit platform without
440 .I -D_FILE_OFFSET_BITS=64
441 calls
442 .BR stat ()
443 on a file whose size exceeds
444 .I (1<<31)-1
445 bytes.
446 .PP
447 The following additional errors can occur for
448 .BR fstatat ():
449 .TP
450 .B EBADF
451 .I dirfd
452 is not a valid file descriptor.
453 .TP
454 .B EINVAL
455 Invalid flag specified in
456 .IR flags .
457 .TP
458 .B ENOTDIR
459 .I pathname
460 is relative and
461 .I dirfd
462 is a file descriptor referring to a file other than a directory.
463 .SH VERSIONS
464 .BR fstatat ()
465 was added to Linux in kernel 2.6.16;
466 library support was added to glibc in version 2.4.
467 .SH CONFORMING TO
468 .BR stat (),
469 .BR fstat (),
470 .BR lstat ():
471 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
472 .\" SVr4 documents additional
473 .\" .BR fstat ()
474 .\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
475 .\" documents additional
476 .\" .BR stat ()
477 .\" and
478 .\" .BR lstat ()
479 .\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
480 .PP
481 .BR fstatat ():
482 POSIX.1-2008.
483 .PP
484 According to POSIX.1-2001,
485 .BR lstat ()
486 on a symbolic link need return valid information only in the
487 .I st_size
488 field and the file type of the
489 .IR st_mode
490 field of the
491 .IR stat
492 structure.
493 POSIX.1-2008 tightens the specification, requiring
494 .BR lstat ()
495 to return valid information in all fields except the mode bits in
496 .IR st_mode .
497 .PP
498 Use of the
499 .I st_blocks
500 and
501 .I st_blksize
502 fields may be less portable.
503 (They were introduced in BSD.
504 The interpretation differs between systems,
505 and possibly on a single system when NFS mounts are involved.)
506 .SH NOTES
507 .SS Timestamp fields
508 Older kernels and older standards did not support nanosecond timestamp
509 fields.
510 Instead, there were three timestamp
511 .RI fields\(em st_atime ,
512 .IR st_mtime ,
513 and
514 .IR st_ctime \(emtyped
515 as
516 .IR time_t
517 that recorded timestamps with one-second precision.
518 .PP
519 Since kernel 2.5.48, the
520 .I stat
521 structure supports nanosecond resolution for the three file timestamp fields.
522 The nanosecond components of each timestamp are available
523 via names of the form
524 .IR st_atim.tv_nsec ,
525 if suitable feature test macros are defined.
526 Nanosecond timestamps were standardized in POSIX.1-2008,
527 and, starting with version 2.12,
528 glibc exposes the nanosecond component names if
529 .BR _POSIX_C_SOURCE
530 is defined with the value 200809L or greater, or
531 .BR _XOPEN_SOURCE
532 is defined with the value 700 or greater.
533 Up to and including glibc 2.19,
534 the definitions of the nanoseconds components are also defined if
535 .B _BSD_SOURCE
536 or
537 .B _SVID_SOURCE
538 is defined.
539 If none of the aforementioned macros are defined,
540 then the nanosecond values are exposed with names of the form
541 .IR st_atimensec .
542 .\"
543 .SS C library/kernel differences
544 Over time, increases in the size of the
545 .I stat
546 structure have led to three successive versions of
547 .BR stat ():
548 .IR sys_stat ()
549 (slot
550 .IR __NR_oldstat ),
551 .IR sys_newstat ()
552 (slot
553 .IR __NR_stat ),
554 and
555 .I sys_stat64()
556 (slot
557 .IR __NR_stat64 )
558 on 32-bit platforms such as i386.
559 The first two versions were already present in Linux 1.0
560 (albeit with different names);
561 .\" See include/asm-i386/stat.h in the Linux 2.4 source code for the
562 .\" various versions of the structure definitions
563 the last was added in Linux 2.4.
564 Similar remarks apply for
565 .BR fstat ()
566 and
567 .BR lstat ().
568 .PP
569 The kernel-internal versions of the
570 .I stat
571 structure dealt with by the different versions are, respectively:
572 .TP
573 .IR __old_kernel_stat
574 The original structure, with rather narrow fields, and no padding.
575 .TP
576 .IR stat
577 Larger
578 .I st_ino
579 field and padding added to various parts of the structure to
580 allow for future expansion.
581 .TP
582 .IR stat64
583 Even larger
584 .I st_ino
585 field,
586 larger
587 .I st_uid
588 and
589 .I st_gid
590 fields to accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits,
591 and various other enlarged fields and further padding in the structure.
592 (Various padding bytes were eventually consumed in Linux 2.6,
593 with the advent of 32-bit device IDs and nanosecond components
594 for the timestamp fields.)
595 .PP
596 The glibc
597 .BR stat ()
598 wrapper function hides these details from applications,
599 invoking the most recent version of the system call provided by the kernel,
600 and repacking the returned information if required for old binaries.
601 .\"
602 .\" A note from Andries Brouwer, July 2007
603 .\"
604 .\" > Is the story not rather more complicated for some calls like
605 .\" > stat(2)?
606 .\"
607 .\" Yes and no, mostly no. See /usr/include/sys/stat.h .
608 .\"
609 .\" The idea is here not so much that syscalls change, but that
610 .\" the definitions of struct stat and of the types dev_t and mode_t change.
611 .\" This means that libc (even if it does not call the kernel
612 .\" but only calls some internal function) must know what the
613 .\" format of dev_t or of struct stat is.
614 .\" The communication between the application and libc goes via
615 .\" the include file <sys/stat.h> that defines a _STAT_VER and
616 .\" _MKNOD_VER describing the layout of the data that user space
617 .\" uses. Each (almost each) occurrence of stat() is replaced by
618 .\" an occurrence of xstat() where the first parameter of xstat()
619 .\" is this version number _STAT_VER.
620 .\"
621 .\" Now, also the definitions used by the kernel change.
622 .\" But glibc copes with this in the standard way, and the
623 .\" struct stat as returned by the kernel is repacked into
624 .\" the struct stat as expected by the application.
625 .\" Thus, _STAT_VER and this setup cater for the application-libc
626 .\" interface, rather than the libc-kernel interface.
627 .\"
628 .\" (Note that the details depend on gcc being used as c compiler.)
629 .PP
630 On modern 64-bit systems, life is simpler: there is a single
631 .BR stat ()
632 system call and the kernel deals with a
633 .I stat
634 structure that contains fields of a sufficient size.
635 .PP
636 The underlying system call employed by the glibc
637 .BR fstatat ()
638 wrapper function is actually called
639 .BR fstatat64 ()
640 or, on some architectures,
641 .\" strace(1) shows the name "newfstatat" on x86-64
642 .BR newfstatat ().
643 .SH EXAMPLE
644 The following program calls
645 .BR lstat ()
646 and displays selected fields in the returned
647 .I stat
648 structure.
649 .PP
650 .EX
651 #include <sys/types.h>
652 #include <sys/stat.h>
653 #include <time.h>
654 #include <stdio.h>
655 #include <stdlib.h>
656 #include <sys/sysmacros.h>
657
658 int
659 main(int argc, char *argv[])
660 {
661 struct stat sb;
662
663 if (argc != 2) {
664 fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
665 exit(EXIT_FAILURE);
666 }
667
668 if (lstat(argv[1], &sb) == \-1) {
669 perror("lstat");
670 exit(EXIT_FAILURE);
671 }
672
673 printf("ID of containing device: [%lx,%lx]\en",
674 (long) major(sb.st_dev), (long) minor(sb.st_dev));
675
676 printf("File type: ");
677
678 switch (sb.st_mode & S_IFMT) {
679 case S_IFBLK: printf("block device\en"); break;
680 case S_IFCHR: printf("character device\en"); break;
681 case S_IFDIR: printf("directory\en"); break;
682 case S_IFIFO: printf("FIFO/pipe\en"); break;
683 case S_IFLNK: printf("symlink\en"); break;
684 case S_IFREG: printf("regular file\en"); break;
685 case S_IFSOCK: printf("socket\en"); break;
686 default: printf("unknown?\en"); break;
687 }
688
689 printf("I\-node number: %ld\en", (long) sb.st_ino);
690
691 printf("Mode: %lo (octal)\en",
692 (unsigned long) sb.st_mode);
693
694 printf("Link count: %ld\en", (long) sb.st_nlink);
695 printf("Ownership: UID=%ld GID=%ld\en",
696 (long) sb.st_uid, (long) sb.st_gid);
697
698 printf("Preferred I/O block size: %ld bytes\en",
699 (long) sb.st_blksize);
700 printf("File size: %lld bytes\en",
701 (long long) sb.st_size);
702 printf("Blocks allocated: %lld\en",
703 (long long) sb.st_blocks);
704
705 printf("Last status change: %s", ctime(&sb.st_ctime));
706 printf("Last file access: %s", ctime(&sb.st_atime));
707 printf("Last file modification: %s", ctime(&sb.st_mtime));
708
709 exit(EXIT_SUCCESS);
710 }
711 .EE
712 .SH SEE ALSO
713 .BR ls (1),
714 .BR stat (1),
715 .BR access (2),
716 .BR chmod (2),
717 .BR chown (2),
718 .BR readlink (2),
719 .BR statx (2),
720 .BR utime (2),
721 .BR capabilities (7),
722 .BR inode (7),
723 .BR symlink (7)