summaryrefslogtreecommitdiff
path: root/man2/utimensat.2
blob: a281efff9dc6a684ae1ecaf8f18f565ad1beb64a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
.\" <mtk.manpages@gmail.com>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH UTIMENSAT 2 2012-03-25 "Linux" "Linux Programmer's Manual"
.SH NAME
utimensat, futimens \- change file timestamps with nanosecond precision
.SH SYNOPSIS
.nf
.B #include <fcntl.h>           /* Definition of AT_* constants */
.B #include <sys/stat.h>
.sp
.BI "int utimensat(int " dirfd ", const char *" pathname ,
.BI "              const struct timespec " times "[2], int " flags );

.BI "int futimens(int " fd ", const struct timespec " times [2]);
.fi
.sp
.in -4n
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.in
.ad l
.PD 0
.sp
.BR utimensat ():
.RS 4
.TP 4
Since glibc 2.10:
_XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
.TP
Before glibc 2.10:
_ATFILE_SOURCE
.RE
.PP
.BR futimens ():
.RS 4
.TP
Since glibc 2.10:
_XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
.TP
Before glibc 2.10:
_GNU_SOURCE
.RE
.PD
.ad
.SH DESCRIPTION
.BR utimensat ()
and
.BR futimens ()
update the timestamps of a file with nanosecond precision.
This contrasts with the historical
.BR utime (2)
and
.BR utimes (2),
which permit only second and microsecond precision, respectively,
when setting file timestamps.

With
.BR utimensat ()
the file is specified via the pathname given in
.IR pathname .
With
.BR futimens ()
the file whose timestamps are to be updated is specified via
an open file descriptor,
.IR fd .

For both calls, the new file timestamps are specified in the array
.IR times :
.IR times [0]
specifies the new "last access time" (\fIatime\fP);
.IR times [1]
specifies the new "last modification time" (\fImtime\fP).
Each of the elements of
.I times
specifies a time as the number of seconds and nanoseconds
since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
This information is conveyed in a structure of the following form:
.in +4n
.nf

struct timespec {
    time_t tv_sec;        /* seconds */
    long   tv_nsec;       /* nanoseconds */
};
.fi
.in
.PP
Updated file timestamps are set to the greatest value
supported by the file system that is not greater than the specified time.

If the
.I tv_nsec
field of one of the
.I timespec
structures has the special value
.BR UTIME_NOW ,
then the corresponding file timestamp is set to the current time.
If the
.I tv_nsec
field of one of the
.I timespec
structures has the special value
.BR UTIME_OMIT ,
then the corresponding file timestamp is left unchanged.
In both of these cases, the value of the corresponding
.I tv_sec
.\" 2.6.22 was broken: it is not ignored
field is ignored.

If
.I times
is NULL, then both timestamps are set to the current time.
.\"
.SS Permissions requirements
To set both file timestamps to the current time (i.e.,
.I times
is NULL, or both
.I tv_nsec
fields specify
.BR UTIME_NOW ),
either:
.IP 1. 3
the caller must have write access to the file;
.\" 2.6.22 was broken here -- for futimens() the check is
.\" based on whether or not the file descriptor is writable,
.\" not on whether the caller's effective UID has write
.\" permission for the file referred to by the descriptor.
.IP 2.
the caller's effective user ID must match the owner of the file; or
.IP 3.
the caller must have appropriate privileges.
.PP
To make any change other than setting both timestamps to the
current time (i.e.,
.I times
is not NULL, and both
.I tv_nsec
fields are not
.B UTIME_NOW
.\" 2.6.22 was broken here:
.\" both must something other than *either* UTIME_OMIT *or* UTIME_NOW.
and both
.I tv_nsec
fields are not
.BR UTIME_OMIT ),
either condition 2 or 3 above must apply.

If both
.I tv_nsec
fields are specified as
.BR UTIME_OMIT ,
then no file ownership or permission checks are performed,
and the file timestamps are not modified,
but other error conditions may still be detected.
.\"
.\"
.SS utimensat() specifics
If
.I pathname
is relative, then by default it is interpreted relative to the
directory referred to by the open file descriptor,
.IR dirfd
(rather than relative to the current working directory of
the calling process, as is done by
.BR utimes (2)
for a relative pathname).
See
.BR openat (2)
for an explanation of why this can be useful.
.\" FIXME . Say something about O_SEARCH?  (But it's not in current
.\" glibc (Mar 08), or kernel 2.6.25.)

If
.I pathname
is relative and
.I dirfd
is the special value
.BR AT_FDCWD ,
then
.I pathname
is interpreted relative to the current working
directory of the calling process (like
.BR utimes (2)).

If
.I pathname
is absolute, then
.I dirfd
is ignored.

The
.I flags
field is a bit mask that may be 0, or include the following constant,
defined in
.IR <fcntl.h> :
.TP
.B AT_SYMLINK_NOFOLLOW
If
.I pathname
specifies a symbolic link, then update the timestamps of the link,
rather than the file to which it refers.
.SH "RETURN VALUE"
On success,
.BR utimensat ()
and
.BR futimens ()
return 0.
On error, \-1 is returned and
.I errno
is set to indicate the error.
.SH ERRORS
.TP
.B EACCES
.I times
is NULL,
or both
.I tv_nsec
values are
.BR UTIME_NOW ,
and:
.PD 0
.RS
.IP * 2
the effective user ID of the caller does not match
the owner of the file,
the caller does not have write access to the file,
and the caller is not privileged
(Linux: does not have either the
.B CAP_FOWNER
or the
.B CAP_DAC_OVERRIDE
capability); or,
.\" But Linux 2.6.22 was broken here.
.\" Traditionally, utime()/utimes() gives the error EACCES for the case
.\" where the timestamp pointer argument is NULL (i.e., set both timestamps
.\" to the current time), and the file is owned by a user other than the
.\" effective UID of the caller, and the file is not writable by the
.\" effective UID of the program.  utimensat() also gives this error in the
.\" same case.  However, in the same circumstances, when utimensat() is
.\" given a 'times' array in which both tv_nsec fields are UTIME_NOW, which
.\" provides equivalent functionality to specifying 'times' as NULL, the
.\" call succeeds.  It should fail with the error EACCES in this case.
.\"
.\" POSIX.1-2008 has the following:
.\" .TP
.\" .B EACCES
.\" .RB ( utimensat ())
.\" .I fd
.\" was not opened with
.\" .B O_SEARCH
.\" and the permissions of the directory to which
.\" .I fd
.\" refers do not allow searches.
.IP *
the file is marked immutable (see
.BR chattr (1)).
.\" EXT2_IMMUTABLE_FL and similar flags for other file systems.
.RE
.PD
.TP
.B EBADF
.RB ( futimens ())
.I fd
is not a valid file descriptor.
.TP
.B EBADF
.RB ( utimensat ())
.I pathname
is a relative pathname, but
.I dirfd
is neither
.BR AT_FDCWD
nor a valid file descriptor.
.TP
.B EFAULT
.I times
pointed to an invalid address; or,
.I dirfd
was
.BR AT_FDCWD ,
and
.I pathname
is NULL or an invalid address.
.TP
.B EINVAL
Invalid value in
.IR flags .
.TP
.B EINVAL
Invalid value in one of the
.I tv_nsec
fields (value outside range 0 to 999,999,999, and not
.B UTIME_NOW
or
.BR UTIME_OMIT );
or an invalid value in one of the
.I tv_sec
fields.
.TP
.B EINVAL
.\" SUSv4 does not specify this error.
.I pathname
is NULL,
.I dirfd
is not
.BR AT_FDCWD ,
and
.I flags
contains
.BR AT_SYMLINK_NOFOLLOW .
.TP
.B ELOOP
.RB ( utimensat ())
Too many symbolic links were encountered in resolving
.IR pathname .
.TP
.B ENAMETOOLONG
.RB ( utimensat ())
.I pathname
is too long.
.TP
.B ENOENT
.RB ( utimensat ())
A component of
.I pathname
does not refer to an existing directory or file,
or
.I pathname
is an empty string.
.TP
.B ENOTDIR
.RB ( utimensat ())
.I pathname
is a relative pathname, but
.I dirfd
is neither
.B AT_FDCWD
nor a file descriptor referring to a directory;
or, one of the prefix components of
.I pathname
is not a directory.
.TP
.B EPERM
The caller attempted to change one or both timestamps to a value
other than the current time,
or to change one of the timestamps to the current time while
leaving the other timestamp unchanged,
(i.e.,
.I times
is not NULL, both
.I tv_nsec
fields are not
.BR UTIME_NOW ,
and both
.I tv_nsec
fields are not
.BR UTIME_OMIT )
and:
.PD 0
.RS
.IP * 2
the caller's effective user ID does not match the owner of file,
and the caller is not privileged
(Linux: does not have the
.BR CAP_FOWNER
capability); or,
.IP *
.\" Linux 2.6.22 was broken here:
.\" it was not consistent with the old utimes() implementation,
.\" since the case when both tv_nsec fields are UTIME_NOW, was not
.\" treated like the (times == NULL) case.
the file is marked append-only or immutable (see
.BR chattr (1)).
.\" EXT2_IMMUTABLE_FL EXT_APPPEND_FL and similar flags for
.\" other file systems.
.\"
.\" Why the inconsistency (which is described under NOTES) between
.\" EACCES and EPERM, where only EPERM tests for append-only.
.\" (This was also so for the older utimes() implementation.)
.RE
.PD
.TP
.B EROFS
The file is on a read-only file system.
.TP
.B ESRCH
.RB ( utimensat ())
Search permission is denied for one of the prefix components of
.IR pathname .
.SH VERSIONS
.BR utimensat ()
was added to Linux in kernel 2.6.22;
glibc support was added with version 2.6.

Support for
.BR futimens ()
first appeared in glibc 2.6.
.SH "CONFORMING TO"
.BR futimens ()
and
.BR utimensat ()
are specified in POSIX.1-2008.
.SH NOTES
.BR utimensat ()
obsoletes
.BR futimesat (2).

On Linux, timestamps cannot be changed for a file marked immutable,
and the only change permitted for files marked append-only is to
set the timestamps to the current time.
(This is consistent with the historical behavior of
.BR utime (2)
and
.BR utimes (2)
on Linux.)

On Linux,
.BR futimens ()
is a library function implemented on top of the
.BR utimensat ()
system call.
To support this, the Linux
.BR utimensat ()
system call implements a nonstandard feature: if
.I pathname
is NULL, then the call modifies the timestamps of
the file referred to by the file descriptor
.I dirfd
(which may refer to any type of file).
Using this feature, the call
.I "futimens(fd,\ times)"
is implemented as:
.nf

    utimensat(fd, NULL, times, 0);
.fi
.SH BUGS
Several bugs afflict
.BR utimensat ()
and
.BR futimens ()
on kernels before 2.6.26.
These bugs are either nonconformances with the POSIX.1 draft specification
or inconsistencies with historical Linux behavior.
.IP * 2
POSIX.1 specifies that if one of the
.I tv_nsec
fields has the value
.B UTIME_NOW
or
.BR UTIME_OMIT ,
then the value of the corresponding
.I tv_sec
field should be ignored.
Instead, the value of the
.I tv_sec
field is required to be 0 (or the error
.B EINVAL
results).
.IP *
Various bugs mean that for the purposes of permission checking,
the case where both
.I tv_nsec
fields are set to
.BR UTIME_NOW
isn't always treated the same as specifying
.I times
as NULL,
and the case where one
.I tv_nsec
value is
.B UTIME_NOW
and the other is
.B UTIME_OMIT
isn't treated the same as specifying
.I times
as a pointer to an array of structures containing arbitrary time values.
As a result, in some cases:
a) file timestamps can be updated by a process that shouldn't have
permission to perform updates;
b) file timestamps can't be updated by a process that should have
permission to perform updates; and
c) the wrong
.I errno
value is returned in case of an error.
.\" Below, the long description of the errors from the previous bullet
.\" point (abridged because it's too much detail for a man page).
.\" .IP *
.\" If one of the
.\" .I tv_nsec
.\" fields is
.\" .BR UTIME_OMIT
.\" and the other is
.\" .BR UTIME_NOW ,
.\" then the error
.\" .B EPERM
.\" should occur if the process's effective user ID does not match
.\" the file owner and the process is not privileged.
.\" Instead, the call successfully changes one of the timestamps.
.\" .IP *
.\" If file is not writable by the effective user ID of the process and
.\" the process's effective user ID does not match the file owner and
.\" the process is not privileged,
.\" and
.\" .I times
.\" is NULL, then the error
.\" .B EACCES
.\" results.
.\" This error should also occur if
.\" .I times
.\" points to an array of structures in which both
.\" .I tv_nsec
.\" fields are
.\" .BR UTIME_NOW .
.\" Instead the call succeeds.
.\" .IP *
.\" If a file is marked as append-only (see
.\" .BR chattr (1)),
.\" then Linux traditionally
.\" (i.e.,
.\" .BR utime (2),
.\" .BR utimes (2)),
.\" permits a NULL
.\" .I times
.\" argument to be used in order to update both timestamps to the current time.
.\" For consistency,
.\" .BR utimensat ()
.\" and
.\" .BR futimens ()
.\" should also produce the same result when given a
.\" .I times
.\" argument that points to an array of structures in which both
.\" .I tv_nsec
.\" fields are
.\" .BR UTIME_NOW .
.\" Instead, the call fails with the error
.\" .BR EPERM .
.\" .IP *
.\" If a file is marked as immutable (see
.\" .BR chattr (1)),
.\" then Linux traditionally
.\" (i.e.,
.\" .BR utime (2),
.\" .BR utimes (2)),
.\" gives an
.\" .B EACCES
.\" error if
.\" .I times
.\" is NULL.
.\" For consistency,
.\" .BR utimensat ()
.\" and
.\" .BR futimens ()
.\" should also produce the same result when given a
.\" .I times
.\" that points to an array of structures in which both
.\" .I tv_nsec
.\" fields are
.\" .BR UTIME_NOW .
.\" Instead, the call fails with the error
.\" .BR EPERM .
.IP *
POSIX.1 says that a process that has \fIwrite access to the file\fP
can make a call with
.I times
as NULL, or with
.I times
pointing to an array of structures in which both
.I tv_nsec
fields are
.BR UTIME_NOW ,
in order to update both timestamps to the current time.
However,
.BR futimens ()
instead checks whether the
.IR "access mode of the file descriptor allows writing" .
.\" This means that a process with a file descriptor that allows
.\" writing could change the timestamps of a file for which it
.\" does not have write permission;
.\" conversely, a process with a read-only file descriptor won't
.\" be able to update the timestamps of a file,
.\" even if it has write permission on the file.
.SH "SEE ALSO"
.BR chattr (1),
.BR futimesat (2),
.BR openat (2),
.BR stat (2),
.BR utimes (2),
.BR futimes (3),
.BR path_resolution (7),
.BR symlink (7)