xref: /netbsd-src/sys/compat/linux32/common/linux32_time.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: linux32_time.c,v 1.16 2007/12/20 23:02:59 dsl 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.16 2007/12/20 23:02:59 dsl 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(struct lwp *l, const struct linux32_sys_gettimeofday_args *uap, register_t *retval)
79 {
80 	/* {
81 		syscallarg(netbsd32_timevalp_t) tp;
82 		syscallarg(netbsd32_timezonep_t) tzp;
83 	} */
84 	struct timeval tv;
85 	struct netbsd32_timeval tv32;
86 	int error;
87 
88 	if (SCARG_P32(uap, tp) != NULL) {
89 		microtime(&tv);
90 		netbsd32_from_timeval(&tv, &tv32);
91 		if ((error = copyout(&tv32, SCARG_P32(uap, tp),
92 		    sizeof(tv32))) != 0)
93 			return error;
94 	}
95 
96 	/* timezone size does not change */
97 	if (SCARG_P32(uap, tzp) != NULL) {
98 		if ((error = copyout(&linux_sys_tz, SCARG_P32(uap, tzp),
99 		    sizeof(linux_sys_tz))) != 0)
100 			return error;
101 	}
102 
103 	return 0;
104 }
105 
106 int
107 linux32_sys_settimeofday(struct lwp *l, const struct linux32_sys_settimeofday_args *uap, register_t *retval)
108 {
109 	/* {
110 		syscallarg(netbsd32_timevalp_t) tp;
111 		syscallarg(netbsd32_timezonep_t) tzp;
112 	} */
113 	struct linux_sys_settimeofday_args ua;
114 
115 	NETBSD32TOP_UAP(tp, struct timeval);
116 	NETBSD32TOP_UAP(tzp, struct timezone);
117 
118 	return linux_sys_settimeofday(l, &ua, retval);
119 }
120 
121 int
122 linux32_sys_time(struct lwp *l, const struct linux32_sys_time_args *uap, register_t *retval)
123 {
124 	/* {
125 		syscallcarg(linux32_timep_t) t;
126 	} */
127         struct timeval atv;
128         linux32_time_t tt;
129         int error;
130 
131         microtime(&atv);
132 
133         tt = (linux32_time_t)atv.tv_sec;
134 
135         if (SCARG_P32(uap, t) && (error = copyout(&tt,
136 	    SCARG_P32(uap, t), sizeof(tt))))
137                 return error;
138 
139         retval[0] = tt;
140 
141         return 0;
142 }
143 
144 
145 static inline linux32_clock_t
146 timeval_to_clock_t(struct timeval *tv)
147 {
148 	return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
149 }
150 
151 int
152 linux32_sys_times(struct lwp *l, const struct linux32_sys_times_args *uap, register_t *retval)
153 {
154 	/* {
155 		syscallarg(linux32_tmsp_t) tms;
156 	} */
157 	struct linux32_tms ltms32;
158 
159 	struct timeval		 t;
160 	struct rusage		 *ru;
161 	struct proc		 *p = l->l_proc;
162 
163 	ru = &p->p_stats->p_ru;
164 	mutex_enter(&p->p_smutex);
165 	calcru(p, &ru->ru_utime, &ru->ru_stime, NULL, NULL);
166 	mutex_exit(&p->p_smutex);
167 
168 	ltms32.ltms32_utime = timeval_to_clock_t(&ru->ru_utime);
169 	ltms32.ltms32_stime = timeval_to_clock_t(&ru->ru_stime);
170 
171 	ru = &p->p_stats->p_cru;
172 	ltms32.ltms32_cutime = timeval_to_clock_t(&ru->ru_utime);
173 	ltms32.ltms32_cstime = timeval_to_clock_t(&ru->ru_stime);
174 
175 	microtime(&t);
176 	*retval = timeval_to_clock_t(&t);
177 
178 	return copyout(&ltms32, SCARG_P32(uap, tms), sizeof(ltms32));
179 }
180 
181 int
182 linux32_sys_stime(struct lwp *l, const struct linux32_sys_stime_args *uap, register_t *retval)
183 {
184 	/* {
185 		syscallarg(linux32_timep_t) t;
186 	} */
187 	struct timespec ts;
188 	linux32_time_t tt32;
189 	int error;
190 
191 	if ((error = copyin(&tt32, SCARG_P32(uap, t), sizeof tt32)) != 0)
192 		return error;
193 
194 	ts.tv_sec = (long)tt32;
195 	ts.tv_nsec = 0;
196 
197 	return settime(l->l_proc, &ts);
198 }
199 
200 int
201 linux32_sys_utime(struct lwp *l, const struct linux32_sys_utime_args *uap, register_t *retval)
202 {
203 	/* {
204 		syscallarg(const netbsd32_charp) path;
205 		syscallarg(linux32_utimbufp_t) times;
206 	} */
207         struct timeval tv[2], *tvp;
208         struct linux32_utimbuf lut;
209         int error;
210 
211         if (SCARG_P32(uap, times) != NULL) {
212                 if ((error = copyin(SCARG_P32(uap, times), &lut, sizeof lut)))
213                         return error;
214 
215                 tv[0].tv_sec = (long)lut.l_actime;
216                 tv[0].tv_usec = 0;
217                 tv[1].tv_sec = (long)lut.l_modtime;
218 		tv[1].tv_usec = 0;
219                 tvp = tv;
220         } else {
221 		tvp = NULL;
222 	}
223 
224         return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
225 			    tvp, UIO_SYSSPACE);
226 }
227