netbsd32_time.c revision 1.39 1 /* $NetBSD: netbsd32_time.c,v 1.39 2010/03/29 15:34:07 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.39 2010/03/29 15:34:07 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 struct timespec ats;
380 int error;
381
382 if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
383 return (error);
384
385 netbsd32_to_timespec(&ts32, &ats);
386 return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
387 }
388
389 int
390 netbsd32___clock_getres50(struct lwp *l, const struct netbsd32___clock_getres50_args *uap, register_t *retval)
391 {
392 /* {
393 syscallarg(netbsd32_clockid_t) clock_id;
394 syscallarg(netbsd32_timespecp_t) tp;
395 } */
396 struct netbsd32_timespec ts32;
397 clockid_t clock_id;
398 struct timespec ts;
399 int error = 0;
400
401 clock_id = SCARG(uap, clock_id);
402 if (clock_id != CLOCK_REALTIME)
403 return (EINVAL);
404
405 if (SCARG_P32(uap, tp)) {
406 ts.tv_sec = 0;
407 ts.tv_nsec = 1000000000 / hz;
408
409 netbsd32_from_timespec(&ts, &ts32);
410 error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
411 }
412
413 return error;
414 }
415
416 int
417 netbsd32___nanosleep50(struct lwp *l, const struct netbsd32___nanosleep50_args *uap, register_t *retval)
418 {
419 /* {
420 syscallarg(const netbsd32_timespecp_t) rqtp;
421 syscallarg(netbsd32_timespecp_t) rmtp;
422 } */
423 struct netbsd32_timespec ts32;
424 struct timespec rqt, rmt;
425 int error, error1;
426
427 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
428 if (error)
429 return (error);
430 netbsd32_to_timespec(&ts32, &rqt);
431
432 error = nanosleep1(l, &rqt, SCARG_P32(uap, rmtp) ? &rmt : NULL);
433 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
434 return error;
435
436 netbsd32_from_timespec(&rmt, &ts32);
437 error1 = copyout(&ts32, SCARG_P32(uap,rmtp), sizeof(ts32));
438 return error1 ? error1 : error;
439 }
440
441 static int
442 netbsd32_timer_create_fetch(const void *src, void *dst, size_t size)
443 {
444 struct sigevent *evp = dst;
445 struct netbsd32_sigevent ev32;
446 int error;
447
448 error = copyin(src, &ev32, sizeof(ev32));
449 if (error)
450 return error;
451
452 netbsd32_to_sigevent(&ev32, evp);
453 return 0;
454 }
455
456 int
457 netbsd32_timer_create(struct lwp *l, const struct netbsd32_timer_create_args *uap, register_t *retval)
458 {
459 /* {
460 syscallarg(netbsd32_clockid_t) clock_id;
461 syscallarg(netbsd32_sigeventp_t) evp;
462 syscallarg(netbsd32_timerp_t) timerid;
463 } */
464
465 return timer_create1(SCARG_P32(uap, timerid),
466 SCARG(uap, clock_id), SCARG_P32(uap, evp),
467 netbsd32_timer_create_fetch, l);
468 }
469
470 int
471 netbsd32_timer_delete(struct lwp *l, const struct netbsd32_timer_delete_args *uap, register_t *retval)
472 {
473 /* {
474 syscallarg(netbsd32_timer_t) timerid;
475 } */
476 struct sys_timer_delete_args ua;
477
478 NETBSD32TO64_UAP(timerid);
479 return sys_timer_delete(l, (void *)&ua, retval);
480 }
481
482 int
483 netbsd32___timer_settime50(struct lwp *l, const struct netbsd32___timer_settime50_args *uap, register_t *retval)
484 {
485 /* {
486 syscallarg(netbsd32_timer_t) timerid;
487 syscallarg(int) flags;
488 syscallarg(const netbsd32_itimerspecp_t) value;
489 syscallarg(netbsd32_itimerspecp_t) ovalue;
490 } */
491 int error;
492 struct itimerspec value, ovalue, *ovp = NULL;
493 struct netbsd32_itimerspec its32;
494
495 if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
496 return (error);
497 netbsd32_to_timespec(&its32.it_interval, &value.it_interval);
498 netbsd32_to_timespec(&its32.it_value, &value.it_value);
499
500 if (SCARG_P32(uap, ovalue))
501 ovp = &ovalue;
502
503 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
504 SCARG(uap, flags), l->l_proc)) != 0)
505 return error;
506
507 if (ovp) {
508 netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval);
509 netbsd32_from_timespec(&ovp->it_value, &its32.it_value);
510 return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
511 }
512 return 0;
513 }
514
515 int
516 netbsd32___timer_gettime50(struct lwp *l, const struct netbsd32___timer_gettime50_args *uap, register_t *retval)
517 {
518 /* {
519 syscallarg(netbsd32_timer_t) timerid;
520 syscallarg(netbsd32_itimerspecp_t) value;
521 } */
522 int error;
523 struct itimerspec its;
524 struct netbsd32_itimerspec its32;
525
526 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
527 &its)) != 0)
528 return error;
529
530 netbsd32_from_timespec(&its.it_interval, &its32.it_interval);
531 netbsd32_from_timespec(&its.it_value, &its32.it_value);
532
533 return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
534 }
535
536 int
537 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval)
538 {
539 /* {
540 syscallarg(netbsd32_timer_t) timerid;
541 } */
542 struct sys_timer_getoverrun_args ua;
543
544 NETBSD32TO64_UAP(timerid);
545 return sys_timer_getoverrun(l, (void *)&ua, retval);
546 }
547