Sync usage with man page.
[netbsd-mini2440.git] / sys / compat / netbsd32 / netbsd32_time.c
blob13ec504f767cc87cec24be6ba604b45aa0096bbc
1 /* $NetBSD: netbsd32_time.c,v 1.35 2009/01/11 02:45:49 christos Exp $ */
3 /*
4 * Copyright (c) 1998, 2001 Matthew R. Green
5 * All rights reserved.
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.
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.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_time.c,v 1.35 2009/01/11 02:45:49 christos Exp $");
32 #if defined(_KERNEL_OPT)
33 #include "opt_ntp.h"
34 #include "opt_compat_netbsd.h"
35 #endif
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>
50 #include <compat/netbsd32/netbsd32.h>
51 #include <compat/netbsd32/netbsd32_syscallargs.h>
52 #include <compat/netbsd32/netbsd32_conv.h>
54 #ifdef NTP
56 int
57 netbsd32___ntp_gettime50(struct lwp *l,
58 const struct netbsd32___ntp_gettime50_args *uap, register_t *retval)
60 /* {
61 syscallarg(netbsd32_ntptimevalp_t) ntvp;
62 } */
63 struct netbsd32_ntptimeval ntv32;
64 struct ntptimeval ntv;
65 int error = 0;
67 if (SCARG_P32(uap, ntvp)) {
68 ntp_gettime(&ntv);
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));
78 if (!error) {
79 *retval = ntp_timestatus();
82 return (error);
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)
90 /* {
91 syscallarg(netbsd32_ntptimeval50p_t) ntvp;
92 } */
93 struct netbsd32_ntptimeval50 ntv32;
94 struct ntptimeval ntv;
95 int error = 0;
97 if (SCARG_P32(uap, ntvp)) {
98 ntp_gettime(&ntv);
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));
108 if (!error) {
109 *retval = ntp_timestatus();
112 return (error);
114 #endif
116 #ifdef COMPAT_30
118 compat_30_netbsd32_ntp_gettime(struct lwp *l, const struct compat_30_netbsd32_ntp_gettime_args *uap, register_t *retval)
120 /* {
121 syscallarg(netbsd32_ntptimevalp_t) ntvp;
122 } */
123 struct netbsd32_ntptimeval30 ntv32;
124 struct ntptimeval ntv;
125 int error = 0;
127 if (SCARG_P32(uap, ntvp)) {
128 ntp_gettime(&ntv);
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));
136 if (!error) {
137 *retval = ntp_timestatus();
140 return (error);
142 #endif
145 netbsd32_ntp_adjtime(struct lwp *l, const struct netbsd32_ntp_adjtime_args *uap, register_t *retval)
147 /* {
148 syscallarg(netbsd32_timexp_t) tp;
149 } */
150 struct netbsd32_timex ntv32;
151 struct timex ntv;
152 int error = 0;
153 int modes;
155 if ((error = copyin(SCARG_P32(uap, tp), &ntv32, sizeof(ntv32))))
156 return (error);
158 netbsd32_to_timex(&ntv32, &ntv);
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.
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);
171 ntp_adjtime1(&ntv);
173 netbsd32_from_timex(&ntv, &ntv32);
174 error = copyout(&ntv32, SCARG_P32(uap, tp), sizeof(ntv32));
175 if (!error) {
176 *retval = ntp_timestatus();
178 return error;
180 #endif /* NTP */
183 netbsd32___setitimer50(struct lwp *l, const struct netbsd32___setitimer50_args *uap, register_t *retval)
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;
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);
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);
211 if (itv32 == 0)
212 return 0;
214 return dosetitimer(p, which, &aitv);
218 netbsd32___getitimer50(struct lwp *l, const struct netbsd32___getitimer50_args *uap, register_t *retval)
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;
229 error = dogetitimer(p, SCARG(uap, which), &aitv);
230 if (error)
231 return error;
233 netbsd32_from_itimerval(&aitv, &s32it);
234 return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it));
238 netbsd32___gettimeofday50(struct lwp *l, const struct netbsd32___gettimeofday50_args *uap, register_t *retval)
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;
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);
256 if (SCARG_P32(uap, tzp)) {
258 * NetBSD has no kernel notion of time zone, so we just
259 * fake up a timezone struct and return it if demanded.
261 tzfake.tz_minuteswest = 0;
262 tzfake.tz_dsttime = 0;
263 error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake));
265 return (error);
269 netbsd32___settimeofday50(struct lwp *l, const struct netbsd32___settimeofday50_args *uap, register_t *retval)
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;
281 /* Verify all parameters before changing time. */
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.
287 if (SCARG_P32(uap, tzp))
288 printf("pid %d attempted to set the "
289 "(obsolete) kernel time zone\n", p->p_pid);
291 if (SCARG_P32(uap, tv) == 0)
292 return 0;
294 if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0)
295 return error;
297 netbsd32_to_timeval(&atv32, &atv);
298 TIMEVAL_TO_TIMESPEC(&atv, &ats);
299 return settime(p, &ats);
303 netbsd32___adjtime50(struct lwp *l, const struct netbsd32___adjtime50_args *uap, register_t *retval)
305 /* {
306 syscallarg(const netbsd32_timevalp_t) delta;
307 syscallarg(netbsd32_timevalp_t) olddelta;
308 } */
309 struct netbsd32_timeval atv;
310 int error;
312 extern int time_adjusted; /* in kern_ntptime.c */
313 extern int64_t time_adjtime; /* in kern_ntptime.c */
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);
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--;
327 (void) copyout(&atv,
328 SCARG_P32(uap, olddelta),
329 sizeof(atv));
330 if (error)
331 return (error);
334 if (SCARG_P32(uap, delta)) {
335 error = copyin(SCARG_P32(uap, delta), &atv,
336 sizeof(struct timeval));
337 if (error)
338 return (error);
340 time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec;
342 if (time_adjtime)
343 /* We need to save the system time during shutdown */
344 time_adjusted |= 1;
347 return (0);
351 netbsd32___clock_gettime50(struct lwp *l, const struct netbsd32___clock_gettime50_args *uap, register_t *retval)
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;
361 clock_id = SCARG(uap, clock_id);
362 if (clock_id != CLOCK_REALTIME)
363 return (EINVAL);
365 nanotime(&ats);
366 netbsd32_from_timespec(&ats, &ts32);
368 return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
372 netbsd32___clock_settime50(struct lwp *l, const struct netbsd32___clock_settime50_args *uap, register_t *retval)
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;
383 clock_id = SCARG(uap, clock_id);
384 if (clock_id != CLOCK_REALTIME)
385 return (EINVAL);
387 if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
388 return (error);
390 netbsd32_to_timespec(&ts32, &ats);
391 return settime(l->l_proc, &ats);
395 netbsd32___clock_getres50(struct lwp *l, const struct netbsd32___clock_getres50_args *uap, register_t *retval)
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;
406 clock_id = SCARG(uap, clock_id);
407 if (clock_id != CLOCK_REALTIME)
408 return (EINVAL);
410 if (SCARG_P32(uap, tp)) {
411 ts.tv_sec = 0;
412 ts.tv_nsec = 1000000000 / hz;
414 netbsd32_from_timespec(&ts, &ts32);
415 error = copyout(&ts, SCARG_P32(uap, tp), sizeof(ts));
418 return error;
422 netbsd32___nanosleep50(struct lwp *l, const struct netbsd32___nanosleep50_args *uap, register_t *retval)
424 /* {
425 syscallarg(const netbsd32_timespecp_t) rqtp;
426 syscallarg(netbsd32_timespecp_t) rmtp;
427 } */
428 static int nanowait;
429 struct netbsd32_timespec ts32;
430 struct timespec rqt, ctime, rmt;
431 int error, timo;
433 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
434 if (error)
435 return (error);
437 netbsd32_to_timespec(&ts32, &rqt);
438 if (itimespecfix(&rqt))
439 return (EINVAL);
441 getnanotime(&ctime);
442 timespecadd(&rqt, &ctime, &rqt);
443 timo = tshzto(&rqt);
445 * Avoid inadvertantly sleeping forever
447 if (timo == 0)
448 timo = 1;
450 error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
451 if (error == ERESTART)
452 error = EINTR;
453 if (error == EWOULDBLOCK)
454 error = 0;
456 if (SCARG_P32(uap, rmtp)) {
457 int error1;
459 getnanotime(&rmt);
461 timespecsub(&rqt, &rmt, &rmt);
462 if (rmt.tv_sec < 0)
463 timespecclear(&rmt);
465 netbsd32_from_timespec(&rmt, &ts32);
466 error1 = copyout(&ts32, SCARG_P32(uap,rmtp), sizeof(ts32));
467 if (error1)
468 return (error1);
471 return error;
474 static int
475 netbsd32_timer_create_fetch(const void *src, void *dst, size_t size)
477 struct sigevent *evp = dst;
478 struct netbsd32_sigevent ev32;
479 int error;
481 error = copyin(src, &ev32, sizeof(ev32));
482 if (error)
483 return error;
485 netbsd32_to_sigevent(&ev32, evp);
486 return 0;
490 netbsd32_timer_create(struct lwp *l, const struct netbsd32_timer_create_args *uap, register_t *retval)
492 /* {
493 syscallarg(netbsd32_clockid_t) clock_id;
494 syscallarg(netbsd32_sigeventp_t) evp;
495 syscallarg(netbsd32_timerp_t) timerid;
496 } */
498 return timer_create1(SCARG_P32(uap, timerid),
499 SCARG(uap, clock_id), SCARG_P32(uap, evp),
500 netbsd32_timer_create_fetch, l);
504 netbsd32_timer_delete(struct lwp *l, const struct netbsd32_timer_delete_args *uap, register_t *retval)
506 /* {
507 syscallarg(netbsd32_timer_t) timerid;
508 } */
509 struct sys_timer_delete_args ua;
511 NETBSD32TO64_UAP(timerid);
512 return sys_timer_delete(l, (void *)&ua, retval);
516 netbsd32___timer_settime50(struct lwp *l, const struct netbsd32___timer_settime50_args *uap, register_t *retval)
518 /* {
519 syscallarg(netbsd32_timer_t) timerid;
520 syscallarg(int) flags;
521 syscallarg(const netbsd32_itimerspecp_t) value;
522 syscallarg(netbsd32_itimerspecp_t) ovalue;
523 } */
524 int error;
525 struct itimerspec value, ovalue, *ovp = NULL;
526 struct netbsd32_itimerspec its32;
528 if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
529 return (error);
530 netbsd32_to_timespec(&its32.it_interval, &value.it_interval);
531 netbsd32_to_timespec(&its32.it_value, &value.it_value);
533 if (SCARG_P32(uap, ovalue))
534 ovp = &ovalue;
536 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
537 SCARG(uap, flags), l->l_proc)) != 0)
538 return error;
540 if (ovp) {
541 netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval);
542 netbsd32_from_timespec(&ovp->it_value, &its32.it_value);
543 return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
545 return 0;
549 netbsd32___timer_gettime50(struct lwp *l, const struct netbsd32___timer_gettime50_args *uap, register_t *retval)
551 /* {
552 syscallarg(netbsd32_timer_t) timerid;
553 syscallarg(netbsd32_itimerspecp_t) value;
554 } */
555 int error;
556 struct itimerspec its;
557 struct netbsd32_itimerspec its32;
559 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
560 &its)) != 0)
561 return error;
563 netbsd32_from_timespec(&its.it_interval, &its32.it_interval);
564 netbsd32_from_timespec(&its.it_value, &its32.it_value);
566 return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
570 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval)
572 /* {
573 syscallarg(netbsd32_timer_t) timerid;
574 } */
575 struct sys_timer_getoverrun_args ua;
577 NETBSD32TO64_UAP(timerid);
578 return sys_timer_getoverrun(l, (void *)&ua, retval);