Update copyright notices with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / futimes.c
blobd37950286967eb3c10eeeb3969712a549dc1d1af
1 /* futimes -- change access and modification times of open file. Linux version.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <sysdep.h>
21 #include <string.h>
22 #include <time.h>
23 #include <utime.h>
24 #include <sys/time.h>
25 #include <_itoa.h>
26 #include <fcntl.h>
28 #include <kernel-features.h>
31 #if defined __NR_utimensat && !defined __ASSUME_UTIMENSAT
32 static int miss_utimensat;
33 #endif
35 /* Change the access time of the file associated with FD to TVP[0] and
36 the modification time of FILE to TVP[1].
38 Starting with 2.6.22 the Linux kernel has the utimensat syscall which
39 can be used to implement futimes. Earlier kernels have no futimes()
40 syscall so we use the /proc filesystem. */
41 int
42 __futimes (int fd, const struct timeval tvp[2])
44 /* The utimensat system call expects timespec not timeval. */
45 struct timespec ts[2];
46 if (tvp != NULL)
48 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000
49 || tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
51 __set_errno (EINVAL);
52 return -1;
55 TIMEVAL_TO_TIMESPEC (&tvp[0], &ts[0]);
56 TIMEVAL_TO_TIMESPEC (&tvp[1], &ts[1]);
59 #ifdef __ASSUME_UTIMENSAT
60 return INLINE_SYSCALL (utimensat, 4, fd, NULL, tvp ? &ts : NULL, 0);
61 #else
62 int result;
63 # ifdef __NR_utimensat
64 if (!__builtin_expect (miss_utimensat, 0))
66 result = INLINE_SYSCALL (utimensat, 4, fd, NULL, tvp ? &ts : NULL, 0);
67 if (__builtin_expect (result, 0) != -1 || errno != ENOSYS)
68 return result;
70 miss_utimensat = 1;
72 # endif
74 static const char selffd[] = "/proc/self/fd/";
75 char fname[sizeof (selffd) + 3 * sizeof (int)];
76 fname[sizeof (fname) - 1] = '\0';
77 char *cp = _itoa_word ((unsigned int) fd, fname + sizeof (fname) - 1, 10, 0);
78 cp = memcpy (cp - sizeof (selffd) + 1, selffd, sizeof (selffd) - 1);
80 # ifdef __NR_utimes
81 result = INLINE_SYSCALL (utimes, 2, cp, tvp);
82 # ifndef __ASSUME_UTIMES
83 if (result == -1 && errno == ENOSYS)
84 # endif
85 # endif
87 /* The utimes() syscall does not exist or is not available in the
88 used kernel. Use utime(). For this we have to convert to the
89 data format utime() expects. */
90 # ifndef __ASSUME_UTIMES
91 struct utimbuf buf;
92 struct utimbuf *times;
94 if (tvp != NULL)
96 times = &buf;
97 buf.actime = tvp[0].tv_sec;
98 buf.modtime = tvp[1].tv_sec;
100 else
101 times = NULL;
103 result = INLINE_SYSCALL (utime, 2, cp, times);
104 # endif
107 if (result == -1)
108 /* Check for errors that result from failing to find /proc.
109 This means we can't do futimes at all, so return ENOSYS
110 rather than some confusing error. */
111 switch (errno)
113 case EACCES:
114 if (tvp == NULL) /* Could be a path problem or a file problem. */
115 break;
116 /*FALLTHROUGH*/
117 case ELOOP:
118 case ENAMETOOLONG:
119 case ENOTDIR:
120 __set_errno (ENOSYS);
121 break;
123 case ENOENT:
124 /* Validate the file descriptor by letting fcntl set errno to
125 EBADF if it's bogus. Otherwise it's a /proc issue. */
126 # if !defined __NR_fcntl && defined __NR_fcntl64
127 # define __NR_fcntl __NR_fcntl64
128 # endif
129 if (INLINE_SYSCALL (fcntl, 3, fd, F_GETFD, 0) != -1)
130 __set_errno (ENOSYS);
131 break;
134 return result;
135 #endif
137 weak_alias (__futimes, futimes)