xref: /netbsd-src/sys/compat/linux32/common/linux32_time.c (revision 86811edb37e43f44504b192591c863c5d48f5e08)
1 /*	$NetBSD: linux32_time.c,v 1.1 2006/02/09 19:18:57 manu Exp $ */
2 
3 /*-
4  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Emmanuel Dreyfus
17  * 4. The name of the author may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 __KERNEL_RCSID(0, "$NetBSD: linux32_time.c,v 1.1 2006/02/09 19:18:57 manu Exp $");
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/fstypes.h>
41 #include <sys/signal.h>
42 #include <sys/dirent.h>
43 #include <sys/kernel.h>
44 #include <sys/fcntl.h>
45 #include <sys/select.h>
46 #include <sys/sa.h>
47 #include <sys/proc.h>
48 #include <sys/ucred.h>
49 #include <sys/swap.h>
50 
51 #include <machine/types.h>
52 
53 #include <sys/syscallargs.h>
54 
55 #include <compat/netbsd32/netbsd32.h>
56 #include <compat/netbsd32/netbsd32_conv.h>
57 #include <compat/netbsd32/netbsd32_syscallargs.h>
58 
59 #include <compat/linux/common/linux_types.h>
60 #include <compat/linux/common/linux_signal.h>
61 #include <compat/linux/common/linux_machdep.h>
62 #include <compat/linux/common/linux_misc.h>
63 #include <compat/linux/common/linux_oldolduname.h>
64 #include <compat/linux/linux_syscallargs.h>
65 
66 #include <compat/linux32/common/linux32_types.h>
67 #include <compat/linux32/common/linux32_signal.h>
68 #include <compat/linux32/common/linux32_machdep.h>
69 #include <compat/linux32/common/linux32_sysctl.h>
70 #include <compat/linux32/common/linux32_socketcall.h>
71 #include <compat/linux32/linux32_syscallargs.h>
72 
73 extern struct timezone linux_sys_tz;
74 int
75 linux32_sys_gettimeofday(l, v, retval)
76 	struct lwp *l;
77 	void *v;
78 	register_t *retval;
79 {
80 	struct linux32_sys_gettimeofday_args /* {
81 		syscallarg(netbsd32_timevalp_t) tp;
82 		syscallarg(netbsd32_timezonep_t) tzp;
83 	} */ *uap = v;
84 	struct timeval tv;
85 	struct netbsd32_timeval tv32;
86 	int error;
87 
88 	if (NETBSD32PTR64(SCARG(uap, tp)) != NULL) {
89 		microtime(&tv);
90 		netbsd32_from_timeval(&tv, &tv32);
91 		if ((error = copyout(&tv32,
92 		    (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
93 		    sizeof(tv32))) != 0)
94 			return error;
95 	}
96 
97 	/* timezone size does not change */
98 	if (NETBSD32PTR64(SCARG(uap, tzp)) != NULL) {
99 		if ((error = copyout(&linux_sys_tz,
100 		    (caddr_t)NETBSD32PTR64(SCARG(uap, tzp)),
101 		    sizeof(linux_sys_tz))) != 0)
102 			return error;
103 	}
104 
105 	return 0;
106 }
107 
108 int
109 linux32_sys_settimeofday(l, v, retval)
110 	struct lwp *l;
111 	void *v;
112 	register_t *retval;
113 {
114 	struct linux32_sys_settimeofday_args /* {
115 		syscallarg(netbsd32_timevalp_t) tp;
116 		syscallarg(netbsd32_timezonep_t) tzp;
117 	} */ *uap = v;
118 	struct linux_sys_settimeofday_args ua;
119 
120 	NETBSD32TOP_UAP(tp, struct timeval);
121 	NETBSD32TOP_UAP(tzp, struct timezone);
122 
123 	return linux_sys_settimeofday(l, &ua, retval);
124 }
125 
126 int
127 linux32_sys_time(l, v, retval)
128 	struct lwp *l;
129 	void *v;
130 	register_t *retval;
131 {
132 	struct linux32_sys_time_args /* {
133 		syscallcarg(linux32_timep_t) t;
134 	} */ *uap = v;
135 	struct linux_sys_time_args ua;
136 
137 	NETBSD32TOP_UAP(t, linux_time_t);
138 
139 	return linux_sys_time(l, &ua, retval);
140 }
141 
142 
143 int
144 linux32_sys_times(l, v, retval)
145 	struct lwp *l;
146 	void *v;
147 	register_t *retval;
148 {
149 	struct linux32_sys_times_args /* {
150 		syscallarg(linux32_tmsp_t) tms;
151 	} */ *uap = v;
152 	struct linux32_tms ltms32;
153 	struct linux_tms ltms;
154 	struct linux_tms *ltmsp;
155 	struct linux_sys_times_args ua;
156 	caddr_t sg = stackgap_init(l->l_proc, 0);
157 	int error;
158 
159 	ltmsp = stackgap_alloc(l->l_proc, &sg, sizeof(*ltmsp));
160 	SCARG(&ua, tms) = (struct times *)ltmsp;
161 
162 	if ((error = linux_sys_times(l, &ua, retval)) != 0)
163 		return error;
164 
165 	if ((error = copyin(ltmsp, &ltms, sizeof(ltms))) != 0)
166 		return error;
167 
168 	ltms32.ltms32_utime = (linux32_clock_t)ltms.ltms_utime;
169 	ltms32.ltms32_stime = (linux32_clock_t)ltms.ltms_stime;
170 	ltms32.ltms32_cutime = (linux32_clock_t)ltms.ltms_cutime;
171 	ltms32.ltms32_cstime = (linux32_clock_t)ltms.ltms_cstime;
172 
173 	if ((error = copyout(&ltms32,
174 	    NETBSD32PTR64(SCARG(uap, tms)), sizeof(ltms32))) != 0)
175 		return error;
176 
177 	return 0;
178 }
179 
180 int
181 linux32_sys_stime(l, v, retval)
182 	struct lwp *l;
183 	void *v;
184 	register_t *retval;
185 {
186 	struct linux32_sys_stime_args /* {
187 		syscallarg(linux32_timep_t) t;
188 	} */ *uap = v;
189 	struct proc *p = l->l_proc;
190 	struct timespec ts;
191 	linux32_time_t tt32;
192 	int error;
193 
194 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
195 		return error;
196 
197 	if ((error = copyin(&tt32,
198 	    NETBSD32PTR64(SCARG(uap, t)),
199 	    sizeof tt32)) != 0)
200 
201 	ts.tv_sec = (long)tt32;
202 	ts.tv_nsec = 0;
203 
204 	return settime(p, &ts);
205 }
206 
207 int
208 linux32_sys_utime(l, v, retval)
209 	struct lwp *l;
210 	void *v;
211 	register_t *retval;
212 {
213 	struct linux32_sys_utime_args /* {
214 		syscallarg(const netbsd32_charp) path;
215 		syscallarg(linux32_utimbufp_t) times;
216 	} */ *uap = v;
217 	struct proc *p = l->l_proc;
218         caddr_t sg = stackgap_init(p, 0);
219         struct sys_utimes_args ua;
220         struct timeval tv[2], *tvp;
221         struct linux32_utimbuf lut;
222         int error;
223 
224 	NETBSD32TOP_UAP(path, const char);
225         CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
226 
227 
228         if (NETBSD32PTR64(SCARG(uap, times)) != NULL) {
229                 if ((error = copyin(NETBSD32PTR64(SCARG(uap, times)),
230 		    &lut, sizeof lut)))
231                         return error;
232 
233                 tv[0].tv_sec = (long)lut.l_actime;
234                 tv[0].tv_usec = 0;
235                 tv[1].tv_sec = (long)lut.l_modtime;
236 		tv[1].tv_usec = 0;
237 
238 	        tvp = (struct timeval *) stackgap_alloc(p, &sg, sizeof(tv));
239 
240                 if ((error = copyout(tv, tvp, sizeof(tv))))
241                         return error;
242                 SCARG(&ua, tptr) = tvp;
243         } else {
244                SCARG(&ua, tptr) = NULL;
245 	}
246 
247         return sys_utimes(l, &ua, retval);
248 }
249