1*ab413307Schristos /* $NetBSD: kern_time_50.c,v 1.38 2024/01/19 18:39:15 christos Exp $ */
2461a86f9Schristos
3461a86f9Schristos /*-
4029f6c41Srmind * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5461a86f9Schristos * All rights reserved.
6461a86f9Schristos *
7461a86f9Schristos * This code is derived from software contributed to The NetBSD Foundation
8461a86f9Schristos * by Christos Zoulas.
9461a86f9Schristos *
10461a86f9Schristos * Redistribution and use in source and binary forms, with or without
11461a86f9Schristos * modification, are permitted provided that the following conditions
12461a86f9Schristos * are met:
13461a86f9Schristos * 1. Redistributions of source code must retain the above copyright
14461a86f9Schristos * notice, this list of conditions and the following disclaimer.
15461a86f9Schristos * 2. Redistributions in binary form must reproduce the above copyright
16461a86f9Schristos * notice, this list of conditions and the following disclaimer in the
17461a86f9Schristos * documentation and/or other materials provided with the distribution.
18461a86f9Schristos *
19461a86f9Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20461a86f9Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21461a86f9Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22461a86f9Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23461a86f9Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24461a86f9Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25461a86f9Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26461a86f9Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27461a86f9Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28461a86f9Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29461a86f9Schristos * POSSIBILITY OF SUCH DAMAGE.
30461a86f9Schristos */
31461a86f9Schristos #include <sys/cdefs.h>
32*ab413307Schristos __KERNEL_RCSID(0, "$NetBSD: kern_time_50.c,v 1.38 2024/01/19 18:39:15 christos Exp $");
33461a86f9Schristos
34461a86f9Schristos #ifdef _KERNEL_OPT
35d91f98a8Spgoyette #include "opt_compat_netbsd.h"
36029f6c41Srmind #include "opt_aio.h"
37461a86f9Schristos #include "opt_ntp.h"
38029f6c41Srmind #include "opt_mqueue.h"
39461a86f9Schristos #endif
40461a86f9Schristos
41461a86f9Schristos #include <sys/param.h>
420f20d948Sapb #include <sys/conf.h>
43461a86f9Schristos #include <sys/systm.h>
44461a86f9Schristos #include <sys/namei.h>
45461a86f9Schristos #include <sys/filedesc.h>
46461a86f9Schristos #include <sys/kernel.h>
47461a86f9Schristos #include <sys/file.h>
48461a86f9Schristos #include <sys/stat.h>
49461a86f9Schristos #include <sys/socketvar.h>
50461a86f9Schristos #include <sys/vnode.h>
51461a86f9Schristos #include <sys/proc.h>
52461a86f9Schristos #include <sys/uio.h>
53461a86f9Schristos #include <sys/dirent.h>
54461a86f9Schristos #include <sys/kauth.h>
55461a86f9Schristos #include <sys/time.h>
56461a86f9Schristos #include <sys/timex.h>
57989df4c7Sapb #include <sys/clockctl.h>
58461a86f9Schristos #include <sys/aio.h>
59461a86f9Schristos #include <sys/poll.h>
60d91f98a8Spgoyette #include <sys/syscall.h>
61461a86f9Schristos #include <sys/syscallargs.h>
62d91f98a8Spgoyette #include <sys/syscallvar.h>
63e8ac3e27Spgoyette #include <sys/sysctl.h>
64461a86f9Schristos #include <sys/resource.h>
65d91f98a8Spgoyette #include <sys/compat_stub.h>
66461a86f9Schristos
67461a86f9Schristos #include <compat/common/compat_util.h>
68e8ac3e27Spgoyette #include <compat/common/compat_mod.h>
69461a86f9Schristos #include <compat/sys/time.h>
70461a86f9Schristos #include <compat/sys/timex.h>
71461a86f9Schristos #include <compat/sys/resource.h>
72461a86f9Schristos #include <compat/sys/clockctl.h>
73461a86f9Schristos
74e8ac3e27Spgoyette struct timeval50 boottime50;
75e8ac3e27Spgoyette
76d91f98a8Spgoyette static const struct syscall_package kern_time_50_syscalls[] = {
77d91f98a8Spgoyette { SYS_compat_50_clock_gettime, 0,
78d91f98a8Spgoyette (sy_call_t *)compat_50_sys_clock_gettime },
79d91f98a8Spgoyette { SYS_compat_50_clock_settime, 0,
80d91f98a8Spgoyette (sy_call_t *)compat_50_sys_clock_settime },
81d91f98a8Spgoyette { SYS_compat_50_clock_getres, 0,
82d91f98a8Spgoyette (sy_call_t *)compat_50_sys_clock_getres},
83d91f98a8Spgoyette { SYS_compat_50_nanosleep, 0, (sy_call_t *)compat_50_sys_nanosleep },
84d91f98a8Spgoyette { SYS_compat_50_gettimeofday, 0,
85d91f98a8Spgoyette (sy_call_t *)compat_50_sys_gettimeofday },
86d91f98a8Spgoyette { SYS_compat_50_settimeofday, 0,
87d91f98a8Spgoyette (sy_call_t *)compat_50_sys_settimeofday },
88d91f98a8Spgoyette { SYS_compat_50_adjtime, 0, (sy_call_t *)compat_50_sys_adjtime },
89d91f98a8Spgoyette { SYS_compat_50_setitimer, 0, (sy_call_t *)compat_50_sys_setitimer },
90d91f98a8Spgoyette { SYS_compat_50_getitimer, 0, (sy_call_t *)compat_50_sys_getitimer },
91d91f98a8Spgoyette { SYS_compat_50_aio_suspend, 0,
92d91f98a8Spgoyette (sy_call_t *)compat_50_sys_aio_suspend },
93d91f98a8Spgoyette { SYS_compat_50_mq_timedsend, 0,
94d91f98a8Spgoyette (sy_call_t *)compat_50_sys_mq_timedsend },
95d91f98a8Spgoyette { SYS_compat_50_mq_timedreceive, 0,
96d91f98a8Spgoyette (sy_call_t *)compat_50_sys_mq_timedreceive },
97d91f98a8Spgoyette { SYS_compat_50_getrusage, 0, (sy_call_t *)compat_50_sys_getrusage },
98d91f98a8Spgoyette { SYS_compat_50_timer_settime, 0,
99d91f98a8Spgoyette (sy_call_t *)compat_50_sys_timer_settime },
100d91f98a8Spgoyette { SYS_compat_50_timer_gettime, 0,
101d91f98a8Spgoyette (sy_call_t *)compat_50_sys_timer_gettime },
102d91f98a8Spgoyette { SYS_compat_50___ntp_gettime30, 0,
103d91f98a8Spgoyette (sy_call_t *)compat_50_sys___ntp_gettime30 },
104d91f98a8Spgoyette { 0, 0, NULL }
105d91f98a8Spgoyette };
106d91f98a8Spgoyette
107461a86f9Schristos int
compat_50_sys_clock_gettime(struct lwp * l,const struct compat_50_sys_clock_gettime_args * uap,register_t * retval)108461a86f9Schristos compat_50_sys_clock_gettime(struct lwp *l,
109461a86f9Schristos const struct compat_50_sys_clock_gettime_args *uap, register_t *retval)
110461a86f9Schristos {
111461a86f9Schristos /* {
112461a86f9Schristos syscallarg(clockid_t) clock_id;
113461a86f9Schristos syscallarg(struct timespec50 *) tp;
114461a86f9Schristos } */
1154f2ea8f3Snjoly int error;
116461a86f9Schristos struct timespec ats;
117461a86f9Schristos struct timespec50 ats50;
118461a86f9Schristos
1194f2ea8f3Snjoly error = clock_gettime1(SCARG(uap, clock_id), &ats);
1204f2ea8f3Snjoly if (error != 0)
1214f2ea8f3Snjoly return error;
1224f2ea8f3Snjoly
123461a86f9Schristos timespec_to_timespec50(&ats, &ats50);
124461a86f9Schristos
125461a86f9Schristos return copyout(&ats50, SCARG(uap, tp), sizeof(ats50));
126461a86f9Schristos }
127461a86f9Schristos
128461a86f9Schristos /* ARGSUSED */
129461a86f9Schristos int
compat_50_sys_clock_settime(struct lwp * l,const struct compat_50_sys_clock_settime_args * uap,register_t * retval)130461a86f9Schristos compat_50_sys_clock_settime(struct lwp *l,
131461a86f9Schristos const struct compat_50_sys_clock_settime_args *uap, register_t *retval)
132461a86f9Schristos {
133461a86f9Schristos /* {
134461a86f9Schristos syscallarg(clockid_t) clock_id;
135461a86f9Schristos syscallarg(const struct timespec50 *) tp;
136461a86f9Schristos } */
137461a86f9Schristos int error;
138461a86f9Schristos struct timespec ats;
139461a86f9Schristos struct timespec50 ats50;
140461a86f9Schristos
141461a86f9Schristos error = copyin(SCARG(uap, tp), &ats50, sizeof(ats50));
142461a86f9Schristos if (error)
143461a86f9Schristos return error;
144461a86f9Schristos timespec50_to_timespec(&ats50, &ats);
145461a86f9Schristos
146461a86f9Schristos return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats,
147461a86f9Schristos true);
148461a86f9Schristos }
149461a86f9Schristos
150461a86f9Schristos
151461a86f9Schristos int
compat_50_sys_clock_getres(struct lwp * l,const struct compat_50_sys_clock_getres_args * uap,register_t * retval)152461a86f9Schristos compat_50_sys_clock_getres(struct lwp *l,
153461a86f9Schristos const struct compat_50_sys_clock_getres_args *uap, register_t *retval)
154461a86f9Schristos {
155461a86f9Schristos /* {
156461a86f9Schristos syscallarg(clockid_t) clock_id;
157461a86f9Schristos syscallarg(struct timespec50 *) tp;
158461a86f9Schristos } */
159461a86f9Schristos struct timespec50 ats50;
1600876f873Snjoly struct timespec ats;
1616647020bSmaxv int error;
162461a86f9Schristos
1630876f873Snjoly error = clock_getres1(SCARG(uap, clock_id), &ats);
1640876f873Snjoly if (error != 0)
1650876f873Snjoly return error;
166461a86f9Schristos
1670876f873Snjoly if (SCARG(uap, tp)) {
1680876f873Snjoly timespec_to_timespec50(&ats, &ats50);
1690876f873Snjoly error = copyout(&ats50, SCARG(uap, tp), sizeof(ats50));
1700876f873Snjoly }
171461a86f9Schristos
172461a86f9Schristos return error;
173461a86f9Schristos }
174461a86f9Schristos
175461a86f9Schristos /* ARGSUSED */
176461a86f9Schristos int
compat_50_sys_nanosleep(struct lwp * l,const struct compat_50_sys_nanosleep_args * uap,register_t * retval)177461a86f9Schristos compat_50_sys_nanosleep(struct lwp *l,
178461a86f9Schristos const struct compat_50_sys_nanosleep_args *uap, register_t *retval)
179461a86f9Schristos {
180461a86f9Schristos /* {
181461a86f9Schristos syscallarg(struct timespec50 *) rqtp;
182461a86f9Schristos syscallarg(struct timespec50 *) rmtp;
183461a86f9Schristos } */
184461a86f9Schristos struct timespec rmt, rqt;
185461a86f9Schristos struct timespec50 rmt50, rqt50;
186461a86f9Schristos int error, error1;
187461a86f9Schristos
188461a86f9Schristos error = copyin(SCARG(uap, rqtp), &rqt50, sizeof(rqt50));
189461a86f9Schristos if (error)
190461a86f9Schristos return error;
191461a86f9Schristos timespec50_to_timespec(&rqt50, &rqt);
192461a86f9Schristos
1931ec74323Schristos error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
1941ec74323Schristos SCARG(uap, rmtp) ? &rmt : NULL);
195461a86f9Schristos if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
196461a86f9Schristos return error;
197461a86f9Schristos
198461a86f9Schristos timespec_to_timespec50(&rmt, &rmt50);
199461a86f9Schristos error1 = copyout(&rmt50, SCARG(uap, rmtp), sizeof(*SCARG(uap, rmtp)));
200461a86f9Schristos return error1 ? error1 : error;
201461a86f9Schristos }
202461a86f9Schristos
203461a86f9Schristos /* ARGSUSED */
204461a86f9Schristos int
compat_50_sys_gettimeofday(struct lwp * l,const struct compat_50_sys_gettimeofday_args * uap,register_t * retval)205461a86f9Schristos compat_50_sys_gettimeofday(struct lwp *l,
206461a86f9Schristos const struct compat_50_sys_gettimeofday_args *uap, register_t *retval)
207461a86f9Schristos {
208461a86f9Schristos /* {
209461a86f9Schristos syscallarg(struct timeval50 *) tp;
210461a86f9Schristos syscallarg(void *) tzp; really "struct timezone *";
211461a86f9Schristos } */
212461a86f9Schristos struct timeval atv;
213461a86f9Schristos struct timeval50 atv50;
214461a86f9Schristos int error = 0;
215461a86f9Schristos struct timezone tzfake;
216461a86f9Schristos
217461a86f9Schristos if (SCARG(uap, tp)) {
218461a86f9Schristos microtime(&atv);
219461a86f9Schristos timeval_to_timeval50(&atv, &atv50);
220461a86f9Schristos error = copyout(&atv50, SCARG(uap, tp), sizeof(*SCARG(uap, tp)));
221461a86f9Schristos if (error)
222461a86f9Schristos return error;
223461a86f9Schristos }
224461a86f9Schristos if (SCARG(uap, tzp)) {
225461a86f9Schristos /*
226461a86f9Schristos * NetBSD has no kernel notion of time zone, so we just
227461a86f9Schristos * fake up a timezone struct and return it if demanded.
228461a86f9Schristos */
22941aa5859Sriastradh memset(&tzfake, 0, sizeof(tzfake));
230461a86f9Schristos tzfake.tz_minuteswest = 0;
231461a86f9Schristos tzfake.tz_dsttime = 0;
232461a86f9Schristos error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
233461a86f9Schristos }
234461a86f9Schristos return error;
235461a86f9Schristos }
236461a86f9Schristos
237461a86f9Schristos /* ARGSUSED */
238461a86f9Schristos int
compat_50_sys_settimeofday(struct lwp * l,const struct compat_50_sys_settimeofday_args * uap,register_t * retval)239461a86f9Schristos compat_50_sys_settimeofday(struct lwp *l,
240461a86f9Schristos const struct compat_50_sys_settimeofday_args *uap, register_t *retval)
241461a86f9Schristos {
242461a86f9Schristos /* {
243461a86f9Schristos syscallarg(const struct timeval50 *) tv;
244461a86f9Schristos syscallarg(const void *) tzp; really "const struct timezone *";
245461a86f9Schristos } */
246461a86f9Schristos struct timeval50 atv50;
247461a86f9Schristos struct timeval atv;
248461a86f9Schristos int error = copyin(SCARG(uap, tv), &atv50, sizeof(atv50));
249461a86f9Schristos if (error)
250461a86f9Schristos return error;
251461a86f9Schristos timeval50_to_timeval(&atv50, &atv);
252461a86f9Schristos return settimeofday1(&atv, false, SCARG(uap, tzp), l, true);
253461a86f9Schristos }
254461a86f9Schristos
255461a86f9Schristos /* ARGSUSED */
256461a86f9Schristos int
compat_50_sys_adjtime(struct lwp * l,const struct compat_50_sys_adjtime_args * uap,register_t * retval)257461a86f9Schristos compat_50_sys_adjtime(struct lwp *l,
258461a86f9Schristos const struct compat_50_sys_adjtime_args *uap, register_t *retval)
259461a86f9Schristos {
260461a86f9Schristos /* {
261461a86f9Schristos syscallarg(const struct timeval50 *) delta;
262461a86f9Schristos syscallarg(struct timeval50 *) olddelta;
263461a86f9Schristos } */
264461a86f9Schristos int error;
265461a86f9Schristos struct timeval50 delta50, olddelta50;
266461a86f9Schristos struct timeval delta, olddelta;
267461a86f9Schristos
268461a86f9Schristos if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
269461a86f9Schristos KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
270461a86f9Schristos return error;
271461a86f9Schristos
272461a86f9Schristos if (SCARG(uap, delta)) {
273461a86f9Schristos error = copyin(SCARG(uap, delta), &delta50,
274461a86f9Schristos sizeof(*SCARG(uap, delta)));
275461a86f9Schristos if (error)
276461a86f9Schristos return (error);
277461a86f9Schristos timeval50_to_timeval(&delta50, &delta);
278461a86f9Schristos }
279461a86f9Schristos adjtime1(SCARG(uap, delta) ? &delta : NULL,
280461a86f9Schristos SCARG(uap, olddelta) ? &olddelta : NULL, l->l_proc);
281461a86f9Schristos if (SCARG(uap, olddelta)) {
282461a86f9Schristos timeval_to_timeval50(&olddelta, &olddelta50);
283461a86f9Schristos error = copyout(&olddelta50, SCARG(uap, olddelta),
284461a86f9Schristos sizeof(*SCARG(uap, olddelta)));
285461a86f9Schristos }
286461a86f9Schristos return error;
287461a86f9Schristos }
288461a86f9Schristos
289461a86f9Schristos /* BSD routine to set/arm an interval timer. */
290461a86f9Schristos /* ARGSUSED */
291461a86f9Schristos int
compat_50_sys_getitimer(struct lwp * l,const struct compat_50_sys_getitimer_args * uap,register_t * retval)292461a86f9Schristos compat_50_sys_getitimer(struct lwp *l,
293461a86f9Schristos const struct compat_50_sys_getitimer_args *uap, register_t *retval)
294461a86f9Schristos {
295461a86f9Schristos /* {
296461a86f9Schristos syscallarg(int) which;
297461a86f9Schristos syscallarg(struct itimerval50 *) itv;
298461a86f9Schristos } */
299461a86f9Schristos struct proc *p = l->l_proc;
300461a86f9Schristos struct itimerval aitv;
301461a86f9Schristos struct itimerval50 aitv50;
302461a86f9Schristos int error;
303461a86f9Schristos
304461a86f9Schristos error = dogetitimer(p, SCARG(uap, which), &aitv);
305461a86f9Schristos if (error)
306461a86f9Schristos return error;
307461a86f9Schristos itimerval_to_itimerval50(&aitv, &aitv50);
308461a86f9Schristos return copyout(&aitv50, SCARG(uap, itv), sizeof(*SCARG(uap, itv)));
309461a86f9Schristos }
310461a86f9Schristos
311461a86f9Schristos int
compat_50_sys_setitimer(struct lwp * l,const struct compat_50_sys_setitimer_args * uap,register_t * retval)312461a86f9Schristos compat_50_sys_setitimer(struct lwp *l,
313461a86f9Schristos const struct compat_50_sys_setitimer_args *uap, register_t *retval)
314461a86f9Schristos {
315461a86f9Schristos /* {
316461a86f9Schristos syscallarg(int) which;
317461a86f9Schristos syscallarg(const struct itimerval50 *) itv;
318461a86f9Schristos syscallarg(struct itimerval50 *) oitv;
319461a86f9Schristos } */
320461a86f9Schristos struct proc *p = l->l_proc;
321461a86f9Schristos int which = SCARG(uap, which);
322461a86f9Schristos struct compat_50_sys_getitimer_args getargs;
323461a86f9Schristos const struct itimerval50 *itvp;
324461a86f9Schristos struct itimerval50 aitv50;
325461a86f9Schristos struct itimerval aitv;
326461a86f9Schristos int error;
327461a86f9Schristos
328461a86f9Schristos itvp = SCARG(uap, itv);
329461a86f9Schristos if (itvp &&
3308cc2b02fSchristos (error = copyin(itvp, &aitv50, sizeof(aitv50))) != 0)
331461a86f9Schristos return (error);
332461a86f9Schristos itimerval50_to_itimerval(&aitv50, &aitv);
333461a86f9Schristos if (SCARG(uap, oitv) != NULL) {
334461a86f9Schristos SCARG(&getargs, which) = which;
335461a86f9Schristos SCARG(&getargs, itv) = SCARG(uap, oitv);
336461a86f9Schristos if ((error = compat_50_sys_getitimer(l, &getargs, retval)) != 0)
337461a86f9Schristos return (error);
338461a86f9Schristos }
339461a86f9Schristos if (itvp == 0)
340461a86f9Schristos return (0);
341461a86f9Schristos
342461a86f9Schristos return dosetitimer(p, which, &aitv);
343461a86f9Schristos }
344461a86f9Schristos
345461a86f9Schristos int
compat_50_sys_aio_suspend(struct lwp * l,const struct compat_50_sys_aio_suspend_args * uap,register_t * retval)346461a86f9Schristos compat_50_sys_aio_suspend(struct lwp *l,
347461a86f9Schristos const struct compat_50_sys_aio_suspend_args *uap, register_t *retval)
348461a86f9Schristos {
349461a86f9Schristos /* {
350461a86f9Schristos syscallarg(const struct aiocb *const[]) list;
351461a86f9Schristos syscallarg(int) nent;
352461a86f9Schristos syscallarg(const struct timespec50 *) timeout;
353461a86f9Schristos } */
354029f6c41Srmind #ifdef AIO
355461a86f9Schristos struct aiocb **list;
356461a86f9Schristos struct timespec ts;
357461a86f9Schristos struct timespec50 ts50;
358461a86f9Schristos int error, nent;
359461a86f9Schristos
360461a86f9Schristos nent = SCARG(uap, nent);
361461a86f9Schristos if (nent <= 0 || nent > aio_listio_max)
362461a86f9Schristos return EAGAIN;
363461a86f9Schristos
364461a86f9Schristos if (SCARG(uap, timeout)) {
365461a86f9Schristos /* Convert timespec to ticks */
366461a86f9Schristos error = copyin(SCARG(uap, timeout), &ts50,
367461a86f9Schristos sizeof(*SCARG(uap, timeout)));
368461a86f9Schristos if (error)
369461a86f9Schristos return error;
370461a86f9Schristos timespec50_to_timespec(&ts50, &ts);
371461a86f9Schristos }
372bb529e55Syamt list = kmem_alloc(nent * sizeof(*list), KM_SLEEP);
373bb529e55Syamt error = copyin(SCARG(uap, list), list, nent * sizeof(*list));
374461a86f9Schristos if (error)
375461a86f9Schristos goto out;
376461a86f9Schristos error = aio_suspend1(l, list, nent, SCARG(uap, timeout) ? &ts : NULL);
377461a86f9Schristos out:
378bb529e55Syamt kmem_free(list, nent * sizeof(*list));
379461a86f9Schristos return error;
380461a86f9Schristos #else
381461a86f9Schristos return ENOSYS;
382461a86f9Schristos #endif
383461a86f9Schristos }
384461a86f9Schristos
385461a86f9Schristos int
compat_50_sys_mq_timedsend(struct lwp * l,const struct compat_50_sys_mq_timedsend_args * uap,register_t * retval)386461a86f9Schristos compat_50_sys_mq_timedsend(struct lwp *l,
387461a86f9Schristos const struct compat_50_sys_mq_timedsend_args *uap, register_t *retval)
388461a86f9Schristos {
389461a86f9Schristos /* {
390461a86f9Schristos syscallarg(mqd_t) mqdes;
391461a86f9Schristos syscallarg(const char *) msg_ptr;
392461a86f9Schristos syscallarg(size_t) msg_len;
393461a86f9Schristos syscallarg(unsigned) msg_prio;
394461a86f9Schristos syscallarg(const struct timespec50 *) abs_timeout;
395461a86f9Schristos } */
396029f6c41Srmind #ifdef MQUEUE
397461a86f9Schristos struct timespec50 ts50;
398c9a5a18dSrmind struct timespec ts, *tsp;
399c9a5a18dSrmind int error;
400461a86f9Schristos
401461a86f9Schristos /* Get and convert time value */
402461a86f9Schristos if (SCARG(uap, abs_timeout)) {
403461a86f9Schristos error = copyin(SCARG(uap, abs_timeout), &ts50, sizeof(ts50));
404461a86f9Schristos if (error)
405461a86f9Schristos return error;
406461a86f9Schristos timespec50_to_timespec(&ts50, &ts);
407c9a5a18dSrmind tsp = &ts;
408c9a5a18dSrmind } else {
409c9a5a18dSrmind tsp = NULL;
410c9a5a18dSrmind }
411461a86f9Schristos
412c9a5a18dSrmind return mq_send1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
413c9a5a18dSrmind SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp);
414029f6c41Srmind #else
415029f6c41Srmind return ENOSYS;
416029f6c41Srmind #endif
417461a86f9Schristos }
418461a86f9Schristos
419461a86f9Schristos int
compat_50_sys_mq_timedreceive(struct lwp * l,const struct compat_50_sys_mq_timedreceive_args * uap,register_t * retval)420461a86f9Schristos compat_50_sys_mq_timedreceive(struct lwp *l,
421461a86f9Schristos const struct compat_50_sys_mq_timedreceive_args *uap, register_t *retval)
422461a86f9Schristos {
423461a86f9Schristos /* {
424461a86f9Schristos syscallarg(mqd_t) mqdes;
425461a86f9Schristos syscallarg(char *) msg_ptr;
426461a86f9Schristos syscallarg(size_t) msg_len;
427461a86f9Schristos syscallarg(unsigned *) msg_prio;
428461a86f9Schristos syscallarg(const struct timespec50 *) abs_timeout;
429461a86f9Schristos } */
430029f6c41Srmind #ifdef MQUEUE
431c9a5a18dSrmind struct timespec ts, *tsp;
432461a86f9Schristos struct timespec50 ts50;
433c9a5a18dSrmind ssize_t mlen;
434c9a5a18dSrmind int error;
435461a86f9Schristos
436461a86f9Schristos /* Get and convert time value */
437461a86f9Schristos if (SCARG(uap, abs_timeout)) {
438461a86f9Schristos error = copyin(SCARG(uap, abs_timeout), &ts50, sizeof(ts50));
439461a86f9Schristos if (error)
440461a86f9Schristos return error;
441461a86f9Schristos
442461a86f9Schristos timespec50_to_timespec(&ts50, &ts);
443c9a5a18dSrmind tsp = &ts;
444c9a5a18dSrmind } else {
445c9a5a18dSrmind tsp = NULL;
446c9a5a18dSrmind }
447461a86f9Schristos
448c9a5a18dSrmind error = mq_recv1(SCARG(uap, mqdes), SCARG(uap, msg_ptr),
449c9a5a18dSrmind SCARG(uap, msg_len), SCARG(uap, msg_prio), tsp, &mlen);
450461a86f9Schristos if (error == 0)
451461a86f9Schristos *retval = mlen;
452461a86f9Schristos
453461a86f9Schristos return error;
454029f6c41Srmind #else
455029f6c41Srmind return ENOSYS;
456029f6c41Srmind #endif
457461a86f9Schristos }
458461a86f9Schristos
459461a86f9Schristos int
compat_50_sys_getrusage(struct lwp * l,const struct compat_50_sys_getrusage_args * uap,register_t * retval)460461a86f9Schristos compat_50_sys_getrusage(struct lwp *l,
461461a86f9Schristos const struct compat_50_sys_getrusage_args *uap, register_t *retval)
462461a86f9Schristos {
463461a86f9Schristos /* {
464461a86f9Schristos syscallarg(int) who;
465461a86f9Schristos syscallarg(struct rusage50 *) rusage;
466461a86f9Schristos } */
4671ebbf7b6Snjoly int error;
468461a86f9Schristos struct rusage ru;
469461a86f9Schristos struct rusage50 ru50;
470461a86f9Schristos struct proc *p = l->l_proc;
471461a86f9Schristos
4721ebbf7b6Snjoly error = getrusage1(p, SCARG(uap, who), &ru);
4731ebbf7b6Snjoly if (error != 0)
4741ebbf7b6Snjoly return error;
475461a86f9Schristos
476461a86f9Schristos rusage_to_rusage50(&ru, &ru50);
477461a86f9Schristos return copyout(&ru50, SCARG(uap, rusage), sizeof(ru50));
478461a86f9Schristos }
479461a86f9Schristos
480461a86f9Schristos
481461a86f9Schristos /* Return the time remaining until a POSIX timer fires. */
482461a86f9Schristos int
compat_50_sys_timer_gettime(struct lwp * l,const struct compat_50_sys_timer_gettime_args * uap,register_t * retval)483461a86f9Schristos compat_50_sys_timer_gettime(struct lwp *l,
484461a86f9Schristos const struct compat_50_sys_timer_gettime_args *uap, register_t *retval)
485461a86f9Schristos {
486461a86f9Schristos /* {
487461a86f9Schristos syscallarg(timer_t) timerid;
488461a86f9Schristos syscallarg(struct itimerspec50 *) value;
489461a86f9Schristos } */
490461a86f9Schristos struct itimerspec its;
491461a86f9Schristos struct itimerspec50 its50;
492461a86f9Schristos int error;
493461a86f9Schristos
494461a86f9Schristos if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
495461a86f9Schristos &its)) != 0)
496461a86f9Schristos return error;
497461a86f9Schristos itimerspec_to_itimerspec50(&its, &its50);
498461a86f9Schristos
499461a86f9Schristos return copyout(&its50, SCARG(uap, value), sizeof(its50));
500461a86f9Schristos }
501461a86f9Schristos
502461a86f9Schristos /* Set and arm a POSIX realtime timer */
503461a86f9Schristos int
compat_50_sys_timer_settime(struct lwp * l,const struct compat_50_sys_timer_settime_args * uap,register_t * retval)504461a86f9Schristos compat_50_sys_timer_settime(struct lwp *l,
505461a86f9Schristos const struct compat_50_sys_timer_settime_args *uap, register_t *retval)
506461a86f9Schristos {
507461a86f9Schristos /* {
508461a86f9Schristos syscallarg(timer_t) timerid;
509461a86f9Schristos syscallarg(int) flags;
510461a86f9Schristos syscallarg(const struct itimerspec50 *) value;
511461a86f9Schristos syscallarg(struct itimerspec50 *) ovalue;
512461a86f9Schristos } */
513461a86f9Schristos int error;
514461a86f9Schristos struct itimerspec value, ovalue, *ovp = NULL;
515461a86f9Schristos struct itimerspec50 value50, ovalue50;
516461a86f9Schristos
517461a86f9Schristos if ((error = copyin(SCARG(uap, value), &value50, sizeof(value50))) != 0)
518461a86f9Schristos return error;
519461a86f9Schristos
520461a86f9Schristos itimerspec50_to_itimerspec(&value50, &value);
521461a86f9Schristos if (SCARG(uap, ovalue))
522461a86f9Schristos ovp = &ovalue;
523461a86f9Schristos
524461a86f9Schristos if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
525461a86f9Schristos SCARG(uap, flags), l->l_proc)) != 0)
526461a86f9Schristos return error;
527461a86f9Schristos
528461a86f9Schristos if (ovp) {
529461a86f9Schristos itimerspec_to_itimerspec50(&ovalue, &ovalue50);
530461a86f9Schristos return copyout(&ovalue50, SCARG(uap, ovalue), sizeof(ovalue50));
531461a86f9Schristos }
532461a86f9Schristos return 0;
533461a86f9Schristos }
534461a86f9Schristos
535461a86f9Schristos /*
536461a86f9Schristos * ntp_gettime() - NTP user application interface
537461a86f9Schristos */
538461a86f9Schristos int
compat_50_sys___ntp_gettime30(struct lwp * l,const struct compat_50_sys___ntp_gettime30_args * uap,register_t * retval)539461a86f9Schristos compat_50_sys___ntp_gettime30(struct lwp *l,
540461a86f9Schristos const struct compat_50_sys___ntp_gettime30_args *uap, register_t *retval)
541461a86f9Schristos {
542d91f98a8Spgoyette if (vec_ntp_gettime == NULL)
543d91f98a8Spgoyette return ENOSYS; /* No NTP available in kernel */
544d91f98a8Spgoyette
545461a86f9Schristos /* {
546461a86f9Schristos syscallarg(struct ntptimeval *) ntvp;
547461a86f9Schristos } */
548461a86f9Schristos struct ntptimeval ntv;
549461a86f9Schristos struct ntptimeval50 ntv50;
550461a86f9Schristos int error;
551461a86f9Schristos
552461a86f9Schristos if (SCARG(uap, ntvp)) {
553d91f98a8Spgoyette (*vec_ntp_gettime)(&ntv);
554cc8a8596Sriastradh memset(&ntv50, 0, sizeof(ntv50));
555461a86f9Schristos timespec_to_timespec50(&ntv.time, &ntv50.time);
556461a86f9Schristos ntv50.maxerror = ntv.maxerror;
557461a86f9Schristos ntv50.esterror = ntv.esterror;
558461a86f9Schristos ntv50.tai = ntv.tai;
559461a86f9Schristos ntv50.time_state = ntv.time_state;
560461a86f9Schristos
561461a86f9Schristos error = copyout(&ntv50, SCARG(uap, ntvp), sizeof(ntv50));
562461a86f9Schristos if (error)
563461a86f9Schristos return error;
564461a86f9Schristos }
565d91f98a8Spgoyette *retval = (*vec_ntp_timestatus)();
566461a86f9Schristos return 0;
567461a86f9Schristos }
568e8ac3e27Spgoyette
56972450a67Spgoyette SYSCTL_SETUP(compat_sysctl_time, "Old system boottime")
570e8ac3e27Spgoyette {
571e8ac3e27Spgoyette struct timeval tv;
572e8ac3e27Spgoyette
573d6c967bbSthorpej getmicroboottime(&tv);
574e8ac3e27Spgoyette timeval_to_timeval50(&tv, &boottime50);
575e8ac3e27Spgoyette
576e8ac3e27Spgoyette sysctl_createv(clog, 0, NULL, NULL,
577e8ac3e27Spgoyette CTLFLAG_PERMANENT,
578e8ac3e27Spgoyette CTLTYPE_STRUCT, "oboottime",
579e8ac3e27Spgoyette SYSCTL_DESCR("System boot time"),
580e8ac3e27Spgoyette NULL, 0, &boottime50, sizeof(boottime50),
581e8ac3e27Spgoyette CTL_KERN, KERN_OBOOTTIME, CTL_EOL);
582e8ac3e27Spgoyette }
583d91f98a8Spgoyette
584d91f98a8Spgoyette int
kern_time_50_init(void)585d91f98a8Spgoyette kern_time_50_init(void)
586d91f98a8Spgoyette {
587d91f98a8Spgoyette int error;
588d91f98a8Spgoyette
589d91f98a8Spgoyette error = syscall_establish(NULL, kern_time_50_syscalls);
590d91f98a8Spgoyette
591d91f98a8Spgoyette return error;
592d91f98a8Spgoyette }
593d91f98a8Spgoyette
594d91f98a8Spgoyette int
kern_time_50_fini(void)595d91f98a8Spgoyette kern_time_50_fini(void)
596d91f98a8Spgoyette {
597d91f98a8Spgoyette int error;
598d91f98a8Spgoyette
599d91f98a8Spgoyette error = syscall_disestablish(NULL, kern_time_50_syscalls);
600d91f98a8Spgoyette
601d91f98a8Spgoyette return error;
602d91f98a8Spgoyette }
603