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