netbsd32_time.c revision 1.34.8.3 1 /* $NetBSD: netbsd32_time.c,v 1.34.8.3 2009/01/04 02:56:09 christos 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.34.8.3 2009/01/04 02:56:09 christos 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 #else /* !NTP */
181 int
182 netbsd32___ntp_gettime50(struct lwp *l, const struct netbsd32___ntp_gettime50_args *uap, register_t *retval)
183 {
184
185 return (ENOSYS);
186 }
187
188 #ifdef COMPAT_30
189 int
190 compat_30_netbsd32_ntp_gettime(struct lwp *l, const struct compat_30_netbsd32_ntp_gettime_args *uap, register_t *retval)
191 {
192
193 return (ENOSYS);
194 }
195 #endif
196
197 int
198 netbsd32_ntp_adjtime(struct lwp *l, const struct netbsd32_ntp_adjtime_args *uap, register_t *retval)
199 {
200
201 return (ENOSYS);
202 }
203 #endif /* NTP */
204
205 int
206 netbsd32___setitimer50(struct lwp *l, const struct netbsd32___setitimer50_args *uap, register_t *retval)
207 {
208 /* {
209 syscallarg(int) which;
210 syscallarg(const netbsd32_itimervalp_t) itv;
211 syscallarg(netbsd32_itimervalp_t) oitv;
212 } */
213 struct proc *p = l->l_proc;
214 struct netbsd32_itimerval s32it, *itv32;
215 int which = SCARG(uap, which);
216 struct netbsd32___getitimer50_args getargs;
217 struct itimerval aitv;
218 int error;
219
220 if ((u_int)which > ITIMER_PROF)
221 return (EINVAL);
222 itv32 = SCARG_P32(uap, itv);
223 if (itv32) {
224 if ((error = copyin(itv32, &s32it, sizeof(s32it))))
225 return (error);
226 netbsd32_to_itimerval(&s32it, &aitv);
227 }
228 if (SCARG_P32(uap, oitv) != 0) {
229 SCARG(&getargs, which) = which;
230 SCARG(&getargs, itv) = SCARG(uap, oitv);
231 if ((error = netbsd32___getitimer50(l, &getargs, retval)) != 0)
232 return (error);
233 }
234 if (itv32 == 0)
235 return 0;
236
237 return dosetitimer(p, which, &aitv);
238 }
239
240 int
241 netbsd32___getitimer50(struct lwp *l, const struct netbsd32___getitimer50_args *uap, register_t *retval)
242 {
243 /* {
244 syscallarg(int) which;
245 syscallarg(netbsd32_itimervalp_t) itv;
246 } */
247 struct proc *p = l->l_proc;
248 struct netbsd32_itimerval s32it;
249 struct itimerval aitv;
250 int error;
251
252 error = dogetitimer(p, SCARG(uap, which), &aitv);
253 if (error)
254 return error;
255
256 netbsd32_from_itimerval(&aitv, &s32it);
257 return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it));
258 }
259
260 int
261 netbsd32___gettimeofday50(struct lwp *l, const struct netbsd32___gettimeofday50_args *uap, register_t *retval)
262 {
263 /* {
264 syscallarg(netbsd32_timevalp_t) tp;
265 syscallarg(netbsd32_timezonep_t) tzp;
266 } */
267 struct timeval atv;
268 struct netbsd32_timeval tv32;
269 int error = 0;
270 struct netbsd32_timezone tzfake;
271
272 if (SCARG_P32(uap, tp)) {
273 microtime(&atv);
274 netbsd32_from_timeval(&atv, &tv32);
275 error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32));
276 if (error)
277 return (error);
278 }
279 if (SCARG_P32(uap, tzp)) {
280 /*
281 * NetBSD has no kernel notion of time zone, so we just
282 * fake up a timezone struct and return it if demanded.
283 */
284 tzfake.tz_minuteswest = 0;
285 tzfake.tz_dsttime = 0;
286 error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake));
287 }
288 return (error);
289 }
290
291 int
292 netbsd32___settimeofday50(struct lwp *l, const struct netbsd32___settimeofday50_args *uap, register_t *retval)
293 {
294 /* {
295 syscallarg(const netbsd32_timevalp_t) tv;
296 syscallarg(const netbsd32_timezonep_t) tzp;
297 } */
298 struct netbsd32_timeval atv32;
299 struct timeval atv;
300 struct timespec ats;
301 int error;
302 struct proc *p = l->l_proc;
303
304 /* Verify all parameters before changing time. */
305
306 /*
307 * NetBSD has no kernel notion of time zone, and only an
308 * obsolete program would try to set it, so we log a warning.
309 */
310 if (SCARG_P32(uap, tzp))
311 printf("pid %d attempted to set the "
312 "(obsolete) kernel time zone\n", p->p_pid);
313
314 if (SCARG_P32(uap, tv) == 0)
315 return 0;
316
317 if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0)
318 return error;
319
320 netbsd32_to_timeval(&atv32, &atv);
321 TIMEVAL_TO_TIMESPEC(&atv, &ats);
322 return settime(p, &ats);
323 }
324
325 int
326 netbsd32___adjtime50(struct lwp *l, const struct netbsd32___adjtime50_args *uap, register_t *retval)
327 {
328 /* {
329 syscallarg(const netbsd32_timevalp_t) delta;
330 syscallarg(netbsd32_timevalp_t) olddelta;
331 } */
332 struct netbsd32_timeval atv;
333 int error;
334
335 extern int time_adjusted; /* in kern_ntptime.c */
336 extern int64_t time_adjtime; /* in kern_ntptime.c */
337
338 if ((error = kauth_authorize_system(l->l_cred,
339 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL,
340 NULL)) != 0)
341 return (error);
342
343 if (SCARG_P32(uap, olddelta)) {
344 atv.tv_sec = time_adjtime / 1000000;
345 atv.tv_usec = time_adjtime % 1000000;
346 if (atv.tv_usec < 0) {
347 atv.tv_usec += 1000000;
348 atv.tv_sec--;
349 }
350 (void) copyout(&atv,
351 SCARG_P32(uap, olddelta),
352 sizeof(atv));
353 if (error)
354 return (error);
355 }
356
357 if (SCARG_P32(uap, delta)) {
358 error = copyin(SCARG_P32(uap, delta), &atv,
359 sizeof(struct timeval));
360 if (error)
361 return (error);
362
363 time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec;
364
365 if (time_adjtime)
366 /* We need to save the system time during shutdown */
367 time_adjusted |= 1;
368 }
369
370 return (0);
371 }
372
373 int
374 netbsd32___clock_gettime50(struct lwp *l, const struct netbsd32___clock_gettime50_args *uap, register_t *retval)
375 {
376 /* {
377 syscallarg(netbsd32_clockid_t) clock_id;
378 syscallarg(netbsd32_timespecp_t) tp;
379 } */
380 clockid_t clock_id;
381 struct timespec ats;
382 struct netbsd32_timespec ts32;
383
384 clock_id = SCARG(uap, clock_id);
385 if (clock_id != CLOCK_REALTIME)
386 return (EINVAL);
387
388 nanotime(&ats);
389 netbsd32_from_timespec(&ats, &ts32);
390
391 return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
392 }
393
394 int
395 netbsd32___clock_settime50(struct lwp *l, const struct netbsd32___clock_settime50_args *uap, register_t *retval)
396 {
397 /* {
398 syscallarg(netbsd32_clockid_t) clock_id;
399 syscallarg(const netbsd32_timespecp_t) tp;
400 } */
401 struct netbsd32_timespec ts32;
402 clockid_t clock_id;
403 struct timespec ats;
404 int error;
405
406 clock_id = SCARG(uap, clock_id);
407 if (clock_id != CLOCK_REALTIME)
408 return (EINVAL);
409
410 if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
411 return (error);
412
413 netbsd32_to_timespec(&ts32, &ats);
414 return settime(l->l_proc, &ats);
415 }
416
417 int
418 netbsd32___clock_getres50(struct lwp *l, const struct netbsd32___clock_getres50_args *uap, register_t *retval)
419 {
420 /* {
421 syscallarg(netbsd32_clockid_t) clock_id;
422 syscallarg(netbsd32_timespecp_t) tp;
423 } */
424 struct netbsd32_timespec ts32;
425 clockid_t clock_id;
426 struct timespec ts;
427 int error = 0;
428
429 clock_id = SCARG(uap, clock_id);
430 if (clock_id != CLOCK_REALTIME)
431 return (EINVAL);
432
433 if (SCARG_P32(uap, tp)) {
434 ts.tv_sec = 0;
435 ts.tv_nsec = 1000000000 / hz;
436
437 netbsd32_from_timespec(&ts, &ts32);
438 error = copyout(&ts, SCARG_P32(uap, tp), sizeof(ts));
439 }
440
441 return error;
442 }
443
444 int
445 netbsd32___nanosleep50(struct lwp *l, const struct netbsd32___nanosleep50_args *uap, register_t *retval)
446 {
447 /* {
448 syscallarg(const netbsd32_timespecp_t) rqtp;
449 syscallarg(netbsd32_timespecp_t) rmtp;
450 } */
451 static int nanowait;
452 struct netbsd32_timespec ts32;
453 struct timespec rqt, ctime, rmt;
454 int error, timo;
455
456 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
457 if (error)
458 return (error);
459
460 netbsd32_to_timespec(&ts32, &rqt);
461 if (itimespecfix(&rqt))
462 return (EINVAL);
463
464 getnanotime(&ctime);
465 timespecadd(&rqt, &ctime, &rqt);
466 timo = tshzto(&rqt);
467 /*
468 * Avoid inadvertantly sleeping forever
469 */
470 if (timo == 0)
471 timo = 1;
472
473 error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
474 if (error == ERESTART)
475 error = EINTR;
476 if (error == EWOULDBLOCK)
477 error = 0;
478
479 if (SCARG_P32(uap, rmtp)) {
480 int error1;
481
482 getnanotime(&rmt);
483
484 timespecsub(&rqt, &rmt, &rmt);
485 if (rmt.tv_sec < 0)
486 timespecclear(&rmt);
487
488 netbsd32_from_timespec(&rmt, &ts32);
489 error1 = copyout(&ts32, SCARG_P32(uap,rmtp), sizeof(ts32));
490 if (error1)
491 return (error1);
492 }
493
494 return error;
495 }
496
497 static int
498 netbsd32_timer_create_fetch(const void *src, void *dst, size_t size)
499 {
500 struct sigevent *evp = dst;
501 struct netbsd32_sigevent ev32;
502 int error;
503
504 error = copyin(src, &ev32, sizeof(ev32));
505 if (error)
506 return error;
507
508 netbsd32_to_sigevent(&ev32, evp);
509 return 0;
510 }
511
512 int
513 netbsd32_timer_create(struct lwp *l, const struct netbsd32_timer_create_args *uap, register_t *retval)
514 {
515 /* {
516 syscallarg(netbsd32_clockid_t) clock_id;
517 syscallarg(netbsd32_sigeventp_t) evp;
518 syscallarg(netbsd32_timerp_t) timerid;
519 } */
520
521 return timer_create1(SCARG_P32(uap, timerid),
522 SCARG(uap, clock_id), SCARG_P32(uap, evp),
523 netbsd32_timer_create_fetch, l);
524 }
525
526 int
527 netbsd32_timer_delete(struct lwp *l, const struct netbsd32_timer_delete_args *uap, register_t *retval)
528 {
529 /* {
530 syscallarg(netbsd32_timer_t) timerid;
531 } */
532 struct sys_timer_delete_args ua;
533
534 NETBSD32TO64_UAP(timerid);
535 return sys_timer_delete(l, (void *)&ua, retval);
536 }
537
538 int
539 netbsd32___timer_settime50(struct lwp *l, const struct netbsd32___timer_settime50_args *uap, register_t *retval)
540 {
541 /* {
542 syscallarg(netbsd32_timer_t) timerid;
543 syscallarg(int) flags;
544 syscallarg(const netbsd32_itimerspecp_t) value;
545 syscallarg(netbsd32_itimerspecp_t) ovalue;
546 } */
547 int error;
548 struct itimerspec value, ovalue, *ovp = NULL;
549 struct netbsd32_itimerspec its32;
550
551 if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
552 return (error);
553 netbsd32_to_timespec(&its32.it_interval, &value.it_interval);
554 netbsd32_to_timespec(&its32.it_value, &value.it_value);
555
556 if (SCARG_P32(uap, ovalue))
557 ovp = &ovalue;
558
559 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
560 SCARG(uap, flags), l->l_proc)) != 0)
561 return error;
562
563 if (ovp) {
564 netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval);
565 netbsd32_from_timespec(&ovp->it_value, &its32.it_value);
566 return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
567 }
568 return 0;
569 }
570
571 int
572 netbsd32___timer_gettime50(struct lwp *l, const struct netbsd32___timer_gettime50_args *uap, register_t *retval)
573 {
574 /* {
575 syscallarg(netbsd32_timer_t) timerid;
576 syscallarg(netbsd32_itimerspecp_t) value;
577 } */
578 int error;
579 struct itimerspec its;
580 struct netbsd32_itimerspec its32;
581
582 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
583 &its)) != 0)
584 return error;
585
586 netbsd32_from_timespec(&its.it_interval, &its32.it_interval);
587 netbsd32_from_timespec(&its.it_value, &its32.it_value);
588
589 return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
590 }
591
592 int
593 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval)
594 {
595 /* {
596 syscallarg(netbsd32_timer_t) timerid;
597 } */
598 struct sys_timer_getoverrun_args ua;
599
600 NETBSD32TO64_UAP(timerid);
601 return sys_timer_getoverrun(l, (void *)&ua, retval);
602 }
603