xref: /illumos-gate/usr/src/uts/common/syscall/utime.c (revision d3e55dcdc881b833a707e39ae1e12d8d5d35ad2d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*d3e55dcdSgww  * Common Development and Distribution License (the "License").
6*d3e55dcdSgww  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d3e55dcdSgww  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
407c478bd9Sstevel@tonic-gate #include <sys/systm.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
437c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
447c478bd9Sstevel@tonic-gate #include <sys/time.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/model.h>
477c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
487c478bd9Sstevel@tonic-gate #include <sys/file.h>
497c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
507c478bd9Sstevel@tonic-gate #include <c2/audit.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate extern int	namesetattr(char *, enum symfollow, vattr_t *, int);
537c478bd9Sstevel@tonic-gate extern int	fdsetattr(int, vattr_t *);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int
567c478bd9Sstevel@tonic-gate cfutimesat(int fd, char *fname, int nmflag, vattr_t *vap, int flags)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	file_t *fp;
607c478bd9Sstevel@tonic-gate 	vnode_t *startvp, *vp;
617c478bd9Sstevel@tonic-gate 	int error;
627c478bd9Sstevel@tonic-gate 	char startchar;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (fd == AT_FDCWD && fname == NULL)
657c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	if (nmflag == 1 || (nmflag == 2 && fname != NULL)) {
687c478bd9Sstevel@tonic-gate 		if (copyin(fname, &startchar, sizeof (char)))
697c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
707c478bd9Sstevel@tonic-gate 	} else
717c478bd9Sstevel@tonic-gate 		startchar = '\0';
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	if (fd == AT_FDCWD)
747c478bd9Sstevel@tonic-gate 		startvp = NULL;
757c478bd9Sstevel@tonic-gate 	else {
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 		/*
787c478bd9Sstevel@tonic-gate 		 * is this absolute path?
797c478bd9Sstevel@tonic-gate 		 */
807c478bd9Sstevel@tonic-gate 		if (startchar != '/') {
817c478bd9Sstevel@tonic-gate 			if ((fp = getf(fd)) == NULL) {
827c478bd9Sstevel@tonic-gate 				return (set_errno(EBADF));
837c478bd9Sstevel@tonic-gate 			}
847c478bd9Sstevel@tonic-gate 			startvp = fp->f_vnode;
857c478bd9Sstevel@tonic-gate 			VN_HOLD(startvp);
867c478bd9Sstevel@tonic-gate 			releasef(fd);
877c478bd9Sstevel@tonic-gate 		} else {
887c478bd9Sstevel@tonic-gate 			startvp = NULL;
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (audit_active)
937c478bd9Sstevel@tonic-gate 		audit_setfsat_path(1);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if ((nmflag == 1) || ((nmflag == 2) && (fname != NULL))) {
967c478bd9Sstevel@tonic-gate 		if (error = lookupnameat(fname, UIO_USERSPACE, FOLLOW,
977c478bd9Sstevel@tonic-gate 		    NULLVPP, &vp, startvp)) {
987c478bd9Sstevel@tonic-gate 			if (startvp != NULL)
997c478bd9Sstevel@tonic-gate 				VN_RELE(startvp);
1007c478bd9Sstevel@tonic-gate 			return (set_errno(error));
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 	} else {
1037c478bd9Sstevel@tonic-gate 		vp = startvp;
1047c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (startvp != NULL) {
1087c478bd9Sstevel@tonic-gate 		VN_RELE(startvp);
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (vn_is_readonly(vp)) {
1127c478bd9Sstevel@tonic-gate 		error = EROFS;
1137c478bd9Sstevel@tonic-gate 	} else {
1147c478bd9Sstevel@tonic-gate 		error = VOP_SETATTR(vp, vap, flags, CRED(), NULL);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
1187c478bd9Sstevel@tonic-gate 	if (error != 0)
1197c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1207c478bd9Sstevel@tonic-gate 	else
1217c478bd9Sstevel@tonic-gate 		return (0);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static int
1257c478bd9Sstevel@tonic-gate get_utimesvattr(struct timeval *tvptr, struct vattr *vattr, int *flags)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	struct timeval tv[2];
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	*flags = 0;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (tvptr != NULL) {
1327c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
1337c478bd9Sstevel@tonic-gate 			if (copyin(tvptr, tv, sizeof (tv)))
1347c478bd9Sstevel@tonic-gate 				return (EFAULT);
1357c478bd9Sstevel@tonic-gate 		} else {
1367c478bd9Sstevel@tonic-gate 			struct timeval32 tv32[2];
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 			if (copyin(tvptr, tv32, sizeof (tv32)))
1397c478bd9Sstevel@tonic-gate 				return (EFAULT);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 			TIMEVAL32_TO_TIMEVAL(&tv[0], &tv32[0]);
1427c478bd9Sstevel@tonic-gate 			TIMEVAL32_TO_TIMEVAL(&tv[1], &tv32[1]);
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		if (tv[0].tv_usec < 0 || tv[0].tv_usec >= 1000000 ||
1467c478bd9Sstevel@tonic-gate 		    tv[1].tv_usec < 0 || tv[1].tv_usec >= 1000000)
1477c478bd9Sstevel@tonic-gate 			return (EINVAL);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		vattr->va_atime.tv_sec = tv[0].tv_sec;
1507c478bd9Sstevel@tonic-gate 		vattr->va_atime.tv_nsec = tv[0].tv_usec * 1000;
1517c478bd9Sstevel@tonic-gate 		vattr->va_mtime.tv_sec = tv[1].tv_sec;
1527c478bd9Sstevel@tonic-gate 		vattr->va_mtime.tv_nsec = tv[1].tv_usec * 1000;
1537c478bd9Sstevel@tonic-gate 		*flags |= ATTR_UTIME;
1547c478bd9Sstevel@tonic-gate 	} else {
1557c478bd9Sstevel@tonic-gate 		gethrestime(&vattr->va_atime);
1567c478bd9Sstevel@tonic-gate 		vattr->va_mtime = vattr->va_atime;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 	vattr->va_mask = AT_ATIME | AT_MTIME;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	return (0);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate int
1637c478bd9Sstevel@tonic-gate futimesat(int fd, char *fname, struct timeval *tvptr)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	struct vattr vattr;
1667c478bd9Sstevel@tonic-gate 	int flags = 0;
1677c478bd9Sstevel@tonic-gate 	int error;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if ((error = get_utimesvattr(tvptr, &vattr, &flags)) != 0)
1707c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	return (cfutimesat(fd, fname, 2, &vattr, flags));
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * Set access/modify times on named file.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate int
1787c478bd9Sstevel@tonic-gate utime(char *fname, time_t *tptr)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	time_t tv[2];
1817c478bd9Sstevel@tonic-gate 	struct vattr vattr;
1827c478bd9Sstevel@tonic-gate 	int flags = 0;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (tptr != NULL) {
1857c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
1867c478bd9Sstevel@tonic-gate 			if (copyin(tptr, tv, sizeof (tv)))
1877c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
1887c478bd9Sstevel@tonic-gate 		} else {
1897c478bd9Sstevel@tonic-gate 			time32_t tv32[2];
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 			if (copyin(tptr, &tv32, sizeof (tv32)))
1927c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 			tv[0] = (time_t)tv32[0];
1957c478bd9Sstevel@tonic-gate 			tv[1] = (time_t)tv32[1];
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		vattr.va_atime.tv_sec = tv[0];
1997c478bd9Sstevel@tonic-gate 		vattr.va_atime.tv_nsec = 0;
2007c478bd9Sstevel@tonic-gate 		vattr.va_mtime.tv_sec = tv[1];
2017c478bd9Sstevel@tonic-gate 		vattr.va_mtime.tv_nsec = 0;
2027c478bd9Sstevel@tonic-gate 		flags |= ATTR_UTIME;
2037c478bd9Sstevel@tonic-gate 	} else {
2047c478bd9Sstevel@tonic-gate 		gethrestime(&vattr.va_atime);
2057c478bd9Sstevel@tonic-gate 		vattr.va_mtime = vattr.va_atime;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	vattr.va_mask = AT_ATIME|AT_MTIME;
2097c478bd9Sstevel@tonic-gate 	return (cfutimesat(AT_FDCWD, fname, 1, &vattr, flags));
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * SunOS4.1 Buyback:
2147c478bd9Sstevel@tonic-gate  * Set access/modify time on named file, with hi res timer
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate int
2177c478bd9Sstevel@tonic-gate utimes(char *fname, struct timeval *tvptr)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	struct vattr vattr;
2207c478bd9Sstevel@tonic-gate 	int flags = 0;
2217c478bd9Sstevel@tonic-gate 	int error;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if ((error = get_utimesvattr(tvptr, &vattr, &flags)) != 0)
2247c478bd9Sstevel@tonic-gate 		return (set_errno(error));
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	return (cfutimesat(AT_FDCWD, fname, 1, &vattr, flags));
2277c478bd9Sstevel@tonic-gate }
228