xref: /netbsd-src/sys/compat/common/kern_time_50.c (revision 3816d47b2c42fcd6e549e3407f842a5b1a1d23ad)
1 /*	$NetBSD: kern_time_50.c,v 1.12 2009/11/11 09:48:50 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: kern_time_50.c,v 1.12 2009/11/11 09:48:50 rmind Exp $");
33 
34 #ifdef _KERNEL_OPT
35 #include "opt_aio.h"
36 #include "opt_ntp.h"
37 #include "opt_mqueue.h"
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/filedesc.h>
44 #include <sys/kernel.h>
45 #include <sys/file.h>
46 #include <sys/stat.h>
47 #include <sys/socketvar.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/proc.h>
51 #include <sys/uio.h>
52 #include <sys/dirent.h>
53 #include <sys/malloc.h>
54 #include <sys/kauth.h>
55 #include <sys/time.h>
56 #include <sys/timex.h>
57 #include <sys/timetc.h>
58 #include <sys/aio.h>
59 #include <sys/poll.h>
60 #include <sys/syscallargs.h>
61 #include <sys/resource.h>
62 
63 #include <compat/common/compat_util.h>
64 #include <compat/sys/time.h>
65 #include <compat/sys/timex.h>
66 #include <compat/sys/resource.h>
67 #include <compat/sys/clockctl.h>
68 
69 static int
70 compat_50_kevent_fetch_timeout(const void *src, void *dest, size_t length)
71 {
72 	struct timespec50 ts50;
73 	int error;
74 
75 	KASSERT(length == sizeof(struct timespec));
76 
77 	error = copyin(src, &ts50, sizeof(ts50));
78 	if (error)
79 		return error;
80 	timespec50_to_timespec(&ts50, (struct timespec *)dest);
81 	return 0;
82 }
83 
84 int
85 compat_50_sys_kevent(struct lwp *l, const struct compat_50_sys_kevent_args *uap,
86     register_t *retval)
87 {
88 	/* {
89 		syscallarg(int) fd;
90 		syscallarg(keventp_t) changelist;
91 		syscallarg(size_t) nchanges;
92 		syscallarg(keventp_t) eventlist;
93 		syscallarg(size_t) nevents;
94 		syscallarg(struct timespec50) timeout;
95 	} */
96 	static const struct kevent_ops compat_50_kevent_ops = {
97 		.keo_private = NULL,
98 		.keo_fetch_timeout = compat_50_kevent_fetch_timeout,
99 		.keo_fetch_changes = kevent_fetch_changes,
100 		.keo_put_events = kevent_put_events,
101 	};
102 
103 	return kevent1(retval, SCARG(uap, fd), SCARG(uap, changelist),
104 	    SCARG(uap, nchanges), SCARG(uap, eventlist), SCARG(uap, nevents),
105 	    (const struct timespec *)(const void *)SCARG(uap, timeout),
106 	    &compat_50_kevent_ops);
107 }
108 
109 int
110 compat_50_sys_clock_gettime(struct lwp *l,
111     const struct compat_50_sys_clock_gettime_args *uap, register_t *retval)
112 {
113 	/* {
114 		syscallarg(clockid_t) clock_id;
115 		syscallarg(struct timespec50 *) tp;
116 	} */
117 	clockid_t clock_id;
118 	struct timespec ats;
119 	struct timespec50 ats50;
120 
121 	clock_id = SCARG(uap, clock_id);
122 	switch (clock_id) {
123 	case CLOCK_REALTIME:
124 		nanotime(&ats);
125 		break;
126 	case CLOCK_MONOTONIC:
127 		nanouptime(&ats);
128 		break;
129 	default:
130 		return (EINVAL);
131 	}
132 	timespec_to_timespec50(&ats, &ats50);
133 
134 	return copyout(&ats50, SCARG(uap, tp), sizeof(ats50));
135 }
136 
137 /* ARGSUSED */
138 int
139 compat_50_sys_clock_settime(struct lwp *l,
140     const struct compat_50_sys_clock_settime_args *uap, register_t *retval)
141 {
142 	/* {
143 		syscallarg(clockid_t) clock_id;
144 		syscallarg(const struct timespec50 *) tp;
145 	} */
146 	int error;
147 	struct timespec ats;
148 	struct timespec50 ats50;
149 
150 	error = copyin(SCARG(uap, tp), &ats50, sizeof(ats50));
151 	if (error)
152 		return error;
153 	timespec50_to_timespec(&ats50, &ats);
154 
155 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats,
156 	    true);
157 }
158 
159 
160 int
161 compat_50_sys_clock_getres(struct lwp *l,
162     const struct compat_50_sys_clock_getres_args *uap, register_t *retval)
163 {
164 	/* {
165 		syscallarg(clockid_t) clock_id;
166 		syscallarg(struct timespec50 *) tp;
167 	} */
168 	clockid_t clock_id;
169 	struct timespec50 ats50;
170 	int error = 0;
171 
172 	clock_id = SCARG(uap, clock_id);
173 	switch (clock_id) {
174 	case CLOCK_REALTIME:
175 	case CLOCK_MONOTONIC:
176 		ats50.tv_sec = 0;
177 		if (tc_getfrequency() > 1000000000)
178 			ats50.tv_nsec = 1;
179 		else
180 			ats50.tv_nsec = 1000000000 / tc_getfrequency();
181 		break;
182 	default:
183 		return (EINVAL);
184 	}
185 
186 	if (SCARG(uap, tp))
187 		error = copyout(&ats50, SCARG(uap, tp), sizeof(*SCARG(uap, tp)));
188 
189 	return error;
190 }
191 
192 /* ARGSUSED */
193 int
194 compat_50_sys_nanosleep(struct lwp *l,
195     const struct compat_50_sys_nanosleep_args *uap, register_t *retval)
196 {
197 	/* {
198 		syscallarg(struct timespec50 *) rqtp;
199 		syscallarg(struct timespec50 *) rmtp;
200 	} */
201 	struct timespec rmt, rqt;
202 	struct timespec50 rmt50, rqt50;
203 	int error, error1;
204 
205 	error = copyin(SCARG(uap, rqtp), &rqt50, sizeof(rqt50));
206 	if (error)
207 		return error;
208 	timespec50_to_timespec(&rqt50, &rqt);
209 
210 	error = nanosleep1(l, &rqt, SCARG(uap, rmtp) ? &rmt : NULL);
211 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
212 		return error;
213 
214 	timespec_to_timespec50(&rmt, &rmt50);
215 	error1 = copyout(&rmt50, SCARG(uap, rmtp), sizeof(*SCARG(uap, rmtp)));
216 	return error1 ? error1 : error;
217 }
218 
219 /* ARGSUSED */
220 int
221 compat_50_sys_gettimeofday(struct lwp *l,
222     const struct compat_50_sys_gettimeofday_args *uap, register_t *retval)
223 {
224 	/* {
225 		syscallarg(struct timeval50 *) tp;
226 		syscallarg(void *) tzp;		really "struct timezone *";
227 	} */
228 	struct timeval atv;
229 	struct timeval50 atv50;
230 	int error = 0;
231 	struct timezone tzfake;
232 
233 	if (SCARG(uap, tp)) {
234 		microtime(&atv);
235 		timeval_to_timeval50(&atv, &atv50);
236 		error = copyout(&atv50, SCARG(uap, tp), sizeof(*SCARG(uap, tp)));
237 		if (error)
238 			return error;
239 	}
240 	if (SCARG(uap, tzp)) {
241 		/*
242 		 * NetBSD has no kernel notion of time zone, so we just
243 		 * fake up a timezone struct and return it if demanded.
244 		 */
245 		tzfake.tz_minuteswest = 0;
246 		tzfake.tz_dsttime = 0;
247 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
248 	}
249 	return error;
250 }
251 
252 /* ARGSUSED */
253 int
254 compat_50_sys_settimeofday(struct lwp *l,
255     const struct compat_50_sys_settimeofday_args *uap, register_t *retval)
256 {
257 	/* {
258 		syscallarg(const struct timeval50 *) tv;
259 		syscallarg(const void *) tzp; really "const struct timezone *";
260 	} */
261 	struct timeval50 atv50;
262 	struct timeval atv;
263 	int error = copyin(SCARG(uap, tv), &atv50, sizeof(atv50));
264 	if (error)
265 		return error;
266 	timeval50_to_timeval(&atv50, &atv);
267 	return settimeofday1(&atv, false, SCARG(uap, tzp), l, true);
268 }
269 
270 /* ARGSUSED */
271 int
272 compat_50_sys_adjtime(struct lwp *l,
273     const struct compat_50_sys_adjtime_args *uap, register_t *retval)
274 {
275 	/* {
276 		syscallarg(const struct timeval50 *) delta;
277 		syscallarg(struct timeval50 *) olddelta;
278 	} */
279 	int error;
280 	struct timeval50 delta50, olddelta50;
281 	struct timeval delta, olddelta;
282 
283 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
284 	    KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
285 		return error;
286 
287 	if (SCARG(uap, delta)) {
288 		error = copyin(SCARG(uap, delta), &delta50,
289 		    sizeof(*SCARG(uap, delta)));
290 		if (error)
291 			return (error);
292 		timeval50_to_timeval(&delta50, &delta);
293 	}
294 	adjtime1(SCARG(uap, delta) ? &delta : NULL,
295 	    SCARG(uap, olddelta) ? &olddelta : NULL, l->l_proc);
296 	if (SCARG(uap, olddelta)) {
297 		timeval_to_timeval50(&olddelta, &olddelta50);
298 		error = copyout(&olddelta50, SCARG(uap, olddelta),
299 		    sizeof(*SCARG(uap, olddelta)));
300 	}
301 	return error;
302 }
303 
304 /* BSD routine to set/arm an interval timer. */
305 /* ARGSUSED */
306 int
307 compat_50_sys_getitimer(struct lwp *l,
308     const struct compat_50_sys_getitimer_args *uap, register_t *retval)
309 {
310 	/* {
311 		syscallarg(int) which;
312 		syscallarg(struct itimerval50 *) itv;
313 	} */
314 	struct proc *p = l->l_proc;
315 	struct itimerval aitv;
316 	struct itimerval50 aitv50;
317 	int error;
318 
319 	error = dogetitimer(p, SCARG(uap, which), &aitv);
320 	if (error)
321 		return error;
322 	itimerval_to_itimerval50(&aitv, &aitv50);
323 	return copyout(&aitv50, SCARG(uap, itv), sizeof(*SCARG(uap, itv)));
324 }
325 
326 int
327 compat_50_sys_setitimer(struct lwp *l,
328     const struct compat_50_sys_setitimer_args *uap, register_t *retval)
329 {
330 	/* {
331 		syscallarg(int) which;
332 		syscallarg(const struct itimerval50 *) itv;
333 		syscallarg(struct itimerval50 *) oitv;
334 	} */
335 	struct proc *p = l->l_proc;
336 	int which = SCARG(uap, which);
337 	struct compat_50_sys_getitimer_args getargs;
338 	const struct itimerval50 *itvp;
339 	struct itimerval50 aitv50;
340 	struct itimerval aitv;
341 	int error;
342 
343 	if ((u_int)which > ITIMER_PROF)
344 		return (EINVAL);
345 	itvp = SCARG(uap, itv);
346 	if (itvp &&
347 	    (error = copyin(itvp, &aitv50, sizeof(aitv50)) != 0))
348 		return (error);
349 	itimerval50_to_itimerval(&aitv50, &aitv);
350 	if (SCARG(uap, oitv) != NULL) {
351 		SCARG(&getargs, which) = which;
352 		SCARG(&getargs, itv) = SCARG(uap, oitv);
353 		if ((error = compat_50_sys_getitimer(l, &getargs, retval)) != 0)
354 			return (error);
355 	}
356 	if (itvp == 0)
357 		return (0);
358 
359 	return dosetitimer(p, which, &aitv);
360 }
361 
362 int
363 compat_50_sys_aio_suspend(struct lwp *l,
364     const struct compat_50_sys_aio_suspend_args *uap, register_t *retval)
365 {
366 	/* {
367 		syscallarg(const struct aiocb *const[]) list;
368 		syscallarg(int) nent;
369 		syscallarg(const struct timespec50 *) timeout;
370 	} */
371 #ifdef AIO
372 	struct aiocb **list;
373 	struct timespec ts;
374 	struct timespec50 ts50;
375 	int error, nent;
376 
377 	nent = SCARG(uap, nent);
378 	if (nent <= 0 || nent > aio_listio_max)
379 		return EAGAIN;
380 
381 	if (SCARG(uap, timeout)) {
382 		/* Convert timespec to ticks */
383 		error = copyin(SCARG(uap, timeout), &ts50,
384 		    sizeof(*SCARG(uap, timeout)));
385 		if (error)
386 			return error;
387 		timespec50_to_timespec(&ts50, &ts);
388 	}
389 	list = kmem_alloc(nent * sizeof(*list), KM_SLEEP);
390 	error = copyin(SCARG(uap, list), list, nent * sizeof(*list));
391 	if (error)
392 		goto out;
393 	error = aio_suspend1(l, list, nent, SCARG(uap, timeout) ? &ts : NULL);
394 out:
395 	kmem_free(list, nent * sizeof(*list));
396 	return error;
397 #else
398 	return ENOSYS;
399 #endif
400 }
401 
402 int
403 compat_50_sys_select(struct lwp *l,
404     const struct compat_50_sys_select_args *uap, register_t *retval)
405 {
406 	/* {
407 		syscallarg(int)			nd;
408 		syscallarg(fd_set *)		in;
409 		syscallarg(fd_set *)		ou;
410 		syscallarg(fd_set *)		ex;
411 		syscallarg(struct timeval50 *)	tv;
412 	} */
413 	struct timespec ats, *ts = NULL;
414 	struct timeval50 atv50;
415 	int error;
416 
417 	if (SCARG(uap, tv)) {
418 		error = copyin(SCARG(uap, tv), (void *)&atv50, sizeof(atv50));
419 		if (error)
420 			return error;
421 		ats.tv_sec = atv50.tv_sec;
422 		ats.tv_nsec = atv50.tv_usec * 1000;
423 		ts = &ats;
424 	}
425 
426 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
427 	    SCARG(uap, ou), SCARG(uap, ex), ts, NULL);
428 }
429 
430 int
431 compat_50_sys_pselect(struct lwp *l,
432     const struct compat_50_sys_pselect_args *uap, register_t *retval)
433 {
434 	/* {
435 		syscallarg(int)				nd;
436 		syscallarg(fd_set *)			in;
437 		syscallarg(fd_set *)			ou;
438 		syscallarg(fd_set *)			ex;
439 		syscallarg(const struct timespec50 *)	ts;
440 		syscallarg(sigset_t *)			mask;
441 	} */
442 	struct timespec50	ats50;
443 	struct timespec	ats, *ts = NULL;
444 	sigset_t	amask, *mask = NULL;
445 	int		error;
446 
447 	if (SCARG(uap, ts)) {
448 		error = copyin(SCARG(uap, ts), &ats50, sizeof(ats50));
449 		if (error)
450 			return error;
451 		timespec50_to_timespec(&ats50, &ats);
452 		ts = &ats;
453 	}
454 	if (SCARG(uap, mask) != NULL) {
455 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
456 		if (error)
457 			return error;
458 		mask = &amask;
459 	}
460 
461 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
462 	    SCARG(uap, ou), SCARG(uap, ex), ts, mask);
463 }
464 int
465 compat_50_sys_pollts(struct lwp *l, const struct compat_50_sys_pollts_args *uap,
466     register_t *retval)
467 {
468 	/* {
469 		syscallarg(struct pollfd *)		fds;
470 		syscallarg(u_int)			nfds;
471 		syscallarg(const struct timespec50 *)	ts;
472 		syscallarg(const sigset_t *)		mask;
473 	} */
474 	struct timespec	ats, *ts = NULL;
475 	struct timespec50 ats50;
476 	sigset_t	amask, *mask = NULL;
477 	int		error;
478 
479 	if (SCARG(uap, ts)) {
480 		error = copyin(SCARG(uap, ts), &ats50, sizeof(ats50));
481 		if (error)
482 			return error;
483 		timespec50_to_timespec(&ats50, &ats);
484 		ts = &ats;
485 	}
486 	if (SCARG(uap, mask)) {
487 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
488 		if (error)
489 			return error;
490 		mask = &amask;
491 	}
492 
493 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds), ts, mask);
494 }
495 
496 int
497 compat_50_sys__lwp_park(struct lwp *l,
498     const struct compat_50_sys__lwp_park_args *uap, register_t *retval)
499 {
500 	/* {
501 		syscallarg(const struct timespec50 *)	ts;
502 		syscallarg(lwpid_t)			unpark;
503 		syscallarg(const void *)		hint;
504 		syscallarg(const void *)		unparkhint;
505 	} */
506 	struct timespec ts, *tsp;
507 	struct timespec50 ts50;
508 	int error;
509 
510 	if (SCARG(uap, ts) == NULL)
511 		tsp = NULL;
512 	else {
513 		error = copyin(SCARG(uap, ts), &ts50, sizeof(ts50));
514 		if (error != 0)
515 			return error;
516 		timespec50_to_timespec(&ts50, &ts);
517 		tsp = &ts;
518 	}
519 
520 	if (SCARG(uap, unpark) != 0) {
521 		error = lwp_unpark(SCARG(uap, unpark), SCARG(uap, unparkhint));
522 		if (error != 0)
523 			return error;
524 	}
525 
526 	return lwp_park(tsp, SCARG(uap, hint));
527 }
528 
529 int
530 compat_50_sys_mq_timedsend(struct lwp *l,
531     const struct compat_50_sys_mq_timedsend_args *uap, register_t *retval)
532 {
533 	/* {
534 		syscallarg(mqd_t) mqdes;
535 		syscallarg(const char *) msg_ptr;
536 		syscallarg(size_t) msg_len;
537 		syscallarg(unsigned) msg_prio;
538 		syscallarg(const struct timespec50 *) abs_timeout;
539 	} */
540 #ifdef MQUEUE
541 	struct timespec50 ts50;
542 	struct timespec ts, *tsp;
543 	int error;
544 
545 	/* Get and convert time value */
546 	if (SCARG(uap, abs_timeout)) {
547 		error = copyin(SCARG(uap, abs_timeout), &ts50, sizeof(ts50));
548 		if (error)
549 			return error;
550 		timespec50_to_timespec(&ts50, &ts);
551 		tsp = &ts;
552 	} else {
553 		tsp = NULL;
554 	}
555 
556 	return mq_send1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
557 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
558 #else
559 	return ENOSYS;
560 #endif
561 }
562 
563 int
564 compat_50_sys_mq_timedreceive(struct lwp *l,
565     const struct compat_50_sys_mq_timedreceive_args *uap, register_t *retval)
566 {
567 	/* {
568 		syscallarg(mqd_t) mqdes;
569 		syscallarg(char *) msg_ptr;
570 		syscallarg(size_t) msg_len;
571 		syscallarg(unsigned *) msg_prio;
572 		syscallarg(const struct timespec50 *) abs_timeout;
573 	} */
574 #ifdef MQUEUE
575 	struct timespec ts, *tsp;
576 	struct timespec50 ts50;
577 	ssize_t mlen;
578 	int error;
579 
580 	/* Get and convert time value */
581 	if (SCARG(uap, abs_timeout)) {
582 		error = copyin(SCARG(uap, abs_timeout), &ts50, sizeof(ts50));
583 		if (error)
584 			return error;
585 
586 		timespec50_to_timespec(&ts50, &ts);
587 		tsp = &ts;
588 	} else {
589 		tsp = NULL;
590 	}
591 
592 	error = mq_recv1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
593 	    SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp, &mlen);
594 	if (error == 0)
595 		*retval = mlen;
596 
597 	return error;
598 #else
599 	return ENOSYS;
600 #endif
601 }
602 
603 static int
604 tscopyin(const void *u, void *s, size_t len)
605 {
606 	struct timespec50 ts50;
607 	KASSERT(len == sizeof(ts50));
608 	int error = copyin(u, &ts50, len);
609 	if (error)
610 		return error;
611 	timespec50_to_timespec(&ts50, s);
612 	return 0;
613 }
614 
615 static int
616 tscopyout(const void *s, void *u, size_t len)
617 {
618 	struct timespec50 ts50;
619 	KASSERT(len == sizeof(ts50));
620 	timespec_to_timespec50(s, &ts50);
621 	int error = copyout(&ts50, u, len);
622 	if (error)
623 		return error;
624 	return 0;
625 }
626 
627 int
628 compat_50_sys___sigtimedwait(struct lwp *l,
629     const struct compat_50_sys___sigtimedwait_args *uap, register_t *retval)
630 {
631 
632 	return __sigtimedwait1(l,
633 	    (const struct sys_____sigtimedwait50_args *)uap, retval, copyout,
634 	    tscopyin, tscopyout);
635 }
636 
637 void
638 rusage_to_rusage50(const struct rusage *ru, struct rusage50 *ru50)
639 {
640 	(void)memcpy(&ru50->ru_first, &ru->ru_first,
641 	    (char *)&ru50->ru_last - (char *)&ru50->ru_first +
642 	    sizeof(ru50->ru_last));
643 	ru50->ru_maxrss = ru->ru_maxrss;
644 	timeval_to_timeval50(&ru->ru_utime, &ru50->ru_utime);
645 	timeval_to_timeval50(&ru->ru_stime, &ru50->ru_stime);
646 }
647 
648 int
649 compat_50_sys_getrusage(struct lwp *l,
650     const struct compat_50_sys_getrusage_args *uap, register_t *retval)
651 {
652 	/* {
653 		syscallarg(int) who;
654 		syscallarg(struct rusage50 *) rusage;
655 	} */
656 	struct rusage ru;
657 	struct rusage50 ru50;
658 	struct proc *p = l->l_proc;
659 
660 	switch (SCARG(uap, who)) {
661 	case RUSAGE_SELF:
662 		mutex_enter(p->p_lock);
663 		memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
664 		calcru(p, &ru.ru_utime, &ru.ru_stime, NULL, NULL);
665 		mutex_exit(p->p_lock);
666 		break;
667 
668 	case RUSAGE_CHILDREN:
669 		mutex_enter(p->p_lock);
670 		memcpy(&ru, &p->p_stats->p_cru, sizeof(ru));
671 		mutex_exit(p->p_lock);
672 		break;
673 
674 	default:
675 		return EINVAL;
676 	}
677 	rusage_to_rusage50(&ru, &ru50);
678 	return copyout(&ru50, SCARG(uap, rusage), sizeof(ru50));
679 }
680 
681 
682 /* Return the time remaining until a POSIX timer fires. */
683 int
684 compat_50_sys_timer_gettime(struct lwp *l,
685     const struct compat_50_sys_timer_gettime_args *uap, register_t *retval)
686 {
687 	/* {
688 		syscallarg(timer_t) timerid;
689 		syscallarg(struct itimerspec50 *) value;
690 	} */
691 	struct itimerspec its;
692 	struct itimerspec50 its50;
693 	int error;
694 
695 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
696 	    &its)) != 0)
697 		return error;
698 	itimerspec_to_itimerspec50(&its, &its50);
699 
700 	return copyout(&its50, SCARG(uap, value), sizeof(its50));
701 }
702 
703 /* Set and arm a POSIX realtime timer */
704 int
705 compat_50_sys_timer_settime(struct lwp *l,
706     const struct compat_50_sys_timer_settime_args *uap, register_t *retval)
707 {
708 	/* {
709 		syscallarg(timer_t) timerid;
710 		syscallarg(int) flags;
711 		syscallarg(const struct itimerspec50 *) value;
712 		syscallarg(struct itimerspec50 *) ovalue;
713 	} */
714 	int error;
715 	struct itimerspec value, ovalue, *ovp = NULL;
716 	struct itimerspec50 value50, ovalue50;
717 
718 	if ((error = copyin(SCARG(uap, value), &value50, sizeof(value50))) != 0)
719 		return error;
720 
721 	itimerspec50_to_itimerspec(&value50, &value);
722 	if (SCARG(uap, ovalue))
723 		ovp = &ovalue;
724 
725 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
726 	    SCARG(uap, flags), l->l_proc)) != 0)
727 		return error;
728 
729 	if (ovp) {
730 		itimerspec_to_itimerspec50(&ovalue, &ovalue50);
731 		return copyout(&ovalue50, SCARG(uap, ovalue), sizeof(ovalue50));
732 	}
733 	return 0;
734 }
735 
736 /*
737  * ntp_gettime() - NTP user application interface
738  */
739 int
740 compat_50_sys___ntp_gettime30(struct lwp *l,
741     const struct compat_50_sys___ntp_gettime30_args *uap, register_t *retval)
742 {
743 #ifdef NTP
744 	/* {
745 		syscallarg(struct ntptimeval *) ntvp;
746 	} */
747 	struct ntptimeval ntv;
748 	struct ntptimeval50 ntv50;
749 	int error;
750 
751 	if (SCARG(uap, ntvp)) {
752 		ntp_gettime(&ntv);
753 		timespec_to_timespec50(&ntv.time, &ntv50.time);
754 		ntv50.maxerror = ntv.maxerror;
755 		ntv50.esterror = ntv.esterror;
756 		ntv50.tai = ntv.tai;
757 		ntv50.time_state = ntv.time_state;
758 
759 		error = copyout(&ntv50, SCARG(uap, ntvp), sizeof(ntv50));
760 		if (error)
761 			return error;
762 	}
763 	*retval = ntp_timestatus();
764 	return 0;
765 #else
766 	return ENOSYS;
767 #endif
768 }
769 int
770 compat50_clockctlioctl(dev_t dev, u_long cmd, void *data, int flags,
771     struct lwp *l)
772 {
773 	int error = 0;
774 
775 	switch (cmd) {
776 	case CLOCKCTL_OSETTIMEOFDAY: {
777 		struct timeval50 tv50;
778 		struct timeval tv;
779 		struct clockctl50_settimeofday *args = data;
780 
781 		error = copyin(args->tv, &tv50, sizeof(tv50));
782 		if (error)
783 			return (error);
784 		timeval50_to_timeval(&tv50, &tv);
785 		error = settimeofday1(&tv, false, args->tzp, l, false);
786 		break;
787 	}
788 	case CLOCKCTL_OADJTIME: {
789 		struct timeval atv, oldatv;
790 		struct timeval50 atv50;
791 		struct clockctl50_adjtime *args = data;
792 
793 		if (args->delta) {
794 			error = copyin(args->delta, &atv50, sizeof(atv50));
795 			if (error)
796 				return (error);
797 			timeval50_to_timeval(&atv50, &atv);
798 		}
799 		adjtime1(args->delta ? &atv : NULL,
800 		    args->olddelta ? &oldatv : NULL, l->l_proc);
801 		if (args->olddelta) {
802 			timeval_to_timeval50(&oldatv, &atv50);
803 			error = copyout(&atv50, args->olddelta, sizeof(atv50));
804 		}
805 		break;
806 	}
807 	case CLOCKCTL_OCLOCK_SETTIME: {
808 		struct timespec50 tp50;
809 		struct timespec tp;
810 		struct clockctl50_clock_settime *args = data;
811 
812 		error = copyin(args->tp, &tp50, sizeof(tp50));
813 		if (error)
814 			return (error);
815 		timespec50_to_timespec(&tp50, &tp);
816 		error = clock_settime1(l->l_proc, args->clock_id, &tp, true);
817 		break;
818 	}
819 	default:
820 		error = EINVAL;
821 	}
822 
823 	return (error);
824 }
825 int
826 compat_50_sys_wait4(struct lwp *l, const struct compat_50_sys_wait4_args *uap,
827     register_t *retval)
828 {
829 	/* {
830 		syscallarg(int)			pid;
831 		syscallarg(int *)		status;
832 		syscallarg(int)			options;
833 		syscallarg(struct rusage50 *)	rusage;
834 	} */
835 	int status, error, pid = SCARG(uap, pid);
836 	struct rusage50 ru50;
837 	struct rusage ru;
838 
839 	error = do_sys_wait(&pid, &status, SCARG(uap, options),
840 	    SCARG(uap, rusage) != NULL ? &ru : NULL);
841 
842 	retval[0] = pid;
843 	if (pid == 0)
844 		return error;
845 
846 	if (SCARG(uap, rusage)) {
847 		rusage_to_rusage50(&ru, &ru50);
848 		error = copyout(&ru50, SCARG(uap, rusage), sizeof(ru50));
849 	}
850 
851 	if (error == 0 && SCARG(uap, status))
852 		error = copyout(&status, SCARG(uap, status), sizeof(status));
853 
854 	return error;
855 }
856