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