1 .\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH UTIMENSAT 2 2021-08-27 "Linux" "Linux Programmer's Manual"
28 utimensat, futimens \- change file timestamps with nanosecond precision
31 .BR "#include <fcntl.h>" " /* Definition of " AT_* " constants */"
32 .B #include <sys/stat.h>
34 .BI "int utimensat(int " dirfd ", const char *" pathname ,
35 .BI " const struct timespec " times "[2], int " flags );
36 .BI "int futimens(int " fd ", const struct timespec " times [2]);
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
47 _POSIX_C_SOURCE >= 200809L
55 _POSIX_C_SOURCE >= 200809L
63 update the timestamps of a file with nanosecond precision.
64 This contrasts with the historical
68 which permit only second and microsecond precision, respectively,
69 when setting file timestamps.
73 the file is specified via the pathname given in
77 the file whose timestamps are to be updated is specified via
78 an open file descriptor,
81 For both calls, the new file timestamps are specified in the array
84 specifies the new "last access time" (\fIatime\fP);
86 specifies the new "last modification time" (\fImtime\fP).
87 Each of the elements of
89 specifies a time as the number of seconds and nanoseconds
90 since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
91 This information is conveyed in a structure of the following form:
96 time_t tv_sec; /* seconds */
97 long tv_nsec; /* nanoseconds */
102 Updated file timestamps are set to the greatest value
103 supported by the filesystem that is not greater than the specified time.
109 structures has the special value
111 then the corresponding file timestamp is set to the current time.
116 structures has the special value
118 then the corresponding file timestamp is left unchanged.
119 In both of these cases, the value of the corresponding
121 .\" 2.6.22 was broken: it is not ignored
126 is NULL, then both timestamps are set to the current time.
128 .SS Permissions requirements
129 To set both file timestamps to the current time (i.e.,
137 the caller must have write access to the file;
138 .\" 2.6.22 was broken here -- for futimens() the check is
139 .\" based on whether or not the file descriptor is writable,
140 .\" not on whether the caller's effective UID has write
141 .\" permission for the file referred to by the descriptor.
143 the caller's effective user ID must match the owner of the file; or
145 the caller must have appropriate privileges.
147 To make any change other than setting both timestamps to the
150 is not NULL, and neither
154 .\" 2.6.22 was broken here:
155 .\" both must be something other than *either* UTIME_OMIT *or* UTIME_NOW.
160 either condition 2 or 3 above must apply.
164 fields are specified as
166 then no file ownership or permission checks are performed,
167 and the file timestamps are not modified,
168 but other error conditions may still be detected.
171 .SS utimensat() specifics
174 is relative, then by default it is interpreted relative to the
175 directory referred to by the open file descriptor,
177 (rather than relative to the current working directory of
178 the calling process, as is done by
180 for a relative pathname).
183 for an explanation of why this can be useful.
193 is interpreted relative to the current working
194 directory of the calling process (like
205 field is a bit mask that may be 0, or include the following constant,
209 .B AT_SYMLINK_NOFOLLOW
212 specifies a symbolic link, then update the timestamps of the link,
213 rather than the file to which it refers.
220 On error, \-1 is returned and
222 is set to indicate the error.
232 and the effective user ID of the caller does not match
233 the owner of the file,
234 the caller does not have write access to the file,
235 and the caller is not privileged
236 (Linux: does not have either the
241 .\" But Linux 2.6.22 was broken here.
242 .\" Traditionally, utime()/utimes() gives the error EACCES for the case
243 .\" where the timestamp pointer argument is NULL (i.e., set both timestamps
244 .\" to the current time), and the file is owned by a user other than the
245 .\" effective UID of the caller, and the file is not writable by the
246 .\" effective UID of the program. utimensat() also gives this error in the
247 .\" same case. However, in the same circumstances, when utimensat() is
248 .\" given a 'times' array in which both tv_nsec fields are UTIME_NOW, which
249 .\" provides equivalent functionality to specifying 'times' as NULL, the
250 .\" call succeeds. It should fail with the error EACCES in this case.
252 .\" POSIX.1-2008 has the following:
255 .\" .RB ( utimensat ())
257 .\" was not opened with
259 .\" and the permissions of the directory to which
261 .\" refers do not allow searches.
262 .\" EXT2_IMMUTABLE_FL and similar flags for other filesystems.
267 is not a valid file descriptor.
276 nor a valid file descriptor.
280 pointed to an invalid address; or,
286 is NULL or an invalid address.
293 Invalid value in one of the
295 fields (value outside range 0 to 999,999,999, and not
299 or an invalid value in one of the
304 .\" SUSv4 does not specify this error.
313 .BR AT_SYMLINK_NOFOLLOW .
317 Too many symbolic links were encountered in resolving
329 does not refer to an existing directory or file,
337 is a relative pathname, but
341 nor a file descriptor referring to a directory;
342 or, one of the prefix components of
347 The caller attempted to change one or both timestamps to a value
348 other than the current time,
349 or to change one of the timestamps to the current time while
350 leaving the other timestamp unchanged,
364 the caller's effective user ID does not match the owner of file,
365 and the caller is not privileged
366 (Linux: does not have the
370 .\" Linux 2.6.22 was broken here:
371 .\" it was not consistent with the old utimes() implementation,
372 .\" since the case when both tv_nsec fields are UTIME_NOW, was not
373 .\" treated like the (times == NULL) case.
374 the file is marked append-only or immutable (see
376 .\" EXT2_IMMUTABLE_FL EXT_APPPEND_FL and similar flags for
377 .\" other filesystems.
379 .\" Why the inconsistency (which is described under NOTES) between
380 .\" EACCES and EPERM, where only EPERM tests for append-only.
381 .\" (This was also so for the older utimes() implementation.)
385 The file is on a read-only filesystem.
389 Search permission is denied for one of the prefix components of
393 was added to Linux in kernel 2.6.22;
394 glibc support was added with version 2.6.
398 first appeared in glibc 2.6.
400 For an explanation of the terms used in this section, see
408 Interface Attribute Value
412 T} Thread safety MT-Safe
421 are specified in POSIX.1-2008.
427 On Linux, timestamps cannot be changed for a file marked immutable,
428 and the only change permitted for files marked append-only is to
429 set the timestamps to the current time.
430 (This is consistent with the historical behavior of
438 fields are specified as
440 then the Linux implementation of
442 succeeds even if the file referred to by
447 .SS C library/kernel ABI differences
450 is a library function implemented on top of the
453 To support this, the Linux
455 system call implements a nonstandard feature: if
457 is NULL, then the call modifies the timestamps of
458 the file referred to by the file descriptor
460 (which may refer to any type of file).
461 Using this feature, the call
462 .I "futimens(fd,\ times)"
467 utimensat(fd, NULL, times, 0);
471 Note, however, that the glibc wrapper for
473 disallows passing NULL as the value for
475 the wrapper function returns the error
483 on kernels before 2.6.26.
484 These bugs are either nonconformances with the POSIX.1 draft specification
485 or inconsistencies with historical Linux behavior.
487 POSIX.1 specifies that if one of the
493 then the value of the corresponding
495 field should be ignored.
496 Instead, the value of the
498 field is required to be 0 (or the error
502 Various bugs mean that for the purposes of permission checking,
507 isn't always treated the same as specifying
510 and the case where one
516 isn't treated the same as specifying
518 as a pointer to an array of structures containing arbitrary time values.
519 As a result, in some cases:
520 a) file timestamps can be updated by a process that shouldn't have
521 permission to perform updates;
522 b) file timestamps can't be updated by a process that should have
523 permission to perform updates; and
526 value is returned in case of an error.
527 .\" Below, the long description of the errors from the previous bullet
528 .\" point (abridged because it's too much detail for a man page).
538 .\" should occur if the process's effective user ID does not match
539 .\" the file owner and the process is not privileged.
540 .\" Instead, the call successfully changes one of the timestamps.
542 .\" If file is not writable by the effective user ID of the process and
543 .\" the process's effective user ID does not match the file owner and
544 .\" the process is not privileged,
547 .\" is NULL, then the error
550 .\" This error should also occur if
552 .\" points to an array of structures in which both
556 .\" Instead the call succeeds.
558 .\" If a file is marked as append-only (see
560 .\" then Linux traditionally
566 .\" argument to be used in order to update both timestamps to the current time.
571 .\" should also produce the same result when given a
573 .\" argument that points to an array of structures in which both
577 .\" Instead, the call fails with the error
580 .\" If a file is marked as immutable (see
582 .\" then Linux traditionally
595 .\" should also produce the same result when given a
597 .\" that points to an array of structures in which both
601 .\" Instead, the call fails with the error
604 POSIX.1 says that a process that has \fIwrite access to the file\fP
609 pointing to an array of structures in which both
613 in order to update both timestamps to the current time.
616 instead checks whether the
617 .IR "access mode of the file descriptor allows writing" .
618 .\" This means that a process with a file descriptor that allows
619 .\" writing could change the timestamps of a file for which it
620 .\" does not have write permission;
621 .\" conversely, a process with a read-only file descriptor won't
622 .\" be able to update the timestamps of a file,
623 .\" even if it has write permission on the file.
633 .BR path_resolution (7),