2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / futimes.c
blob272b83eb05ce28e163cb74594d6863db9140b002
1 /* futimes -- change access and modification times of open file. Linux version.
2 Copyright (C) 2002,2003,2005,2006,2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <sysdep.h>
22 #include <string.h>
23 #include <time.h>
24 #include <utime.h>
25 #include <sys/time.h>
26 #include <stdio-common/_itoa.h>
27 #include <fcntl.h>
29 #include <kernel-features.h>
32 #if defined __NR_utimensat && !defined __ASSUME_UTIMENSAT
33 static int miss_utimensat;
34 #endif
36 /* Change the access time of the file associated with FD to TVP[0] and
37 the modification time of FILE to TVP[1].
39 Starting with 2.6.22 the Linux kernel has the utimensat syscall which
40 can be used to implement futimes. Earlier kernels have no futimes()
41 syscall so we use the /proc filesystem. */
42 int
43 __futimes (int fd, const struct timeval tvp[2])
45 /* The utimensat system call expects timespec not timeval. */
46 struct timespec ts[2];
47 if (tvp != NULL)
49 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000
50 || tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
52 __set_errno (EINVAL);
53 return -1;
56 TIMEVAL_TO_TIMESPEC (&tvp[0], &ts[0]);
57 TIMEVAL_TO_TIMESPEC (&tvp[1], &ts[1]);
60 #ifdef __ASSUME_UTIMENSAT
61 return INLINE_SYSCALL (utimensat, 4, fd, NULL, tvp ? &ts : NULL, 0);
62 #else
63 int result;
64 # ifdef __NR_utimensat
65 if (!__builtin_expect (miss_utimensat, 0))
67 result = INLINE_SYSCALL (utimensat, 4, fd, NULL, tvp ? &ts : NULL, 0);
68 if (__builtin_expect (result, 0) != -1 || errno != ENOSYS)
69 return result;
71 miss_utimensat = 1;
73 # endif
75 static const char selffd[] = "/proc/self/fd/";
76 char fname[sizeof (selffd) + 3 * sizeof (int)];
77 fname[sizeof (fname) - 1] = '\0';
78 char *cp = _itoa_word ((unsigned int) fd, fname + sizeof (fname) - 1, 10, 0);
79 cp = memcpy (cp - sizeof (selffd) + 1, selffd, sizeof (selffd) - 1);
81 # ifdef __NR_utimes
82 result = INLINE_SYSCALL (utimes, 2, cp, tvp);
83 # ifndef __ASSUME_UTIMES
84 if (result == -1 && errno == ENOSYS)
85 # endif
86 # endif
88 /* The utimes() syscall does not exist or is not available in the
89 used kernel. Use utime(). For this we have to convert to the
90 data format utime() expects. */
91 # ifndef __ASSUME_UTIMES
92 struct utimbuf buf;
93 struct utimbuf *times;
95 if (tvp != NULL)
97 times = &buf;
98 buf.actime = tvp[0].tv_sec + (tvp[0].tv_usec + 500000) / 1000000;
99 buf.modtime = tvp[1].tv_sec + (tvp[1].tv_usec + 500000) / 1000000;
101 else
102 times = NULL;
104 result = INLINE_SYSCALL (utime, 2, cp, times);
105 # endif
108 if (result == -1)
109 /* Check for errors that result from failing to find /proc.
110 This means we can't do futimes at all, so return ENOSYS
111 rather than some confusing error. */
112 switch (errno)
114 case EACCES:
115 if (tvp == NULL) /* Could be a path problem or a file problem. */
116 break;
117 /*FALLTHROUGH*/
118 case ELOOP:
119 case ENAMETOOLONG:
120 case ENOTDIR:
121 __set_errno (ENOSYS);
122 break;
124 case ENOENT:
125 /* Validate the file descriptor by letting fcntl set errno to
126 EBADF if it's bogus. Otherwise it's a /proc issue. */
127 # if !defined __NR_fcntl && defined __NR_fcntl64
128 # define __NR_fcntl __NR_fcntl64
129 # endif
130 if (INLINE_SYSCALL (fcntl, 3, fd, F_GETFD, 0) != -1)
131 __set_errno (ENOSYS);
132 break;
135 return result;
136 #endif
138 weak_alias (__futimes, futimes)