1 .\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH utimensat 2 (date) "Linux man-pages (unreleased)"
8 utimensat, futimens \- change file timestamps with nanosecond precision
11 .RI ( libc ", " \-lc )
14 .BR "#include <fcntl.h>" " /* Definition of " AT_* " constants */"
15 .B #include <sys/stat.h>
17 .BI "int utimensat(int " dirfd ", const char *" pathname ,
18 .BI " const struct timespec " times "[2], int " flags );
19 .BI "int futimens(int " fd ", const struct timespec " times [2]);
23 Feature Test Macro Requirements for glibc (see
24 .BR feature_test_macros (7)):
30 _POSIX_C_SOURCE >= 200809L
38 _POSIX_C_SOURCE >= 200809L
46 update the timestamps of a file with nanosecond precision.
47 This contrasts with the historical
51 which permit only second and microsecond precision, respectively,
52 when setting file timestamps.
56 the file is specified via the pathname given in
60 the file whose timestamps are to be updated is specified via
61 an open file descriptor,
64 For both calls, the new file timestamps are specified in the array
67 specifies the new "last access time" (\fIatime\fP);
69 specifies the new "last modification time" (\fImtime\fP).
70 Each of the elements of
72 specifies a time as the number of seconds and nanoseconds
73 since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
74 This information is conveyed in a
78 Updated file timestamps are set to the greatest value
79 supported by the filesystem that is not greater than the specified time.
85 structures has the special value
87 then the corresponding file timestamp is set to the current time.
92 structures has the special value
94 then the corresponding file timestamp is left unchanged.
95 In both of these cases, the value of the corresponding
97 .\" 2.6.22 was broken: it is not ignored
102 is NULL, then both timestamps are set to the current time.
105 The status change time (ctime) will be set to the current time, even if the
106 other time stamps don't actually change.
107 .SS Permissions requirements
108 To set both file timestamps to the current time (i.e.,
116 the caller must have write access to the file;
117 .\" 2.6.22 was broken here -- for futimens() the check is
118 .\" based on whether or not the file descriptor is writable,
119 .\" not on whether the caller's effective UID has write
120 .\" permission for the file referred to by the descriptor.
122 the caller's effective user ID must match the owner of the file; or
124 the caller must have appropriate privileges.
126 To make any change other than setting both timestamps to the
129 is not NULL, and neither
133 .\" 2.6.22 was broken here:
134 .\" both must be something other than *either* UTIME_OMIT *or* UTIME_NOW.
139 either condition 2 or 3 above must apply.
143 fields are specified as
145 then no file ownership or permission checks are performed,
146 and the file timestamps are not modified,
147 but other error conditions may still be detected.
150 .SS utimensat() specifics
153 is relative, then by default it is interpreted relative to the
154 directory referred to by the open file descriptor,
156 (rather than relative to the current working directory of
157 the calling process, as is done by
159 for a relative pathname).
162 for an explanation of why this can be useful.
172 is interpreted relative to the current working
173 directory of the calling process (like
184 field is a bit mask that may be 0, or include the following constant,
188 .B AT_SYMLINK_NOFOLLOW
191 specifies a symbolic link, then update the timestamps of the link,
192 rather than the file to which it refers.
199 On error, \-1 is returned and
201 is set to indicate the error.
211 and the effective user ID of the caller does not match
212 the owner of the file,
213 the caller does not have write access to the file,
214 and the caller is not privileged
215 (Linux: does not have either the
220 .\" But Linux 2.6.22 was broken here.
221 .\" Traditionally, utime()/utimes() gives the error EACCES for the case
222 .\" where the timestamp pointer argument is NULL (i.e., set both timestamps
223 .\" to the current time), and the file is owned by a user other than the
224 .\" effective UID of the caller, and the file is not writable by the
225 .\" effective UID of the program. utimensat() also gives this error in the
226 .\" same case. However, in the same circumstances, when utimensat() is
227 .\" given a 'times' array in which both tv_nsec fields are UTIME_NOW, which
228 .\" provides equivalent functionality to specifying 'times' as NULL, the
229 .\" call succeeds. It should fail with the error EACCES in this case.
231 .\" POSIX.1-2008 has the following:
234 .\" .RB ( utimensat ())
236 .\" was not opened with
238 .\" and the permissions of the directory to which
240 .\" refers do not allow searches.
241 .\" EXT2_IMMUTABLE_FL and similar flags for other filesystems.
246 is not a valid file descriptor.
255 nor a valid file descriptor.
259 pointed to an invalid address; or,
265 is NULL or an invalid address.
272 Invalid value in one of the
274 fields (value outside range 0 to 999,999,999, and not
278 or an invalid value in one of the
283 .\" SUSv4 does not specify this error.
292 .BR AT_SYMLINK_NOFOLLOW .
296 Too many symbolic links were encountered in resolving
308 does not refer to an existing directory or file,
316 is a relative pathname, but
320 nor a file descriptor referring to a directory;
321 or, one of the prefix components of
326 The caller attempted to change one or both timestamps to a value
327 other than the current time,
328 or to change one of the timestamps to the current time while
329 leaving the other timestamp unchanged,
343 the caller's effective user ID does not match the owner of file,
344 and the caller is not privileged
345 (Linux: does not have the
349 .\" Linux 2.6.22 was broken here:
350 .\" it was not consistent with the old utimes() implementation,
351 .\" since the case when both tv_nsec fields are UTIME_NOW, was not
352 .\" treated like the (times == NULL) case.
353 the file is marked append-only or immutable (see
355 .\" EXT2_IMMUTABLE_FL EXT_APPPEND_FL and similar flags for
356 .\" other filesystems.
358 .\" Why the inconsistency (which is described under NOTES) between
359 .\" EACCES and EPERM, where only EPERM tests for append-only.
360 .\" (This was also so for the older utimes() implementation.)
364 The file is on a read-only filesystem.
368 Search permission is denied for one of the prefix components of
372 was added to Linux in kernel 2.6.22;
373 glibc support was added with version 2.6.
377 first appeared in glibc 2.6.
379 For an explanation of the terms used in this section, see
387 Interface Attribute Value
391 T} Thread safety MT-Safe
400 are specified in POSIX.1-2008.
406 On Linux, timestamps cannot be changed for a file marked immutable,
407 and the only change permitted for files marked append-only is to
408 set the timestamps to the current time.
409 (This is consistent with the historical behavior of
417 fields are specified as
419 then the Linux implementation of
421 succeeds even if the file referred to by
426 .SS C library/kernel ABI differences
429 is a library function implemented on top of the
432 To support this, the Linux
434 system call implements a nonstandard feature: if
436 is NULL, then the call modifies the timestamps of
437 the file referred to by the file descriptor
439 (which may refer to any type of file).
440 Using this feature, the call
441 .I "futimens(fd,\ times)"
446 utimensat(fd, NULL, times, 0);
450 Note, however, that the glibc wrapper for
452 disallows passing NULL as the value for
454 the wrapper function returns the error
462 on kernels before 2.6.26.
463 These bugs are either nonconformances with the POSIX.1 draft specification
464 or inconsistencies with historical Linux behavior.
466 POSIX.1 specifies that if one of the
472 then the value of the corresponding
474 field should be ignored.
475 Instead, the value of the
477 field is required to be 0 (or the error
481 Various bugs mean that for the purposes of permission checking,
486 isn't always treated the same as specifying
489 and the case where one
495 isn't treated the same as specifying
497 as a pointer to an array of structures containing arbitrary time values.
498 As a result, in some cases:
499 a) file timestamps can be updated by a process that shouldn't have
500 permission to perform updates;
501 b) file timestamps can't be updated by a process that should have
502 permission to perform updates; and
505 value is returned in case of an error.
506 .\" Below, the long description of the errors from the previous bullet
507 .\" point (abridged because it's too much detail for a man page).
517 .\" should occur if the process's effective user ID does not match
518 .\" the file owner and the process is not privileged.
519 .\" Instead, the call successfully changes one of the timestamps.
521 .\" If file is not writable by the effective user ID of the process and
522 .\" the process's effective user ID does not match the file owner and
523 .\" the process is not privileged,
526 .\" is NULL, then the error
529 .\" This error should also occur if
531 .\" points to an array of structures in which both
535 .\" Instead the call succeeds.
537 .\" If a file is marked as append-only (see
539 .\" then Linux traditionally
545 .\" argument to be used in order to update both timestamps to the current time.
550 .\" should also produce the same result when given a
552 .\" argument that points to an array of structures in which both
556 .\" Instead, the call fails with the error
559 .\" If a file is marked as immutable (see
561 .\" then Linux traditionally
574 .\" should also produce the same result when given a
576 .\" that points to an array of structures in which both
580 .\" Instead, the call fails with the error
583 POSIX.1 says that a process that has \fIwrite access to the file\fP
588 pointing to an array of structures in which both
592 in order to update both timestamps to the current time.
595 instead checks whether the
596 .IR "access mode of the file descriptor allows writing" .
597 .\" This means that a process with a file descriptor that allows
598 .\" writing could change the timestamps of a file for which it
599 .\" does not have write permission;
600 .\" conversely, a process with a read-only file descriptor won't
601 .\" be able to update the timestamps of a file,
602 .\" even if it has write permission on the file.
613 .BR path_resolution (7),