xref: /netbsd-src/sys/compat/linux32/common/linux32_time.c (revision 0df165c04d0a9ca1adde9ed2b890344c937954a6)
1 /*	$NetBSD: linux32_time.c,v 1.14 2007/11/25 00:35:27 elad 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.14 2007/11/25 00:35:27 elad 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/kauth.h>
44 #include <sys/kernel.h>
45 #include <sys/fcntl.h>
46 #include <sys/namei.h>
47 #include <sys/select.h>
48 #include <sys/proc.h>
49 #include <sys/resourcevar.h>
50 #include <sys/ucred.h>
51 #include <sys/swap.h>
52 #include <sys/vfs_syscalls.h>
53 
54 #include <machine/types.h>
55 
56 #include <sys/syscallargs.h>
57 
58 #include <compat/netbsd32/netbsd32.h>
59 #include <compat/netbsd32/netbsd32_conv.h>
60 #include <compat/netbsd32/netbsd32_syscallargs.h>
61 
62 #include <compat/linux/common/linux_types.h>
63 #include <compat/linux/common/linux_signal.h>
64 #include <compat/linux/common/linux_machdep.h>
65 #include <compat/linux/common/linux_misc.h>
66 #include <compat/linux/common/linux_oldolduname.h>
67 #include <compat/linux/linux_syscallargs.h>
68 
69 #include <compat/linux32/common/linux32_types.h>
70 #include <compat/linux32/common/linux32_signal.h>
71 #include <compat/linux32/common/linux32_machdep.h>
72 #include <compat/linux32/common/linux32_sysctl.h>
73 #include <compat/linux32/common/linux32_socketcall.h>
74 #include <compat/linux32/linux32_syscallargs.h>
75 
76 extern struct timezone linux_sys_tz;
77 int
78 linux32_sys_gettimeofday(l, v, retval)
79 	struct lwp *l;
80 	void *v;
81 	register_t *retval;
82 {
83 	struct linux32_sys_gettimeofday_args /* {
84 		syscallarg(netbsd32_timevalp_t) tp;
85 		syscallarg(netbsd32_timezonep_t) tzp;
86 	} */ *uap = v;
87 	struct timeval tv;
88 	struct netbsd32_timeval tv32;
89 	int error;
90 
91 	if (SCARG_P32(uap, tp) != NULL) {
92 		microtime(&tv);
93 		netbsd32_from_timeval(&tv, &tv32);
94 		if ((error = copyout(&tv32, SCARG_P32(uap, tp),
95 		    sizeof(tv32))) != 0)
96 			return error;
97 	}
98 
99 	/* timezone size does not change */
100 	if (SCARG_P32(uap, tzp) != NULL) {
101 		if ((error = copyout(&linux_sys_tz, SCARG_P32(uap, tzp),
102 		    sizeof(linux_sys_tz))) != 0)
103 			return error;
104 	}
105 
106 	return 0;
107 }
108 
109 int
110 linux32_sys_settimeofday(l, v, retval)
111 	struct lwp *l;
112 	void *v;
113 	register_t *retval;
114 {
115 	struct linux32_sys_settimeofday_args /* {
116 		syscallarg(netbsd32_timevalp_t) tp;
117 		syscallarg(netbsd32_timezonep_t) tzp;
118 	} */ *uap = v;
119 	struct linux_sys_settimeofday_args ua;
120 
121 	NETBSD32TOP_UAP(tp, struct timeval);
122 	NETBSD32TOP_UAP(tzp, struct timezone);
123 
124 	return linux_sys_settimeofday(l, &ua, retval);
125 }
126 
127 int
128 linux32_sys_time(l, v, retval)
129 	struct lwp *l;
130 	void *v;
131 	register_t *retval;
132 {
133 	struct linux32_sys_time_args /* {
134 		syscallcarg(linux32_timep_t) t;
135 	} */ *uap = v;
136         struct timeval atv;
137         linux32_time_t tt;
138         int error;
139 
140         microtime(&atv);
141 
142         tt = (linux32_time_t)atv.tv_sec;
143 
144         if (SCARG_P32(uap, t) && (error = copyout(&tt,
145 	    SCARG_P32(uap, t), sizeof(tt))))
146                 return error;
147 
148         retval[0] = tt;
149 
150         return 0;
151 }
152 
153 
154 static inline linux32_clock_t
155 timeval_to_clock_t(struct timeval *tv)
156 {
157 	return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
158 }
159 
160 int
161 linux32_sys_times(l, v, retval)
162 	struct lwp *l;
163 	void *v;
164 	register_t *retval;
165 {
166 	struct linux32_sys_times_args /* {
167 		syscallarg(linux32_tmsp_t) tms;
168 	} */ *uap = v;
169 	struct linux32_tms ltms32;
170 
171 	struct timeval		 t;
172 	struct rusage		 *ru;
173 	struct proc		 *p = l->l_proc;
174 
175 	ru = &p->p_stats->p_ru;
176 	mutex_enter(&p->p_smutex);
177 	calcru(p, &ru->ru_utime, &ru->ru_stime, NULL, NULL);
178 	mutex_exit(&p->p_smutex);
179 
180 	ltms32.ltms32_utime = timeval_to_clock_t(&ru->ru_utime);
181 	ltms32.ltms32_stime = timeval_to_clock_t(&ru->ru_stime);
182 
183 	ru = &p->p_stats->p_cru;
184 	ltms32.ltms32_cutime = timeval_to_clock_t(&ru->ru_utime);
185 	ltms32.ltms32_cstime = timeval_to_clock_t(&ru->ru_stime);
186 
187 	microtime(&t);
188 	*retval = timeval_to_clock_t(&t);
189 
190 	return copyout(&ltms32, SCARG_P32(uap, tms), sizeof(ltms32));
191 }
192 
193 int
194 linux32_sys_stime(l, v, retval)
195 	struct lwp *l;
196 	void *v;
197 	register_t *retval;
198 {
199 	struct linux32_sys_stime_args /* {
200 		syscallarg(linux32_timep_t) t;
201 	} */ *uap = v;
202 	struct timespec ts;
203 	linux32_time_t tt32;
204 	int error;
205 
206 	if ((error = copyin(&tt32, SCARG_P32(uap, t), sizeof tt32)) != 0)
207 		return error;
208 
209 	ts.tv_sec = (long)tt32;
210 	ts.tv_nsec = 0;
211 
212 	return settime(l->l_proc, &ts);
213 }
214 
215 int
216 linux32_sys_utime(l, v, retval)
217 	struct lwp *l;
218 	void *v;
219 	register_t *retval;
220 {
221 	struct linux32_sys_utime_args /* {
222 		syscallarg(const netbsd32_charp) path;
223 		syscallarg(linux32_utimbufp_t) times;
224 	} */ *uap = v;
225         struct timeval tv[2], *tvp;
226         struct linux32_utimbuf lut;
227         int error;
228 
229         if (SCARG_P32(uap, times) != NULL) {
230                 if ((error = copyin(SCARG_P32(uap, times), &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                 tvp = tv;
238         } else {
239 		tvp = NULL;
240 	}
241 
242         return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
243 			    tvp, UIO_SYSSPACE);
244 }
245