]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/utimensat.2
mmap.2: srcfix: note kernel commit that caused MAP_POPULATE | MAP_NONBLOCK to be...
[thirdparty/man-pages.git] / man2 / utimensat.2
CommitLineData
61dd016f
MK
1.\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
2.\" <mtk.manpages@gmail.com>
3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
61dd016f
MK
5.\" Permission is granted to make and distribute verbatim copies of this
6.\" manual provided the copyright notice and this permission notice are
7.\" preserved on all copies.
8.\"
9.\" Permission is granted to copy and distribute modified versions of this
10.\" manual under the conditions for verbatim copying, provided that the
11.\" entire resulting derived work is distributed under the terms of a
12.\" permission notice identical to this one.
13.\"
14.\" Since the Linux kernel and libraries are constantly changing, this
15.\" manual page may be incorrect or out-of-date. The author(s) assume no
16.\" responsibility for errors or omissions, or for damages resulting from
17.\" the use of the information contained herein. The author(s) may not
18.\" have taken the same level of care in the production of this manual,
19.\" which is licensed free of charge, as they might when working
20.\" professionally.
21.\"
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
61dd016f 25.\"
4b8c67d9 26.TH UTIMENSAT 2 2017-09-15 "Linux" "Linux Programmer's Manual"
61dd016f
MK
27.SH NAME
28utimensat, futimens \- change file timestamps with nanosecond precision
61dd016f
MK
29.SH SYNOPSIS
30.nf
3045389f 31.B #include <fcntl.h> /* Definition of AT_* constants */
61dd016f 32.B #include <sys/stat.h>
68e4db0a 33.PP
61dd016f
MK
34.BI "int utimensat(int " dirfd ", const char *" pathname ,
35.BI " const struct timespec " times "[2], int " flags );
dbfe9c70 36.PP
61dd016f
MK
37.BI "int futimens(int " fd ", const struct timespec " times [2]);
38.fi
68e4db0a 39.PP
61dd016f
MK
40.in -4n
41Feature Test Macro Requirements for glibc (see
42.BR feature_test_macros (7)):
43.in
68e4db0a 44.PP
34c38af8
MK
45.ad l
46.PD 0
61dd016f 47.BR utimensat ():
34c38af8
MK
48.RS 4
49.TP 4
50Since glibc 2.10:
b0da7b8b 51_POSIX_C_SOURCE\ >=\ 200809L
34c38af8 52.TP
7cc796ee 53Before glibc 2.10:
61dd016f 54_ATFILE_SOURCE
34c38af8
MK
55.RE
56.PP
61dd016f 57.BR futimens ():
34c38af8
MK
58.RS 4
59.TP
60Since glibc 2.10:
b0da7b8b 61_POSIX_C_SOURCE\ >=\ 200809L
34c38af8 62.TP
7cc796ee
MK
63Before glibc 2.10:
64_GNU_SOURCE
34c38af8
MK
65.RE
66.PD
67.ad
61dd016f
MK
68.SH DESCRIPTION
69.BR utimensat ()
70and
71.BR futimens ()
72update the timestamps of a file with nanosecond precision.
73This contrasts with the historical
74.BR utime (2)
75and
76.BR utimes (2),
77which permit only second and microsecond precision, respectively,
78when setting file timestamps.
efeece04 79.PP
61dd016f
MK
80With
81.BR utimensat ()
82the file is specified via the pathname given in
83.IR pathname .
84With
85.BR futimens ()
86the file whose timestamps are to be updated is specified via
87an open file descriptor,
88.IR fd .
efeece04 89.PP
61dd016f
MK
90For both calls, the new file timestamps are specified in the array
91.IR times :
92.IR times [0]
93specifies the new "last access time" (\fIatime\fP);
94.IR times [1]
95specifies the new "last modification time" (\fImtime\fP).
96Each of the elements of
97.I times
cc3707c0 98specifies a time as the number of seconds and nanoseconds
f49c451a
MW
99since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
100This information is conveyed in a structure of the following form:
e646a1ba 101.PP
61dd016f 102.in +4n
e646a1ba 103.EX
61dd016f
MK
104struct timespec {
105 time_t tv_sec; /* seconds */
106 long tv_nsec; /* nanoseconds */
107};
b8302363 108.EE
61dd016f
MK
109.in
110.PP
111Updated file timestamps are set to the greatest value
9ee4a2b6 112supported by the filesystem that is not greater than the specified time.
efeece04 113.PP
61dd016f
MK
114If the
115.I tv_nsec
116field of one of the
117.I timespec
118structures has the special value
119.BR UTIME_NOW ,
120then the corresponding file timestamp is set to the current time.
121If the
122.I tv_nsec
123field of one of the
124.I timespec
125structures has the special value
126.BR UTIME_OMIT ,
127then the corresponding file timestamp is left unchanged.
128In both of these cases, the value of the corresponding
129.I tv_sec
130.\" 2.6.22 was broken: it is not ignored
131field is ignored.
efeece04 132.PP
61dd016f
MK
133If
134.I times
135is NULL, then both timestamps are set to the current time.
136.\"
137.SS Permissions requirements
138To set both file timestamps to the current time (i.e.,
139.I times
140is NULL, or both
141.I tv_nsec
142fields specify
143.BR UTIME_NOW ),
144either:
145.IP 1. 3
146the caller must have write access to the file;
147.\" 2.6.22 was broken here -- for futimens() the check is
148.\" based on whether or not the file descriptor is writable,
66d90115 149.\" not on whether the caller's effective UID has write
61dd016f
MK
150.\" permission for the file referred to by the descriptor.
151.IP 2.
152the caller's effective user ID must match the owner of the file; or
153.IP 3.
154the caller must have appropriate privileges.
155.PP
156To make any change other than setting both timestamps to the
157current time (i.e.,
158.I times
17068fad 159is not NULL, and neither
61dd016f 160.I tv_nsec
17068fad 161field is
61dd016f
MK
162.B UTIME_NOW
163.\" 2.6.22 was broken here:
79e20ac1 164.\" both must be something other than *either* UTIME_OMIT *or* UTIME_NOW.
17068fad 165and neither
61dd016f 166.I tv_nsec
17068fad 167field is
61dd016f
MK
168.BR UTIME_OMIT ),
169either condition 2 or 3 above must apply.
efeece04 170.PP
61dd016f
MK
171If both
172.I tv_nsec
173fields are specified as
174.BR UTIME_OMIT ,
175then no file ownership or permission checks are performed,
176and the file timestamps are not modified,
177but other error conditions may still be detected.
178.\"
179.\"
180.SS utimensat() specifics
181If
182.I pathname
183is relative, then by default it is interpreted relative to the
184directory referred to by the open file descriptor,
185.IR dirfd
186(rather than relative to the current working directory of
187the calling process, as is done by
188.BR utimes (2)
189for a relative pathname).
190See
191.BR openat (2)
192for an explanation of why this can be useful.
efeece04 193.PP
61dd016f
MK
194If
195.I pathname
196is relative and
197.I dirfd
198is the special value
199.BR AT_FDCWD ,
200then
201.I pathname
202is interpreted relative to the current working
203directory of the calling process (like
204.BR utimes (2)).
efeece04 205.PP
61dd016f
MK
206If
207.I pathname
208is absolute, then
209.I dirfd
210is ignored.
efeece04 211.PP
61dd016f
MK
212The
213.I flags
214field is a bit mask that may be 0, or include the following constant,
215defined in
216.IR <fcntl.h> :
217.TP
218.B AT_SYMLINK_NOFOLLOW
219If
220.I pathname
221specifies a symbolic link, then update the timestamps of the link,
222rather than the file to which it refers.
47297adb 223.SH RETURN VALUE
61dd016f
MK
224On success,
225.BR utimensat ()
226and
227.BR futimens ()
228return 0.
229On error, \-1 is returned and
230.I errno
231is set to indicate the error.
232.SH ERRORS
233.TP
234.B EACCES
235.I times
236is NULL,
237or both
238.I tv_nsec
239values are
240.BR UTIME_NOW ,
1083ffb4 241and the effective user ID of the caller does not match
61dd016f
MK
242the owner of the file,
243the caller does not have write access to the file,
244and the caller is not privileged
245(Linux: does not have either the
246.B CAP_FOWNER
247or the
248.B CAP_DAC_OVERRIDE
1083ffb4 249capability).
61dd016f
MK
250.\" But Linux 2.6.22 was broken here.
251.\" Traditionally, utime()/utimes() gives the error EACCES for the case
252.\" where the timestamp pointer argument is NULL (i.e., set both timestamps
253.\" to the current time), and the file is owned by a user other than the
254.\" effective UID of the caller, and the file is not writable by the
255.\" effective UID of the program. utimensat() also gives this error in the
256.\" same case. However, in the same circumstances, when utimensat() is
257.\" given a 'times' array in which both tv_nsec fields are UTIME_NOW, which
258.\" provides equivalent functionality to specifying 'times' as NULL, the
259.\" call succeeds. It should fail with the error EACCES in this case.
260.\"
261.\" POSIX.1-2008 has the following:
262.\" .TP
263.\" .B EACCES
264.\" .RB ( utimensat ())
265.\" .I fd
266.\" was not opened with
267.\" .B O_SEARCH
268.\" and the permissions of the directory to which
269.\" .I fd
270.\" refers do not allow searches.
9ee4a2b6 271.\" EXT2_IMMUTABLE_FL and similar flags for other filesystems.
61dd016f 272.RE
61dd016f
MK
273.TP
274.B EBADF
275.RB ( futimens ())
276.I fd
277is not a valid file descriptor.
278.TP
279.B EBADF
280.RB ( utimensat ())
281.I pathname
282is a relative pathname, but
283.I dirfd
284is neither
285.BR AT_FDCWD
286nor a valid file descriptor.
287.TP
288.B EFAULT
289.I times
290pointed to an invalid address; or,
291.I dirfd
292was
293.BR AT_FDCWD ,
294and
295.I pathname
296is NULL or an invalid address.
297.TP
298.B EINVAL
299Invalid value in
300.IR flags .
301.TP
302.B EINVAL
303Invalid value in one of the
304.I tv_nsec
305fields (value outside range 0 to 999,999,999, and not
306.B UTIME_NOW
307or
308.BR UTIME_OMIT );
309or an invalid value in one of the
310.I tv_sec
311fields.
312.TP
313.B EINVAL
314.\" SUSv4 does not specify this error.
315.I pathname
316is NULL,
317.I dirfd
318is not
319.BR AT_FDCWD ,
320and
321.I flags
322contains
323.BR AT_SYMLINK_NOFOLLOW .
324.TP
325.B ELOOP
326.RB ( utimensat ())
327Too many symbolic links were encountered in resolving
328.IR pathname .
329.TP
330.B ENAMETOOLONG
331.RB ( utimensat ())
332.I pathname
333is too long.
334.TP
335.B ENOENT
336.RB ( utimensat ())
337A component of
338.I pathname
339does not refer to an existing directory or file,
340or
341.I pathname
342is an empty string.
343.TP
344.B ENOTDIR
345.RB ( utimensat ())
346.I pathname
347is a relative pathname, but
348.I dirfd
349is neither
350.B AT_FDCWD
351nor a file descriptor referring to a directory;
352or, one of the prefix components of
353.I pathname
354is not a directory.
355.TP
356.B EPERM
357The caller attempted to change one or both timestamps to a value
358other than the current time,
359or to change one of the timestamps to the current time while
360leaving the other timestamp unchanged,
361(i.e.,
362.I times
49f4f33c 363is not NULL, neither
61dd016f 364.I tv_nsec
49f4f33c 365field is
61dd016f 366.BR UTIME_NOW ,
49f4f33c 367and neither
61dd016f 368.I tv_nsec
49f4f33c 369field is
61dd016f 370.BR UTIME_OMIT )
47c3854b 371and either:
61dd016f 372.RS
47c3854b 373.IP * 3
61dd016f
MK
374the caller's effective user ID does not match the owner of file,
375and the caller is not privileged
376(Linux: does not have the
377.BR CAP_FOWNER
378capability); or,
379.IP *
380.\" Linux 2.6.22 was broken here:
381.\" it was not consistent with the old utimes() implementation,
382.\" since the case when both tv_nsec fields are UTIME_NOW, was not
383.\" treated like the (times == NULL) case.
384the file is marked append-only or immutable (see
385.BR chattr (1)).
386.\" EXT2_IMMUTABLE_FL EXT_APPPEND_FL and similar flags for
9ee4a2b6 387.\" other filesystems.
61dd016f
MK
388.\"
389.\" Why the inconsistency (which is described under NOTES) between
390.\" EACCES and EPERM, where only EPERM tests for append-only.
391.\" (This was also so for the older utimes() implementation.)
392.RE
61dd016f
MK
393.TP
394.B EROFS
9ee4a2b6 395The file is on a read-only filesystem.
61dd016f
MK
396.TP
397.B ESRCH
398.RB ( utimensat ())
399Search permission is denied for one of the prefix components of
400.IR pathname .
401.SH VERSIONS
402.BR utimensat ()
403was added to Linux in kernel 2.6.22;
404glibc support was added with version 2.6.
efeece04 405.PP
61dd016f
MK
406Support for
407.BR futimens ()
408first appeared in glibc 2.6.
47f06c75
ZL
409.SH ATTRIBUTES
410For an explanation of the terms used in this section, see
411.BR attributes (7).
412.TS
413allbox;
414lbw23 lb lb
415l l l.
416Interface Attribute Value
417T{
418.BR utimensat (),
419.BR futimens ()
420T} Thread safety MT-Safe
421.TE
efeece04 422.sp 1
47297adb 423.SH CONFORMING TO
61dd016f
MK
424.BR futimens ()
425and
426.BR utimensat ()
5db9708d 427are specified in POSIX.1-2008.
61dd016f
MK
428.SH NOTES
429.BR utimensat ()
430obsoletes
431.BR futimesat (2).
efeece04 432.PP
61dd016f
MK
433On Linux, timestamps cannot be changed for a file marked immutable,
434and the only change permitted for files marked append-only is to
435set the timestamps to the current time.
436(This is consistent with the historical behavior of
437.BR utime (2)
438and
439.BR utimes (2)
440on Linux.)
efeece04 441.PP
2389c1e3
MK
442If both
443.I tv_nsec
444fields are specified as
445.BR UTIME_OMIT ,
446then the Linux implementation of
447.BR utimensat ()
448succeeds even if the file referred to by
449.IR dirfd
450and
451.I pathname
452does not exist.
b146aada 453.SS C library/kernel ABI differences
61dd016f
MK
454On Linux,
455.BR futimens ()
456is a library function implemented on top of the
457.BR utimensat ()
458system call.
459To support this, the Linux
460.BR utimensat ()
c8f2dd47 461system call implements a nonstandard feature: if
61dd016f
MK
462.I pathname
463is NULL, then the call modifies the timestamps of
464the file referred to by the file descriptor
465.I dirfd
466(which may refer to any type of file).
467Using this feature, the call
468.I "futimens(fd,\ times)"
469is implemented as:
207050fa
MK
470.PP
471.in +4n
472.EX
473utimensat(fd, NULL, times, 0);
474.EE
475.in
efeece04 476.PP
0fa34fb3
MK
477Note, however, that the glibc wrapper for
478.BR utimensat ()
479disallows passing NULL as the value for
af6534dd 480.IR pathname :
0fa34fb3
MK
481the wrapper function returns the error
482.IR EINVAL
483in this case.
61dd016f
MK
484.SH BUGS
485Several bugs afflict
486.BR utimensat ()
487and
488.BR futimens ()
489on kernels before 2.6.26.
cfea5132 490These bugs are either nonconformances with the POSIX.1 draft specification
61dd016f 491or inconsistencies with historical Linux behavior.
47c3854b 492.IP * 3
61dd016f
MK
493POSIX.1 specifies that if one of the
494.I tv_nsec
495fields has the value
496.B UTIME_NOW
497or
498.BR UTIME_OMIT ,
499then the value of the corresponding
500.I tv_sec
501field should be ignored.
502Instead, the value of the
503.I tv_sec
504field is required to be 0 (or the error
505.B EINVAL
506results).
507.IP *
508Various bugs mean that for the purposes of permission checking,
509the case where both
510.I tv_nsec
511fields are set to
512.BR UTIME_NOW
513isn't always treated the same as specifying
514.I times
515as NULL,
516and the case where one
517.I tv_nsec
518value is
519.B UTIME_NOW
520and the other is
521.B UTIME_OMIT
522isn't treated the same as specifying
523.I times