summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/syscall/utime.c
blob: 38cf6069f76095206e8ae6a54e45a9b5ee8b4b42 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Portions of this source code were derived from Berkeley 4.3 BSD
 * under license from the Regents of the University of California.
 */

#include <sys/param.h>
#include <sys/isa_defs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/vfs.h>
#include <sys/time.h>
#include <sys/debug.h>
#include <sys/model.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/pathname.h>
#include <c2/audit.h>

static int
cfutimesat(int fd, char *fname, int nmflag, vattr_t *vap, int flags, int follow)
{
	file_t *fp;
	vnode_t *startvp, *vp;
	int error;
	char startchar;

	if (fd == AT_FDCWD && fname == NULL)
		return (set_errno(EFAULT));

	if (nmflag == 1 || (nmflag == 2 && fname != NULL)) {
		if (copyin(fname, &startchar, sizeof (char)))
			return (set_errno(EFAULT));
	} else {
		startchar = '\0';
	}

	if (fd == AT_FDCWD) {
		startvp = NULL;
	} else {
		/*
		 * is this absolute path?
		 */
		if (startchar != '/') {
			if ((fp = getf(fd)) == NULL)
				return (set_errno(EBADF));
			startvp = fp->f_vnode;
			VN_HOLD(startvp);
			releasef(fd);
		} else {
			startvp = NULL;
		}
	}

	if (audit_active)
		audit_setfsat_path(1);

	if ((nmflag == 1) || ((nmflag == 2) && (fname != NULL))) {
		if ((error = lookupnameat(fname, UIO_USERSPACE,
		    follow, NULLVPP, &vp, startvp)) != 0) {
			if (startvp != NULL)
				VN_RELE(startvp);
			return (set_errno(error));
		}
	} else {
		vp = startvp;
		VN_HOLD(vp);
	}

	if (startvp != NULL) {
		VN_RELE(startvp);
	}

	if (vn_is_readonly(vp)) {
		error = EROFS;
	} else {
		error = VOP_SETATTR(vp, vap, flags, CRED(), NULL);
	}

	VN_RELE(vp);
	if (error != 0)
		return (set_errno(error));
	return (0);
}

/*
 * Expunge this function when futimesat() and utimes()
 * are expunged from the kernel.
 */
static int
get_timeval_vattr(struct timeval *tvptr, struct vattr *vattr, int *flags)
{
	struct timeval tv[2];

	if (tvptr != NULL) {
		if (get_udatamodel() == DATAMODEL_NATIVE) {
			if (copyin(tvptr, tv, sizeof (tv)))
				return (EFAULT);
		} else {
			struct timeval32 tv32[2];

			if (copyin(tvptr, tv32, sizeof (tv32)))
				return (EFAULT);

			TIMEVAL32_TO_TIMEVAL(&tv[0], &tv32[0]);
			TIMEVAL32_TO_TIMEVAL(&tv[1], &tv32[1]);
		}

		if (tv[0].tv_usec < 0 || tv[0].tv_usec >= MICROSEC ||
		    tv[1].tv_usec < 0 || tv[1].tv_usec >= MICROSEC)
			return (EINVAL);

		vattr->va_atime.tv_sec = tv[0].tv_sec;
		vattr->va_atime.tv_nsec = tv[0].tv_usec * 1000;
		vattr->va_mtime.tv_sec = tv[1].tv_sec;
		vattr->va_mtime.tv_nsec = tv[1].tv_usec * 1000;
		*flags = ATTR_UTIME;
	} else {
		gethrestime(&vattr->va_atime);
		vattr->va_mtime = vattr->va_atime;
		*flags = 0;
	}
	vattr->va_mask = AT_ATIME | AT_MTIME;

	return (0);
}

static int
get_timespec_vattr(timespec_t *tsptr, struct vattr *vattr, int *flags)
{
	timespec_t ts[2];
	timespec_t now;
	uint_t mask;

	if (tsptr != NULL) {
		if (get_udatamodel() == DATAMODEL_NATIVE) {
			if (copyin(tsptr, ts, sizeof (ts)))
				return (EFAULT);
		} else {
			timespec32_t ts32[2];

			if (copyin(tsptr, ts32, sizeof (ts32)))
				return (EFAULT);
			TIMESPEC32_TO_TIMESPEC(&ts[0], &ts32[0]);
			TIMESPEC32_TO_TIMESPEC(&ts[1], &ts32[1]);
		}
		if (ts[0].tv_nsec == UTIME_NOW || ts[1].tv_nsec == UTIME_NOW)
			gethrestime(&now);
		mask = 0;
		if (ts[0].tv_nsec == UTIME_OMIT) {
			ts[0].tv_nsec = 0;
		} else {
			mask |= AT_ATIME;
			if (ts[0].tv_nsec == UTIME_NOW)
				ts[0] = now;
			else if (ts[0].tv_nsec < 0 || ts[0].tv_nsec >= NANOSEC)
				return (EINVAL);
		}
		if (ts[1].tv_nsec == UTIME_OMIT) {
			ts[1].tv_nsec = 0;
		} else {
			mask |= AT_MTIME;
			if (ts[1].tv_nsec == UTIME_NOW)
				ts[1] = now;
			else if (ts[1].tv_nsec < 0 || ts[1].tv_nsec >= NANOSEC)
				return (EINVAL);
		}
		vattr->va_atime = ts[0];
		vattr->va_mtime = ts[1];
		vattr->va_mask = mask;
		*flags = ATTR_UTIME;
	} else {
		gethrestime(&now);
		vattr->va_atime = now;
		vattr->va_mtime = now;
		vattr->va_mask = AT_ATIME | AT_MTIME;
		*flags = 0;
	}

	return (0);
}

/*
 * The futimesat() system call is no longer invoked from libc.
 * The futimesat() function has been implemented in libc using calls
 * to futimens() and utimensat().  The kernel code for futimesat()
 * should be expunged as soon as there is no longer a need
 * to run Solaris 10 and prior versions of libc on the system.
 * This includes the calls to futimesat in common/syscall/fsat.c
 */
int
futimesat(int fd, char *fname, struct timeval *tvptr)
{
	struct vattr vattr;
	int flags;
	int error;

	if ((error = get_timeval_vattr(tvptr, &vattr, &flags)) != 0)
		return (set_errno(error));

	return (cfutimesat(fd, fname, 2, &vattr, flags, FOLLOW));
}

/*
 * The utime() system call is no longer invoked from libc.
 * The utime() function has been implemented in libc using
 * a call to utimensat().  The kernel code for utime()
 * should be expunged as soon as there is no longer a need
 * to run Solaris 10 and prior versions of libc on the system.
 */
int
utime(char *fname, time_t *tptr)
{
	time_t tv[2];
	struct vattr vattr;
	int flags;

	if (tptr != NULL) {
		if (get_udatamodel() == DATAMODEL_NATIVE) {
			if (copyin(tptr, tv, sizeof (tv)))
				return (set_errno(EFAULT));
		} else {
			time32_t tv32[2];

			if (copyin(tptr, &tv32, sizeof (tv32)))
				return (set_errno(EFAULT));

			tv[0] = (time_t)tv32[0];
			tv[1] = (time_t)tv32[1];
		}

		vattr.va_atime.tv_sec = tv[0];
		vattr.va_atime.tv_nsec = 0;
		vattr.va_mtime.tv_sec = tv[1];
		vattr.va_mtime.tv_nsec = 0;
		flags = ATTR_UTIME;
	} else {
		gethrestime(&vattr.va_atime);
		vattr.va_mtime = vattr.va_atime;
		flags = 0;
	}

	vattr.va_mask = AT_ATIME|AT_MTIME;
	return (cfutimesat(AT_FDCWD, fname, 1, &vattr, flags, FOLLOW));
}

/*
 * The utimes() system call is no longer invoked from libc.
 * The utimes() function has been implemented in libc using
 * a call to utimensat().  The kernel code for utimes()
 * should be expunged as soon as there is no longer a need
 * to run Solaris 10 and prior versions of libc on the system.
 */
int
utimes(char *fname, struct timeval *tvptr)
{
	struct vattr vattr;
	int flags;
	int error;

	if ((error = get_timeval_vattr(tvptr, &vattr, &flags)) != 0)
		return (set_errno(error));

	return (cfutimesat(AT_FDCWD, fname, 1, &vattr, flags, FOLLOW));
}

int
futimens(int fd, timespec_t *tsptr)
{
	struct vattr vattr;
	int flags;
	int error;

	if ((error = get_timespec_vattr(tsptr, &vattr, &flags)) != 0)
		return (set_errno(error));

	return (cfutimesat(fd, NULL, 2, &vattr, flags, FOLLOW));
}

int
utimensat(int fd, char *fname, timespec_t *tsptr, int flag)
{
	struct vattr vattr;
	int flags;
	int error;

	if ((error = get_timespec_vattr(tsptr, &vattr, &flags)) != 0)
		return (set_errno(error));

	return (cfutimesat(fd, fname, 1, &vattr, flags,
	    (flag & AT_SYMLINK_NOFOLLOW)? NO_FOLLOW : FOLLOW));
}

int
utimesys(int code,
    uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
{
	switch (code) {
	case 0:
		return (futimens((int)arg1, (timespec_t *)arg2));
	case 1:
		return (utimensat((int)arg1, (char *)arg2,
		    (timespec_t *)arg3, (int)arg4));
	default:
		return (set_errno(EINVAL));
	}
}