xref: /netbsd-src/sys/compat/netbsd32/netbsd32_time.c (revision a7e090f70e491979434963c9a27df4020fe0a18b)
1 /*	$NetBSD: netbsd32_time.c,v 1.37 2010/03/28 22:03:51 njoly Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2001 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_time.c,v 1.37 2010/03/28 22:03:51 njoly Exp $");
31 
32 #if defined(_KERNEL_OPT)
33 #include "opt_ntp.h"
34 #include "opt_compat_netbsd.h"
35 #endif
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mount.h>
40 #include <sys/time.h>
41 #include <sys/timex.h>
42 #include <sys/timevar.h>
43 #include <sys/timetc.h>
44 #include <sys/proc.h>
45 #include <sys/pool.h>
46 #include <sys/resourcevar.h>
47 #include <sys/dirent.h>
48 #include <sys/kauth.h>
49 
50 #include <compat/netbsd32/netbsd32.h>
51 #include <compat/netbsd32/netbsd32_syscallargs.h>
52 #include <compat/netbsd32/netbsd32_conv.h>
53 
54 #ifdef NTP
55 
56 int
57 netbsd32___ntp_gettime50(struct lwp *l,
58     const struct netbsd32___ntp_gettime50_args *uap, register_t *retval)
59 {
60 	/* {
61 		syscallarg(netbsd32_ntptimevalp_t) ntvp;
62 	} */
63 	struct netbsd32_ntptimeval ntv32;
64 	struct ntptimeval ntv;
65 	int error = 0;
66 
67 	if (SCARG_P32(uap, ntvp)) {
68 		ntp_gettime(&ntv);
69 
70 		ntv32.time.tv_sec = ntv.time.tv_sec;
71 		ntv32.time.tv_nsec = ntv.time.tv_nsec;
72 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
73 		ntv32.esterror = (netbsd32_long)ntv.esterror;
74 		ntv32.tai = (netbsd32_long)ntv.tai;
75 		ntv32.time_state = ntv.time_state;
76 		error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
77 	}
78 	if (!error) {
79 		*retval = ntp_timestatus();
80 	}
81 
82 	return (error);
83 }
84 
85 #ifdef COMPAT_50
86 int
87 compat_50_netbsd32_ntp_gettime(struct lwp *l,
88     const struct compat_50_netbsd32_ntp_gettime_args *uap, register_t *retval)
89 {
90 	/* {
91 		syscallarg(netbsd32_ntptimeval50p_t) ntvp;
92 	} */
93 	struct netbsd32_ntptimeval50 ntv32;
94 	struct ntptimeval ntv;
95 	int error = 0;
96 
97 	if (SCARG_P32(uap, ntvp)) {
98 		ntp_gettime(&ntv);
99 
100 		ntv32.time.tv_sec = (int32_t)ntv.time.tv_sec;
101 		ntv32.time.tv_nsec = ntv.time.tv_nsec;
102 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
103 		ntv32.esterror = (netbsd32_long)ntv.esterror;
104 		ntv32.tai = (netbsd32_long)ntv.tai;
105 		ntv32.time_state = ntv.time_state;
106 		error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
107 	}
108 	if (!error) {
109 		*retval = ntp_timestatus();
110 	}
111 
112 	return (error);
113 }
114 #endif
115 
116 #ifdef COMPAT_30
117 int
118 compat_30_netbsd32_ntp_gettime(struct lwp *l, const struct compat_30_netbsd32_ntp_gettime_args *uap, register_t *retval)
119 {
120 	/* {
121 		syscallarg(netbsd32_ntptimevalp_t) ntvp;
122 	} */
123 	struct netbsd32_ntptimeval30 ntv32;
124 	struct ntptimeval ntv;
125 	int error = 0;
126 
127 	if (SCARG_P32(uap, ntvp)) {
128 		ntp_gettime(&ntv);
129 
130 		ntv32.time.tv_sec = ntv.time.tv_sec;
131 		ntv32.time.tv_usec = ntv.time.tv_nsec / 1000;
132 		ntv32.maxerror = (netbsd32_long)ntv.maxerror;
133 		ntv32.esterror = (netbsd32_long)ntv.esterror;
134 		error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32));
135 	}
136 	if (!error) {
137 		*retval = ntp_timestatus();
138 	}
139 
140 	return (error);
141 }
142 #endif
143 
144 int
145 netbsd32_ntp_adjtime(struct lwp *l, const struct netbsd32_ntp_adjtime_args *uap, register_t *retval)
146 {
147 	/* {
148 		syscallarg(netbsd32_timexp_t) tp;
149 	} */
150 	struct netbsd32_timex ntv32;
151 	struct timex ntv;
152 	int error = 0;
153 	int modes;
154 
155 	if ((error = copyin(SCARG_P32(uap, tp), &ntv32, sizeof(ntv32))))
156 		return (error);
157 
158 	netbsd32_to_timex(&ntv32, &ntv);
159 
160 	/*
161 	 * Update selected clock variables - only the superuser can
162 	 * change anything. Note that there is no error checking here on
163 	 * the assumption the superuser should know what it is doing.
164 	 */
165 	modes = ntv.modes;
166 	if (modes != 0 && (error = kauth_authorize_system(l->l_cred,
167 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_NTPADJTIME, NULL, NULL,
168 	    NULL)))
169 		return (error);
170 
171 	ntp_adjtime1(&ntv);
172 
173 	netbsd32_from_timex(&ntv, &ntv32);
174 	error = copyout(&ntv32, SCARG_P32(uap, tp), sizeof(ntv32));
175 	if (!error) {
176 		*retval = ntp_timestatus();
177 	}
178 	return error;
179 }
180 #endif /* NTP */
181 
182 int
183 netbsd32___setitimer50(struct lwp *l, const struct netbsd32___setitimer50_args *uap, register_t *retval)
184 {
185 	/* {
186 		syscallarg(int) which;
187 		syscallarg(const netbsd32_itimervalp_t) itv;
188 		syscallarg(netbsd32_itimervalp_t) oitv;
189 	} */
190 	struct proc *p = l->l_proc;
191 	struct netbsd32_itimerval s32it, *itv32;
192 	int which = SCARG(uap, which);
193 	struct netbsd32___getitimer50_args getargs;
194 	struct itimerval aitv;
195 	int error;
196 
197 	if ((u_int)which > ITIMER_PROF)
198 		return (EINVAL);
199 	itv32 = SCARG_P32(uap, itv);
200 	if (itv32) {
201 		if ((error = copyin(itv32, &s32it, sizeof(s32it))))
202 			return (error);
203 		netbsd32_to_itimerval(&s32it, &aitv);
204 	}
205 	if (SCARG_P32(uap, oitv) != 0) {
206 		SCARG(&getargs, which) = which;
207 		SCARG(&getargs, itv) = SCARG(uap, oitv);
208 		if ((error = netbsd32___getitimer50(l, &getargs, retval)) != 0)
209 			return (error);
210 	}
211 	if (itv32 == 0)
212 		return 0;
213 
214 	return dosetitimer(p, which, &aitv);
215 }
216 
217 int
218 netbsd32___getitimer50(struct lwp *l, const struct netbsd32___getitimer50_args *uap, register_t *retval)
219 {
220 	/* {
221 		syscallarg(int) which;
222 		syscallarg(netbsd32_itimervalp_t) itv;
223 	} */
224 	struct proc *p = l->l_proc;
225 	struct netbsd32_itimerval s32it;
226 	struct itimerval aitv;
227 	int error;
228 
229 	error = dogetitimer(p, SCARG(uap, which), &aitv);
230 	if (error)
231 		return error;
232 
233 	netbsd32_from_itimerval(&aitv, &s32it);
234 	return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it));
235 }
236 
237 int
238 netbsd32___gettimeofday50(struct lwp *l, const struct netbsd32___gettimeofday50_args *uap, register_t *retval)
239 {
240 	/* {
241 		syscallarg(netbsd32_timevalp_t) tp;
242 		syscallarg(netbsd32_timezonep_t) tzp;
243 	} */
244 	struct timeval atv;
245 	struct netbsd32_timeval tv32;
246 	int error = 0;
247 	struct netbsd32_timezone tzfake;
248 
249 	if (SCARG_P32(uap, tp)) {
250 		microtime(&atv);
251 		netbsd32_from_timeval(&atv, &tv32);
252 		error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32));
253 		if (error)
254 			return (error);
255 	}
256 	if (SCARG_P32(uap, tzp)) {
257 		/*
258 		 * NetBSD has no kernel notion of time zone, so we just
259 		 * fake up a timezone struct and return it if demanded.
260 		 */
261 		tzfake.tz_minuteswest = 0;
262 		tzfake.tz_dsttime = 0;
263 		error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake));
264 	}
265 	return (error);
266 }
267 
268 int
269 netbsd32___settimeofday50(struct lwp *l, const struct netbsd32___settimeofday50_args *uap, register_t *retval)
270 {
271 	/* {
272 		syscallarg(const netbsd32_timevalp_t) tv;
273 		syscallarg(const netbsd32_timezonep_t) tzp;
274 	} */
275 	struct netbsd32_timeval atv32;
276 	struct timeval atv;
277 	struct timespec ats;
278 	int error;
279 	struct proc *p = l->l_proc;
280 
281 	/* Verify all parameters before changing time. */
282 
283 	/*
284 	 * NetBSD has no kernel notion of time zone, and only an
285 	 * obsolete program would try to set it, so we log a warning.
286 	 */
287 	if (SCARG_P32(uap, tzp))
288 		printf("pid %d attempted to set the "
289 		    "(obsolete) kernel time zone\n", p->p_pid);
290 
291 	if (SCARG_P32(uap, tv) == 0)
292 		return 0;
293 
294 	if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0)
295 		return error;
296 
297 	netbsd32_to_timeval(&atv32, &atv);
298 	TIMEVAL_TO_TIMESPEC(&atv, &ats);
299 	return settime(p, &ats);
300 }
301 
302 int
303 netbsd32___adjtime50(struct lwp *l, const struct netbsd32___adjtime50_args *uap, register_t *retval)
304 {
305 	/* {
306 		syscallarg(const netbsd32_timevalp_t) delta;
307 		syscallarg(netbsd32_timevalp_t) olddelta;
308 	} */
309 	struct netbsd32_timeval atv;
310 	int error;
311 
312 	extern int time_adjusted;     /* in kern_ntptime.c */
313 	extern int64_t time_adjtime;  /* in kern_ntptime.c */
314 
315 	if ((error = kauth_authorize_system(l->l_cred,
316 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL,
317 	    NULL)) != 0)
318 		return (error);
319 
320 	if (SCARG_P32(uap, olddelta)) {
321 		atv.tv_sec = time_adjtime / 1000000;
322 		atv.tv_usec = time_adjtime % 1000000;
323 		if (atv.tv_usec < 0) {
324 			atv.tv_usec += 1000000;
325 			atv.tv_sec--;
326 		}
327 		(void) copyout(&atv,
328 			       SCARG_P32(uap, olddelta),
329 			       sizeof(atv));
330 		if (error)
331 			return (error);
332 	}
333 
334 	if (SCARG_P32(uap, delta)) {
335 		error = copyin(SCARG_P32(uap, delta), &atv,
336 			       sizeof(struct timeval));
337 		if (error)
338 			return (error);
339 
340 		time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec;
341 
342 		if (time_adjtime)
343 			/* We need to save the system time during shutdown */
344 			time_adjusted |= 1;
345 	}
346 
347 	return (0);
348 }
349 
350 int
351 netbsd32___clock_gettime50(struct lwp *l, const struct netbsd32___clock_gettime50_args *uap, register_t *retval)
352 {
353 	/* {
354 		syscallarg(netbsd32_clockid_t) clock_id;
355 		syscallarg(netbsd32_timespecp_t) tp;
356 	} */
357 	clockid_t clock_id;
358 	struct timespec ats;
359 	struct netbsd32_timespec ts32;
360 
361 	clock_id = SCARG(uap, clock_id);
362 	if (clock_id != CLOCK_REALTIME)
363 		return (EINVAL);
364 
365 	nanotime(&ats);
366 	netbsd32_from_timespec(&ats, &ts32);
367 
368 	return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
369 }
370 
371 int
372 netbsd32___clock_settime50(struct lwp *l, const struct netbsd32___clock_settime50_args *uap, register_t *retval)
373 {
374 	/* {
375 		syscallarg(netbsd32_clockid_t) clock_id;
376 		syscallarg(const netbsd32_timespecp_t) tp;
377 	} */
378 	struct netbsd32_timespec ts32;
379 	clockid_t clock_id;
380 	struct timespec ats;
381 	int error;
382 
383 	clock_id = SCARG(uap, clock_id);
384 	if (clock_id != CLOCK_REALTIME)
385 		return (EINVAL);
386 
387 	if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
388 		return (error);
389 
390 	netbsd32_to_timespec(&ts32, &ats);
391 	return settime(l->l_proc, &ats);
392 }
393 
394 int
395 netbsd32___clock_getres50(struct lwp *l, const struct netbsd32___clock_getres50_args *uap, register_t *retval)
396 {
397 	/* {
398 		syscallarg(netbsd32_clockid_t) clock_id;
399 		syscallarg(netbsd32_timespecp_t) tp;
400 	} */
401 	struct netbsd32_timespec ts32;
402 	clockid_t clock_id;
403 	struct timespec ts;
404 	int error = 0;
405 
406 	clock_id = SCARG(uap, clock_id);
407 	if (clock_id != CLOCK_REALTIME)
408 		return (EINVAL);
409 
410 	if (SCARG_P32(uap, tp)) {
411 		ts.tv_sec = 0;
412 		ts.tv_nsec = 1000000000 / hz;
413 
414 		netbsd32_from_timespec(&ts, &ts32);
415 		error = copyout(&ts, SCARG_P32(uap, tp), sizeof(ts));
416 	}
417 
418 	return error;
419 }
420 
421 int
422 netbsd32___nanosleep50(struct lwp *l, const struct netbsd32___nanosleep50_args *uap, register_t *retval)
423 {
424 	/* {
425 		syscallarg(const netbsd32_timespecp_t) rqtp;
426 		syscallarg(netbsd32_timespecp_t) rmtp;
427 	} */
428 	struct netbsd32_timespec ts32;
429 	struct timespec rqt, rmt;
430 	int error, error1;
431 
432 	error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
433 	if (error)
434 		return (error);
435 	netbsd32_to_timespec(&ts32, &rqt);
436 
437 	error = nanosleep1(l, &rqt, SCARG_P32(uap, rmtp) ? &rmt : NULL);
438 	if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
439 		return error;
440 
441 	netbsd32_from_timespec(&rmt, &ts32);
442 	error1 = copyout(&ts32, SCARG_P32(uap,rmtp), sizeof(ts32));
443 	return error1 ? error1 : error;
444 }
445 
446 static int
447 netbsd32_timer_create_fetch(const void *src, void *dst, size_t size)
448 {
449 	struct sigevent *evp = dst;
450 	struct netbsd32_sigevent ev32;
451 	int error;
452 
453 	error = copyin(src, &ev32, sizeof(ev32));
454 	if (error)
455 		return error;
456 
457 	netbsd32_to_sigevent(&ev32, evp);
458 	return 0;
459 }
460 
461 int
462 netbsd32_timer_create(struct lwp *l, const struct netbsd32_timer_create_args *uap, register_t *retval)
463 {
464 	/* {
465 		syscallarg(netbsd32_clockid_t) clock_id;
466 		syscallarg(netbsd32_sigeventp_t) evp;
467 		syscallarg(netbsd32_timerp_t) timerid;
468 	} */
469 
470 	return timer_create1(SCARG_P32(uap, timerid),
471 	    SCARG(uap, clock_id), SCARG_P32(uap, evp),
472 	    netbsd32_timer_create_fetch, l);
473 }
474 
475 int
476 netbsd32_timer_delete(struct lwp *l, const struct netbsd32_timer_delete_args *uap, register_t *retval)
477 {
478 	/* {
479 		syscallarg(netbsd32_timer_t) timerid;
480 	} */
481 	struct sys_timer_delete_args ua;
482 
483 	NETBSD32TO64_UAP(timerid);
484 	return sys_timer_delete(l, (void *)&ua, retval);
485 }
486 
487 int
488 netbsd32___timer_settime50(struct lwp *l, const struct netbsd32___timer_settime50_args *uap, register_t *retval)
489 {
490 	/* {
491 		syscallarg(netbsd32_timer_t) timerid;
492 		syscallarg(int) flags;
493 		syscallarg(const netbsd32_itimerspecp_t) value;
494 		syscallarg(netbsd32_itimerspecp_t) ovalue;
495 	} */
496 	int error;
497 	struct itimerspec value, ovalue, *ovp = NULL;
498 	struct netbsd32_itimerspec its32;
499 
500 	if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
501 		return (error);
502 	netbsd32_to_timespec(&its32.it_interval, &value.it_interval);
503 	netbsd32_to_timespec(&its32.it_value, &value.it_value);
504 
505 	if (SCARG_P32(uap, ovalue))
506 		ovp = &ovalue;
507 
508 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
509 	    SCARG(uap, flags), l->l_proc)) != 0)
510 		return error;
511 
512 	if (ovp) {
513 		netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval);
514 		netbsd32_from_timespec(&ovp->it_value, &its32.it_value);
515 		return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
516 	}
517 	return 0;
518 }
519 
520 int
521 netbsd32___timer_gettime50(struct lwp *l, const struct netbsd32___timer_gettime50_args *uap, register_t *retval)
522 {
523 	/* {
524 		syscallarg(netbsd32_timer_t) timerid;
525 		syscallarg(netbsd32_itimerspecp_t) value;
526 	} */
527 	int error;
528 	struct itimerspec its;
529 	struct netbsd32_itimerspec its32;
530 
531 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
532 	    &its)) != 0)
533 		return error;
534 
535 	netbsd32_from_timespec(&its.it_interval, &its32.it_interval);
536 	netbsd32_from_timespec(&its.it_value, &its32.it_value);
537 
538 	return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
539 }
540 
541 int
542 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval)
543 {
544 	/* {
545 		syscallarg(netbsd32_timer_t) timerid;
546 	} */
547 	struct sys_timer_getoverrun_args ua;
548 
549 	NETBSD32TO64_UAP(timerid);
550 	return sys_timer_getoverrun(l, (void *)&ua, retval);
551 }
552