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