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