netbsd32_time.c revision 1.43 1 /* $NetBSD: netbsd32_time.c,v 1.43 2015/07/12 14:06:52 martin 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.43 2015/07/12 14:06:52 martin 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(atv));
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 int error;
357 struct timespec ats;
358 struct netbsd32_timespec ts32;
359
360 error = clock_gettime1(SCARG(uap, clock_id), &ats);
361 if (error != 0)
362 return error;
363
364 netbsd32_from_timespec(&ats, &ts32);
365 return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
366 }
367
368 int
369 netbsd32___clock_settime50(struct lwp *l, const struct netbsd32___clock_settime50_args *uap, register_t *retval)
370 {
371 /* {
372 syscallarg(netbsd32_clockid_t) clock_id;
373 syscallarg(const netbsd32_timespecp_t) tp;
374 } */
375 struct netbsd32_timespec ts32;
376 struct timespec ats;
377 int error;
378
379 if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0)
380 return (error);
381
382 netbsd32_to_timespec(&ts32, &ats);
383 return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
384 }
385
386 int
387 netbsd32___clock_getres50(struct lwp *l, const struct netbsd32___clock_getres50_args *uap, register_t *retval)
388 {
389 /* {
390 syscallarg(netbsd32_clockid_t) clock_id;
391 syscallarg(netbsd32_timespecp_t) tp;
392 } */
393 struct netbsd32_timespec ts32;
394 struct timespec ts;
395 int error = 0;
396
397 error = clock_getres1(SCARG(uap, clock_id), &ts);
398 if (error != 0)
399 return error;
400
401 if (SCARG_P32(uap, tp)) {
402 netbsd32_from_timespec(&ts, &ts32);
403 error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32));
404 }
405
406 return error;
407 }
408
409 int
410 netbsd32___nanosleep50(struct lwp *l, const struct netbsd32___nanosleep50_args *uap, register_t *retval)
411 {
412 /* {
413 syscallarg(const netbsd32_timespecp_t) rqtp;
414 syscallarg(netbsd32_timespecp_t) rmtp;
415 } */
416 struct netbsd32_timespec ts32;
417 struct timespec rqt, rmt;
418 int error, error1;
419
420 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
421 if (error)
422 return (error);
423 netbsd32_to_timespec(&ts32, &rqt);
424
425 error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
426 SCARG_P32(uap, rmtp) ? &rmt : NULL);
427 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
428 return error;
429
430 netbsd32_from_timespec(&rmt, &ts32);
431 error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32));
432 return error1 ? error1 : error;
433 }
434
435 int
436 netbsd32_clock_nanosleep(struct lwp *l, const struct netbsd32_clock_nanosleep_args *uap, register_t *retval)
437 {
438 /* {
439 clockid_t clock_id;
440 int flags;
441 syscallarg(const netbsd32_timespecp_t) rqtp;
442 syscallarg(netbsd32_timespecp_t) rmtp;
443 } */
444 struct netbsd32_timespec ts32;
445 struct timespec rqt, rmt;
446 int error, error1;
447
448 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32));
449 if (error)
450 return (error);
451 netbsd32_to_timespec(&ts32, &rqt);
452
453 error = nanosleep1(l, SCARG(uap, clock_id), SCARG(uap, flags),
454 &rqt, SCARG_P32(uap, rmtp) ? &rmt : NULL);
455 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR))
456 return error;
457
458 netbsd32_from_timespec(&rmt, &ts32);
459 error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32));
460 return error1 ? error1 : error;
461 }
462
463 static int
464 netbsd32_timer_create_fetch(const void *src, void *dst, size_t size)
465 {
466 struct sigevent *evp = dst;
467 struct netbsd32_sigevent ev32;
468 int error;
469
470 error = copyin(src, &ev32, sizeof(ev32));
471 if (error)
472 return error;
473
474 netbsd32_to_sigevent(&ev32, evp);
475 return 0;
476 }
477
478 int
479 netbsd32_timer_create(struct lwp *l, const struct netbsd32_timer_create_args *uap, register_t *retval)
480 {
481 /* {
482 syscallarg(netbsd32_clockid_t) clock_id;
483 syscallarg(netbsd32_sigeventp_t) evp;
484 syscallarg(netbsd32_timerp_t) timerid;
485 } */
486
487 return timer_create1(SCARG_P32(uap, timerid),
488 SCARG(uap, clock_id), SCARG_P32(uap, evp),
489 netbsd32_timer_create_fetch, l);
490 }
491
492 int
493 netbsd32_timer_delete(struct lwp *l, const struct netbsd32_timer_delete_args *uap, register_t *retval)
494 {
495 /* {
496 syscallarg(netbsd32_timer_t) timerid;
497 } */
498 struct sys_timer_delete_args ua;
499
500 NETBSD32TO64_UAP(timerid);
501 return sys_timer_delete(l, (void *)&ua, retval);
502 }
503
504 int
505 netbsd32___timer_settime50(struct lwp *l, const struct netbsd32___timer_settime50_args *uap, register_t *retval)
506 {
507 /* {
508 syscallarg(netbsd32_timer_t) timerid;
509 syscallarg(int) flags;
510 syscallarg(const netbsd32_itimerspecp_t) value;
511 syscallarg(netbsd32_itimerspecp_t) ovalue;
512 } */
513 int error;
514 struct itimerspec value, ovalue, *ovp = NULL;
515 struct netbsd32_itimerspec its32;
516
517 if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0)
518 return (error);
519 netbsd32_to_timespec(&its32.it_interval, &value.it_interval);
520 netbsd32_to_timespec(&its32.it_value, &value.it_value);
521
522 if (SCARG_P32(uap, ovalue))
523 ovp = &ovalue;
524
525 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
526 SCARG(uap, flags), l->l_proc)) != 0)
527 return error;
528
529 if (ovp) {
530 netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval);
531 netbsd32_from_timespec(&ovp->it_value, &its32.it_value);
532 return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32));
533 }
534 return 0;
535 }
536
537 int
538 netbsd32___timer_gettime50(struct lwp *l, const struct netbsd32___timer_gettime50_args *uap, register_t *retval)
539 {
540 /* {
541 syscallarg(netbsd32_timer_t) timerid;
542 syscallarg(netbsd32_itimerspecp_t) value;
543 } */
544 int error;
545 struct itimerspec its;
546 struct netbsd32_itimerspec its32;
547
548 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
549 &its)) != 0)
550 return error;
551
552 netbsd32_from_timespec(&its.it_interval, &its32.it_interval);
553 netbsd32_from_timespec(&its.it_value, &its32.it_value);
554
555 return copyout(&its32, SCARG_P32(uap, value), sizeof(its32));
556 }
557
558 int
559 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval)
560 {
561 /* {
562 syscallarg(netbsd32_timer_t) timerid;
563 } */
564 struct sys_timer_getoverrun_args ua;
565
566 NETBSD32TO64_UAP(timerid);
567 return sys_timer_getoverrun(l, (void *)&ua, retval);
568 }
569