xref: /titanic_50/usr/src/uts/common/syscall/utime.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/param.h>
38 #include <sys/isa_defs.h>
39 #include <sys/types.h>
40 #include <sys/sysmacros.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/vnode.h>
44 #include <sys/vfs.h>
45 #include <sys/time.h>
46 #include <sys/debug.h>
47 #include <sys/model.h>
48 #include <sys/fcntl.h>
49 #include <sys/file.h>
50 #include <sys/pathname.h>
51 #include <c2/audit.h>
52 
53 extern int	namesetattr(char *, enum symfollow, vattr_t *, int);
54 extern int	fdsetattr(int, vattr_t *);
55 
56 static int
57 cfutimesat(int fd, char *fname, int nmflag, vattr_t *vap, int flags)
58 {
59 
60 	file_t *fp;
61 	vnode_t *startvp, *vp;
62 	int error;
63 	char startchar;
64 
65 	if (fd == AT_FDCWD && fname == NULL)
66 		return (set_errno(EFAULT));
67 
68 	if (nmflag == 1 || (nmflag == 2 && fname != NULL)) {
69 		if (copyin(fname, &startchar, sizeof (char)))
70 			return (set_errno(EFAULT));
71 	} else
72 		startchar = '\0';
73 
74 	if (fd == AT_FDCWD)
75 		startvp = NULL;
76 	else {
77 
78 		/*
79 		 * is this absolute path?
80 		 */
81 		if (startchar != '/') {
82 			if ((fp = getf(fd)) == NULL) {
83 				return (set_errno(EBADF));
84 			}
85 			startvp = fp->f_vnode;
86 			VN_HOLD(startvp);
87 			releasef(fd);
88 		} else {
89 			startvp = NULL;
90 		}
91 	}
92 
93 #ifdef C2_AUDIT
94 	if (audit_active)
95 		audit_setfsat_path(1);
96 #endif /* C2_AUDIT */
97 
98 	if ((nmflag == 1) || ((nmflag == 2) && (fname != NULL))) {
99 		if (error = lookupnameat(fname, UIO_USERSPACE, FOLLOW,
100 		    NULLVPP, &vp, startvp)) {
101 			if (startvp != NULL)
102 				VN_RELE(startvp);
103 			return (set_errno(error));
104 		}
105 	} else {
106 		vp = startvp;
107 		VN_HOLD(vp);
108 	}
109 
110 	if (startvp != NULL) {
111 		VN_RELE(startvp);
112 	}
113 
114 	if (vn_is_readonly(vp)) {
115 		error = EROFS;
116 	} else {
117 		error = VOP_SETATTR(vp, vap, flags, CRED(), NULL);
118 	}
119 
120 	VN_RELE(vp);
121 	if (error != 0)
122 		return (set_errno(error));
123 	else
124 		return (0);
125 }
126 
127 static int
128 get_utimesvattr(struct timeval *tvptr, struct vattr *vattr, int *flags)
129 {
130 	struct timeval tv[2];
131 
132 	*flags = 0;
133 
134 	if (tvptr != NULL) {
135 		if (get_udatamodel() == DATAMODEL_NATIVE) {
136 			if (copyin(tvptr, tv, sizeof (tv)))
137 				return (EFAULT);
138 		} else {
139 			struct timeval32 tv32[2];
140 
141 			if (copyin(tvptr, tv32, sizeof (tv32)))
142 				return (EFAULT);
143 
144 			TIMEVAL32_TO_TIMEVAL(&tv[0], &tv32[0]);
145 			TIMEVAL32_TO_TIMEVAL(&tv[1], &tv32[1]);
146 		}
147 
148 		if (tv[0].tv_usec < 0 || tv[0].tv_usec >= 1000000 ||
149 		    tv[1].tv_usec < 0 || tv[1].tv_usec >= 1000000)
150 			return (EINVAL);
151 
152 		vattr->va_atime.tv_sec = tv[0].tv_sec;
153 		vattr->va_atime.tv_nsec = tv[0].tv_usec * 1000;
154 		vattr->va_mtime.tv_sec = tv[1].tv_sec;
155 		vattr->va_mtime.tv_nsec = tv[1].tv_usec * 1000;
156 		*flags |= ATTR_UTIME;
157 	} else {
158 		gethrestime(&vattr->va_atime);
159 		vattr->va_mtime = vattr->va_atime;
160 	}
161 	vattr->va_mask = AT_ATIME | AT_MTIME;
162 
163 	return (0);
164 }
165 int
166 futimesat(int fd, char *fname, struct timeval *tvptr)
167 {
168 	struct vattr vattr;
169 	int flags = 0;
170 	int error;
171 
172 	if ((error = get_utimesvattr(tvptr, &vattr, &flags)) != 0)
173 		return (set_errno(error));
174 
175 	return (cfutimesat(fd, fname, 2, &vattr, flags));
176 }
177 /*
178  * Set access/modify times on named file.
179  */
180 int
181 utime(char *fname, time_t *tptr)
182 {
183 	time_t tv[2];
184 	struct vattr vattr;
185 	int flags = 0;
186 
187 	if (tptr != NULL) {
188 		if (get_udatamodel() == DATAMODEL_NATIVE) {
189 			if (copyin(tptr, tv, sizeof (tv)))
190 				return (set_errno(EFAULT));
191 		} else {
192 			time32_t tv32[2];
193 
194 			if (copyin(tptr, &tv32, sizeof (tv32)))
195 				return (set_errno(EFAULT));
196 
197 			tv[0] = (time_t)tv32[0];
198 			tv[1] = (time_t)tv32[1];
199 		}
200 
201 		vattr.va_atime.tv_sec = tv[0];
202 		vattr.va_atime.tv_nsec = 0;
203 		vattr.va_mtime.tv_sec = tv[1];
204 		vattr.va_mtime.tv_nsec = 0;
205 		flags |= ATTR_UTIME;
206 	} else {
207 		gethrestime(&vattr.va_atime);
208 		vattr.va_mtime = vattr.va_atime;
209 	}
210 
211 	vattr.va_mask = AT_ATIME|AT_MTIME;
212 	return (cfutimesat(AT_FDCWD, fname, 1, &vattr, flags));
213 }
214 
215 /*
216  * SunOS4.1 Buyback:
217  * Set access/modify time on named file, with hi res timer
218  */
219 int
220 utimes(char *fname, struct timeval *tvptr)
221 {
222 	struct vattr vattr;
223 	int flags = 0;
224 	int error;
225 
226 	if ((error = get_utimesvattr(tvptr, &vattr, &flags)) != 0)
227 		return (set_errno(error));
228 
229 	return (cfutimesat(AT_FDCWD, fname, 1, &vattr, flags));
230 }
231