1 /* $NetBSD: netbsd32_time.c,v 1.32 2008/01/20 18:09:10 joerg 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 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_time.c,v 1.32 2008/01/20 18:09:10 joerg Exp $"); 33 34 #if defined(_KERNEL_OPT) 35 #include "opt_ntp.h" 36 #include "opt_compat_netbsd.h" 37 #endif 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/mount.h> 42 #include <sys/time.h> 43 #include <sys/timex.h> 44 #include <sys/timevar.h> 45 #include <sys/timetc.h> 46 #include <sys/proc.h> 47 #include <sys/pool.h> 48 #include <sys/resourcevar.h> 49 #include <sys/dirent.h> 50 #include <sys/kauth.h> 51 52 #include <compat/netbsd32/netbsd32.h> 53 #include <compat/netbsd32/netbsd32_syscallargs.h> 54 #include <compat/netbsd32/netbsd32_conv.h> 55 56 #ifdef NTP 57 58 int 59 netbsd32_ntp_gettime(struct lwp *l, const struct netbsd32_ntp_gettime_args *uap, register_t *retval) 60 { 61 /* { 62 syscallarg(netbsd32_ntptimevalp_t) ntvp; 63 } */ 64 struct netbsd32_ntptimeval ntv32; 65 struct ntptimeval ntv; 66 int error = 0; 67 68 if (SCARG_P32(uap, ntvp)) { 69 ntp_gettime(&ntv); 70 71 ntv32.time.tv_sec = ntv.time.tv_sec; 72 ntv32.time.tv_nsec = ntv.time.tv_nsec; 73 ntv32.maxerror = (netbsd32_long)ntv.maxerror; 74 ntv32.esterror = (netbsd32_long)ntv.esterror; 75 ntv32.tai = (netbsd32_long)ntv.tai; 76 ntv32.time_state = ntv.time_state; 77 error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32)); 78 } 79 if (!error) { 80 *retval = ntp_timestatus(); 81 } 82 83 return (error); 84 } 85 86 #ifdef COMPAT_30 87 int 88 compat_30_netbsd32_ntp_gettime(struct lwp *l, const struct compat_30_netbsd32_ntp_gettime_args *uap, register_t *retval) 89 { 90 /* { 91 syscallarg(netbsd32_ntptimevalp_t) ntvp; 92 } */ 93 struct netbsd32_ntptimeval30 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 = ntv.time.tv_sec; 101 ntv32.time.tv_usec = ntv.time.tv_nsec / 1000; 102 ntv32.maxerror = (netbsd32_long)ntv.maxerror; 103 ntv32.esterror = (netbsd32_long)ntv.esterror; 104 error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32)); 105 } 106 if (!error) { 107 *retval = ntp_timestatus(); 108 } 109 110 return (error); 111 } 112 #endif 113 114 int 115 netbsd32_ntp_adjtime(struct lwp *l, const struct netbsd32_ntp_adjtime_args *uap, register_t *retval) 116 { 117 /* { 118 syscallarg(netbsd32_timexp_t) tp; 119 } */ 120 struct netbsd32_timex ntv32; 121 struct timex ntv; 122 int error = 0; 123 int modes; 124 125 if ((error = copyin(SCARG_P32(uap, tp), &ntv32, sizeof(ntv32)))) 126 return (error); 127 128 netbsd32_to_timex(&ntv32, &ntv); 129 130 /* 131 * Update selected clock variables - only the superuser can 132 * change anything. Note that there is no error checking here on 133 * the assumption the superuser should know what it is doing. 134 */ 135 modes = ntv.modes; 136 if (modes != 0 && (error = kauth_authorize_system(l->l_cred, 137 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_NTPADJTIME, NULL, NULL, 138 NULL))) 139 return (error); 140 141 ntp_adjtime1(&ntv); 142 143 netbsd32_from_timex(&ntv, &ntv32); 144 error = copyout(&ntv32, SCARG_P32(uap, tp), sizeof(ntv32)); 145 if (!error) { 146 *retval = ntp_timestatus(); 147 } 148 return error; 149 } 150 #else /* !NTP */ 151 int 152 netbsd32_ntp_gettime(struct lwp *l, const struct netbsd32_ntp_gettime_args *uap, register_t *retval) 153 { 154 155 return (ENOSYS); 156 } 157 158 #ifdef COMPAT_30 159 int 160 compat_30_netbsd32_ntp_gettime(struct lwp *l, const struct compat_30_netbsd32_ntp_gettime_args *uap, register_t *retval) 161 { 162 163 return (ENOSYS); 164 } 165 #endif 166 167 int 168 netbsd32_ntp_adjtime(struct lwp *l, const struct netbsd32_ntp_adjtime_args *uap, register_t *retval) 169 { 170 171 return (ENOSYS); 172 } 173 #endif /* NTP */ 174 175 int 176 netbsd32_setitimer(struct lwp *l, const struct netbsd32_setitimer_args *uap, register_t *retval) 177 { 178 /* { 179 syscallarg(int) which; 180 syscallarg(const netbsd32_itimervalp_t) itv; 181 syscallarg(netbsd32_itimervalp_t) oitv; 182 } */ 183 struct proc *p = l->l_proc; 184 struct netbsd32_itimerval s32it, *itv32; 185 int which = SCARG(uap, which); 186 struct netbsd32_getitimer_args getargs; 187 struct itimerval aitv; 188 int error; 189 190 if ((u_int)which > ITIMER_PROF) 191 return (EINVAL); 192 itv32 = SCARG_P32(uap, itv); 193 if (itv32) { 194 if ((error = copyin(itv32, &s32it, sizeof(s32it)))) 195 return (error); 196 netbsd32_to_itimerval(&s32it, &aitv); 197 } 198 if (SCARG_P32(uap, oitv) != 0) { 199 SCARG(&getargs, which) = which; 200 SCARG(&getargs, itv) = SCARG(uap, oitv); 201 if ((error = netbsd32_getitimer(l, &getargs, retval)) != 0) 202 return (error); 203 } 204 if (itv32 == 0) 205 return 0; 206 207 return dosetitimer(p, which, &aitv); 208 } 209 210 int 211 netbsd32_getitimer(struct lwp *l, const struct netbsd32_getitimer_args *uap, register_t *retval) 212 { 213 /* { 214 syscallarg(int) which; 215 syscallarg(netbsd32_itimervalp_t) itv; 216 } */ 217 struct proc *p = l->l_proc; 218 struct netbsd32_itimerval s32it; 219 struct itimerval aitv; 220 int error; 221 222 error = dogetitimer(p, SCARG(uap, which), &aitv); 223 if (error) 224 return error; 225 226 netbsd32_from_itimerval(&aitv, &s32it); 227 return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it)); 228 } 229 230 int 231 netbsd32_gettimeofday(struct lwp *l, const struct netbsd32_gettimeofday_args *uap, register_t *retval) 232 { 233 /* { 234 syscallarg(netbsd32_timevalp_t) tp; 235 syscallarg(netbsd32_timezonep_t) tzp; 236 } */ 237 struct timeval atv; 238 struct netbsd32_timeval tv32; 239 int error = 0; 240 struct netbsd32_timezone tzfake; 241 242 if (SCARG_P32(uap, tp)) { 243 microtime(&atv); 244 netbsd32_from_timeval(&atv, &tv32); 245 error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32)); 246 if (error) 247 return (error); 248 } 249 if (SCARG_P32(uap, tzp)) { 250 /* 251 * NetBSD has no kernel notion of time zone, so we just 252 * fake up a timezone struct and return it if demanded. 253 */ 254 tzfake.tz_minuteswest = 0; 255 tzfake.tz_dsttime = 0; 256 error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake)); 257 } 258 return (error); 259 } 260 261 int 262 netbsd32_settimeofday(struct lwp *l, const struct netbsd32_settimeofday_args *uap, register_t *retval) 263 { 264 /* { 265 syscallarg(const netbsd32_timevalp_t) tv; 266 syscallarg(const netbsd32_timezonep_t) tzp; 267 } */ 268 struct netbsd32_timeval atv32; 269 struct timeval atv; 270 struct timespec ats; 271 int error; 272 struct proc *p = l->l_proc; 273 274 /* Verify all parameters before changing time. */ 275 276 /* 277 * NetBSD has no kernel notion of time zone, and only an 278 * obsolete program would try to set it, so we log a warning. 279 */ 280 if (SCARG_P32(uap, tzp)) 281 printf("pid %d attempted to set the " 282 "(obsolete) kernel time zone\n", p->p_pid); 283 284 if (SCARG_P32(uap, tv) == 0) 285 return 0; 286 287 if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0) 288 return error; 289 290 netbsd32_to_timeval(&atv32, &atv); 291 TIMEVAL_TO_TIMESPEC(&atv, &ats); 292 return settime(p, &ats); 293 } 294 295 int 296 netbsd32_adjtime(struct lwp *l, const struct netbsd32_adjtime_args *uap, register_t *retval) 297 { 298 /* { 299 syscallarg(const netbsd32_timevalp_t) delta; 300 syscallarg(netbsd32_timevalp_t) olddelta; 301 } */ 302 struct netbsd32_timeval atv; 303 int error; 304 305 extern int time_adjusted; /* in kern_ntptime.c */ 306 extern int64_t time_adjtime; /* in kern_ntptime.c */ 307 308 if ((error = kauth_authorize_system(l->l_cred, 309 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, 310 NULL)) != 0) 311 return (error); 312 313 if (SCARG_P32(uap, olddelta)) { 314 atv.tv_sec = time_adjtime / 1000000; 315 atv.tv_usec = time_adjtime % 1000000; 316 if (atv.tv_usec < 0) { 317 atv.tv_usec += 1000000; 318 atv.tv_sec--; 319 } 320 (void) copyout(&atv, 321 SCARG_P32(uap, olddelta), 322 sizeof(atv)); 323 if (error) 324 return (error); 325 } 326 327 if (SCARG_P32(uap, delta)) { 328 error = copyin(SCARG_P32(uap, delta), &atv, 329 sizeof(struct timeval)); 330 if (error) 331 return (error); 332 333 time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec; 334 335 if (time_adjtime) 336 /* We need to save the system time during shutdown */ 337 time_adjusted |= 1; 338 } 339 340 return (0); 341 } 342 343 int 344 netbsd32_clock_gettime(struct lwp *l, const struct netbsd32_clock_gettime_args *uap, register_t *retval) 345 { 346 /* { 347 syscallarg(netbsd32_clockid_t) clock_id; 348 syscallarg(netbsd32_timespecp_t) tp; 349 } */ 350 clockid_t clock_id; 351 struct timespec ats; 352 struct netbsd32_timespec ts32; 353 354 clock_id = SCARG(uap, clock_id); 355 if (clock_id != CLOCK_REALTIME) 356 return (EINVAL); 357 358 nanotime(&ats); 359 netbsd32_from_timespec(&ats, &ts32); 360 361 return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32)); 362 } 363 364 int 365 netbsd32_clock_settime(struct lwp *l, const struct netbsd32_clock_settime_args *uap, register_t *retval) 366 { 367 /* { 368 syscallarg(netbsd32_clockid_t) clock_id; 369 syscallarg(const netbsd32_timespecp_t) tp; 370 } */ 371 struct netbsd32_timespec ts32; 372 clockid_t clock_id; 373 struct timespec ats; 374 int error; 375 376 clock_id = SCARG(uap, clock_id); 377 if (clock_id != CLOCK_REALTIME) 378 return (EINVAL); 379 380 if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0) 381 return (error); 382 383 netbsd32_to_timespec(&ts32, &ats); 384 return settime(l->l_proc, &ats); 385 } 386 387 int 388 netbsd32_clock_getres(struct lwp *l, const struct netbsd32_clock_getres_args *uap, register_t *retval) 389 { 390 /* { 391 syscallarg(netbsd32_clockid_t) clock_id; 392 syscallarg(netbsd32_timespecp_t) tp; 393 } */ 394 struct netbsd32_timespec ts32; 395 clockid_t clock_id; 396 struct timespec ts; 397 int error = 0; 398 399 clock_id = SCARG(uap, clock_id); 400 if (clock_id != CLOCK_REALTIME) 401 return (EINVAL); 402 403 if (SCARG_P32(uap, tp)) { 404 ts.tv_sec = 0; 405 ts.tv_nsec = 1000000000 / hz; 406 407 netbsd32_from_timespec(&ts, &ts32); 408 error = copyout(&ts, SCARG_P32(uap, tp), sizeof(ts)); 409 } 410 411 return error; 412 } 413 414 int 415 netbsd32_nanosleep(struct lwp *l, const struct netbsd32_nanosleep_args *uap, register_t *retval) 416 { 417 /* { 418 syscallarg(const netbsd32_timespecp_t) rqtp; 419 syscallarg(netbsd32_timespecp_t) rmtp; 420 } */ 421 static int nanowait; 422 struct netbsd32_timespec ts32; 423 struct timespec rqt; 424 struct timespec rmt; 425 struct timeval atv, utv, ctime; 426 int error, timo; 427 428 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32)); 429 if (error) 430 return (error); 431 432 netbsd32_to_timespec(&ts32, &rqt); 433 TIMESPEC_TO_TIMEVAL(&atv,&rqt); 434 if (itimerfix(&atv)) 435 return (EINVAL); 436 437 getmicrotime(&ctime); 438 timeradd(&atv,&ctime,&atv); 439 timo = hzto(&atv); 440 /* 441 * Avoid inadvertantly sleeping forever 442 */ 443 if (timo == 0) 444 timo = 1; 445 446 error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo); 447 if (error == ERESTART) 448 error = EINTR; 449 if (error == EWOULDBLOCK) 450 error = 0; 451 452 if (SCARG_P32(uap, rmtp)) { 453 int error1; 454 455 getmicrotime(&utv); 456 457 timersub(&atv, &utv, &utv); 458 if (utv.tv_sec < 0) 459 timerclear(&utv); 460 461 TIMEVAL_TO_TIMESPEC(&utv,&rmt); 462 netbsd32_from_timespec(&rmt, &ts32); 463 error1 = copyout(&ts32, SCARG_P32(uap,rmtp), sizeof(ts32)); 464 if (error1) 465 return (error1); 466 } 467 468 return error; 469 } 470 471 static int 472 netbsd32_timer_create_fetch(const void *src, void *dst, size_t size) 473 { 474 struct sigevent *evp = dst; 475 struct netbsd32_sigevent ev32; 476 int error; 477 478 error = copyin(src, &ev32, sizeof(ev32)); 479 if (error) 480 return error; 481 482 netbsd32_to_sigevent(&ev32, evp); 483 return 0; 484 } 485 486 int 487 netbsd32_timer_create(struct lwp *l, const struct netbsd32_timer_create_args *uap, register_t *retval) 488 { 489 /* { 490 syscallarg(netbsd32_clockid_t) clock_id; 491 syscallarg(netbsd32_sigeventp_t) evp; 492 syscallarg(netbsd32_timerp_t) timerid; 493 } */ 494 495 return timer_create1(SCARG_P32(uap, timerid), 496 SCARG(uap, clock_id), SCARG_P32(uap, evp), 497 netbsd32_timer_create_fetch, l); 498 } 499 500 int 501 netbsd32_timer_delete(struct lwp *l, const struct netbsd32_timer_delete_args *uap, register_t *retval) 502 { 503 /* { 504 syscallarg(netbsd32_timer_t) timerid; 505 } */ 506 struct sys_timer_delete_args ua; 507 508 NETBSD32TO64_UAP(timerid); 509 return sys_timer_delete(l, (void *)&ua, retval); 510 } 511 512 int 513 netbsd32_timer_settime(struct lwp *l, const struct netbsd32_timer_settime_args *uap, register_t *retval) 514 { 515 /* { 516 syscallarg(netbsd32_timer_t) timerid; 517 syscallarg(int) flags; 518 syscallarg(const netbsd32_itimerspecp_t) value; 519 syscallarg(netbsd32_itimerspecp_t) ovalue; 520 } */ 521 int error; 522 struct itimerspec value, ovalue, *ovp = NULL; 523 struct netbsd32_itimerspec its32; 524 525 if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0) 526 return (error); 527 netbsd32_to_timespec(&its32.it_interval, &value.it_interval); 528 netbsd32_to_timespec(&its32.it_value, &value.it_value); 529 530 if (SCARG_P32(uap, ovalue)) 531 ovp = &ovalue; 532 533 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp, 534 SCARG(uap, flags), l->l_proc)) != 0) 535 return error; 536 537 if (ovp) { 538 netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval); 539 netbsd32_from_timespec(&ovp->it_value, &its32.it_value); 540 return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32)); 541 } 542 return 0; 543 } 544 545 int 546 netbsd32_timer_gettime(struct lwp *l, const struct netbsd32_timer_gettime_args *uap, register_t *retval) 547 { 548 /* { 549 syscallarg(netbsd32_timer_t) timerid; 550 syscallarg(netbsd32_itimerspecp_t) value; 551 } */ 552 int error; 553 struct itimerspec its; 554 struct netbsd32_itimerspec its32; 555 556 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc, 557 &its)) != 0) 558 return error; 559 560 netbsd32_from_timespec(&its.it_interval, &its32.it_interval); 561 netbsd32_from_timespec(&its.it_value, &its32.it_value); 562 563 return copyout(&its32, SCARG_P32(uap, value), sizeof(its32)); 564 } 565 566 int 567 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval) 568 { 569 /* { 570 syscallarg(netbsd32_timer_t) timerid; 571 } */ 572 struct sys_timer_getoverrun_args ua; 573 574 NETBSD32TO64_UAP(timerid); 575 return sys_timer_getoverrun(l, (void *)&ua, retval); 576 } 577