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