1 /* $NetBSD: netbsd32_time.c,v 1.50 2018/10/30 14:43:38 riastradh 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.50 2018/10/30 14:43:38 riastradh 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 memset(&ntv32, 0, sizeof(ntv32)); 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 memset(&ntv32, 0, sizeof(ntv32)); 101 ntv32.time.tv_sec = (int32_t)ntv.time.tv_sec; 102 ntv32.time.tv_nsec = ntv.time.tv_nsec; 103 ntv32.maxerror = (netbsd32_long)ntv.maxerror; 104 ntv32.esterror = (netbsd32_long)ntv.esterror; 105 ntv32.tai = (netbsd32_long)ntv.tai; 106 ntv32.time_state = ntv.time_state; 107 error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32)); 108 } 109 if (!error) { 110 *retval = ntp_timestatus(); 111 } 112 113 return (error); 114 } 115 #endif 116 117 #ifdef COMPAT_30 118 int 119 compat_30_netbsd32_ntp_gettime(struct lwp *l, const struct compat_30_netbsd32_ntp_gettime_args *uap, register_t *retval) 120 { 121 /* { 122 syscallarg(netbsd32_ntptimevalp_t) ntvp; 123 } */ 124 struct netbsd32_ntptimeval30 ntv32; 125 struct ntptimeval ntv; 126 int error = 0; 127 128 if (SCARG_P32(uap, ntvp)) { 129 ntp_gettime(&ntv); 130 131 memset(&ntv32, 0, sizeof(ntv32)); 132 ntv32.time.tv_sec = ntv.time.tv_sec; 133 ntv32.time.tv_usec = ntv.time.tv_nsec / 1000; 134 ntv32.maxerror = (netbsd32_long)ntv.maxerror; 135 ntv32.esterror = (netbsd32_long)ntv.esterror; 136 error = copyout(&ntv32, SCARG_P32(uap, ntvp), sizeof(ntv32)); 137 } 138 if (!error) { 139 *retval = ntp_timestatus(); 140 } 141 142 return (error); 143 } 144 #endif 145 146 int 147 netbsd32_ntp_adjtime(struct lwp *l, const struct netbsd32_ntp_adjtime_args *uap, register_t *retval) 148 { 149 /* { 150 syscallarg(netbsd32_timexp_t) tp; 151 } */ 152 struct netbsd32_timex ntv32; 153 struct timex ntv; 154 int error = 0; 155 int modes; 156 157 if ((error = copyin(SCARG_P32(uap, tp), &ntv32, sizeof(ntv32)))) 158 return (error); 159 160 netbsd32_to_timex(&ntv32, &ntv); 161 162 /* 163 * Update selected clock variables - only the superuser can 164 * change anything. Note that there is no error checking here on 165 * the assumption the superuser should know what it is doing. 166 */ 167 modes = ntv.modes; 168 if (modes != 0 && (error = kauth_authorize_system(l->l_cred, 169 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_NTPADJTIME, NULL, NULL, 170 NULL))) 171 return (error); 172 173 ntp_adjtime1(&ntv); 174 175 netbsd32_from_timex(&ntv, &ntv32); 176 error = copyout(&ntv32, SCARG_P32(uap, tp), sizeof(ntv32)); 177 if (!error) { 178 *retval = ntp_timestatus(); 179 } 180 return error; 181 } 182 #endif /* NTP */ 183 184 int 185 netbsd32___setitimer50(struct lwp *l, const struct netbsd32___setitimer50_args *uap, register_t *retval) 186 { 187 /* { 188 syscallarg(int) which; 189 syscallarg(const netbsd32_itimervalp_t) itv; 190 syscallarg(netbsd32_itimervalp_t) oitv; 191 } */ 192 struct proc *p = l->l_proc; 193 struct netbsd32_itimerval s32it, *itv32; 194 int which = SCARG(uap, which); 195 struct netbsd32___getitimer50_args getargs; 196 struct itimerval aitv; 197 int error; 198 199 if ((u_int)which > ITIMER_PROF) 200 return (EINVAL); 201 itv32 = SCARG_P32(uap, itv); 202 if (itv32) { 203 if ((error = copyin(itv32, &s32it, sizeof(s32it)))) 204 return (error); 205 netbsd32_to_itimerval(&s32it, &aitv); 206 } 207 if (SCARG_P32(uap, oitv) != 0) { 208 SCARG(&getargs, which) = which; 209 SCARG(&getargs, itv) = SCARG(uap, oitv); 210 if ((error = netbsd32___getitimer50(l, &getargs, retval)) != 0) 211 return (error); 212 } 213 if (itv32 == 0) 214 return 0; 215 216 return dosetitimer(p, which, &aitv); 217 } 218 219 int 220 netbsd32___getitimer50(struct lwp *l, const struct netbsd32___getitimer50_args *uap, register_t *retval) 221 { 222 /* { 223 syscallarg(int) which; 224 syscallarg(netbsd32_itimervalp_t) itv; 225 } */ 226 struct proc *p = l->l_proc; 227 struct netbsd32_itimerval s32it; 228 struct itimerval aitv; 229 int error; 230 231 error = dogetitimer(p, SCARG(uap, which), &aitv); 232 if (error) 233 return error; 234 235 netbsd32_from_itimerval(&aitv, &s32it); 236 return copyout(&s32it, SCARG_P32(uap, itv), sizeof(s32it)); 237 } 238 239 int 240 netbsd32___gettimeofday50(struct lwp *l, const struct netbsd32___gettimeofday50_args *uap, register_t *retval) 241 { 242 /* { 243 syscallarg(netbsd32_timevalp_t) tp; 244 syscallarg(netbsd32_timezonep_t) tzp; 245 } */ 246 struct timeval atv; 247 struct netbsd32_timeval tv32; 248 int error = 0; 249 struct netbsd32_timezone tzfake; 250 251 if (SCARG_P32(uap, tp)) { 252 microtime(&atv); 253 netbsd32_from_timeval(&atv, &tv32); 254 error = copyout(&tv32, SCARG_P32(uap, tp), sizeof(tv32)); 255 if (error) 256 return (error); 257 } 258 if (SCARG_P32(uap, tzp)) { 259 /* 260 * NetBSD has no kernel notion of time zone, so we just 261 * fake up a timezone struct and return it if demanded. 262 */ 263 tzfake.tz_minuteswest = 0; 264 tzfake.tz_dsttime = 0; 265 error = copyout(&tzfake, SCARG_P32(uap, tzp), sizeof(tzfake)); 266 } 267 return (error); 268 } 269 270 int 271 netbsd32___settimeofday50(struct lwp *l, const struct netbsd32___settimeofday50_args *uap, register_t *retval) 272 { 273 /* { 274 syscallarg(const netbsd32_timevalp_t) tv; 275 syscallarg(const netbsd32_timezonep_t) tzp; 276 } */ 277 struct netbsd32_timeval atv32; 278 struct timeval atv; 279 struct timespec ats; 280 int error; 281 struct proc *p = l->l_proc; 282 283 /* Verify all parameters before changing time. */ 284 285 /* 286 * NetBSD has no kernel notion of time zone, and only an 287 * obsolete program would try to set it, so we log a warning. 288 */ 289 if (SCARG_P32(uap, tzp)) 290 printf("pid %d attempted to set the " 291 "(obsolete) kernel time zone\n", p->p_pid); 292 293 if (SCARG_P32(uap, tv) == 0) 294 return 0; 295 296 if ((error = copyin(SCARG_P32(uap, tv), &atv32, sizeof(atv32))) != 0) 297 return error; 298 299 netbsd32_to_timeval(&atv32, &atv); 300 TIMEVAL_TO_TIMESPEC(&atv, &ats); 301 return settime(p, &ats); 302 } 303 304 int 305 netbsd32___adjtime50(struct lwp *l, const struct netbsd32___adjtime50_args *uap, register_t *retval) 306 { 307 /* { 308 syscallarg(const netbsd32_timevalp_t) delta; 309 syscallarg(netbsd32_timevalp_t) olddelta; 310 } */ 311 struct netbsd32_timeval atv; 312 int error; 313 314 extern int time_adjusted; /* in kern_ntptime.c */ 315 extern int64_t time_adjtime; /* in kern_ntptime.c */ 316 317 if ((error = kauth_authorize_system(l->l_cred, 318 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, 319 NULL)) != 0) 320 return (error); 321 322 if (SCARG_P32(uap, olddelta)) { 323 atv.tv_sec = time_adjtime / 1000000; 324 atv.tv_usec = time_adjtime % 1000000; 325 if (atv.tv_usec < 0) { 326 atv.tv_usec += 1000000; 327 atv.tv_sec--; 328 } 329 error = copyout(&atv, SCARG_P32(uap, olddelta), sizeof(atv)); 330 if (error) 331 return (error); 332 } 333 334 if (SCARG_P32(uap, delta)) { 335 error = copyin(SCARG_P32(uap, delta), &atv, sizeof(atv)); 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, CLOCK_MONOTONIC, 0, &rqt, 426 SCARG_P32(uap, rmtp) ? &rmt : NULL); 427 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 428 return error; 429 430 netbsd32_from_timespec(&rmt, &ts32); 431 error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32)); 432 return error1 ? error1 : error; 433 } 434 435 int 436 netbsd32_clock_nanosleep(struct lwp *l, const struct netbsd32_clock_nanosleep_args *uap, register_t *retval) 437 { 438 /* { 439 syscallarg(clockid_t) clock_id; 440 syscallarg(int) flags; 441 syscallarg(const netbsd32_timespecp_t) rqtp; 442 syscallarg(netbsd32_timespecp_t) rmtp; 443 } */ 444 struct netbsd32_timespec ts32; 445 struct timespec rqt, rmt; 446 int error, error1; 447 448 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32)); 449 if (error) 450 goto out; 451 netbsd32_to_timespec(&ts32, &rqt); 452 453 error = nanosleep1(l, SCARG(uap, clock_id), SCARG(uap, flags), 454 &rqt, SCARG_P32(uap, rmtp) ? &rmt : NULL); 455 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 456 goto out; 457 458 netbsd32_from_timespec(&rmt, &ts32); 459 if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0 && 460 (error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32))) != 0) 461 error = error1; 462 out: 463 *retval = error; 464 return 0; 465 } 466 467 static int 468 netbsd32_timer_create_fetch(const void *src, void *dst, size_t size) 469 { 470 struct sigevent *evp = dst; 471 struct netbsd32_sigevent ev32; 472 int error; 473 474 error = copyin(src, &ev32, sizeof(ev32)); 475 if (error) 476 return error; 477 478 netbsd32_to_sigevent(&ev32, evp); 479 return 0; 480 } 481 482 int 483 netbsd32_timer_create(struct lwp *l, const struct netbsd32_timer_create_args *uap, register_t *retval) 484 { 485 /* { 486 syscallarg(netbsd32_clockid_t) clock_id; 487 syscallarg(netbsd32_sigeventp_t) evp; 488 syscallarg(netbsd32_timerp_t) timerid; 489 } */ 490 491 return timer_create1(SCARG_P32(uap, timerid), 492 SCARG(uap, clock_id), SCARG_P32(uap, evp), 493 netbsd32_timer_create_fetch, l); 494 } 495 496 int 497 netbsd32_timer_delete(struct lwp *l, const struct netbsd32_timer_delete_args *uap, register_t *retval) 498 { 499 /* { 500 syscallarg(netbsd32_timer_t) timerid; 501 } */ 502 struct sys_timer_delete_args ua; 503 504 NETBSD32TO64_UAP(timerid); 505 return sys_timer_delete(l, (void *)&ua, retval); 506 } 507 508 int 509 netbsd32___timer_settime50(struct lwp *l, const struct netbsd32___timer_settime50_args *uap, register_t *retval) 510 { 511 /* { 512 syscallarg(netbsd32_timer_t) timerid; 513 syscallarg(int) flags; 514 syscallarg(const netbsd32_itimerspecp_t) value; 515 syscallarg(netbsd32_itimerspecp_t) ovalue; 516 } */ 517 int error; 518 struct itimerspec value, ovalue, *ovp = NULL; 519 struct netbsd32_itimerspec its32; 520 521 if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0) 522 return (error); 523 netbsd32_to_timespec(&its32.it_interval, &value.it_interval); 524 netbsd32_to_timespec(&its32.it_value, &value.it_value); 525 526 if (SCARG_P32(uap, ovalue)) 527 ovp = &ovalue; 528 529 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp, 530 SCARG(uap, flags), l->l_proc)) != 0) 531 return error; 532 533 if (ovp) { 534 netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval); 535 netbsd32_from_timespec(&ovp->it_value, &its32.it_value); 536 return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32)); 537 } 538 return 0; 539 } 540 541 int 542 netbsd32___timer_gettime50(struct lwp *l, const struct netbsd32___timer_gettime50_args *uap, register_t *retval) 543 { 544 /* { 545 syscallarg(netbsd32_timer_t) timerid; 546 syscallarg(netbsd32_itimerspecp_t) value; 547 } */ 548 int error; 549 struct itimerspec its; 550 struct netbsd32_itimerspec its32; 551 552 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc, 553 &its)) != 0) 554 return error; 555 556 netbsd32_from_timespec(&its.it_interval, &its32.it_interval); 557 netbsd32_from_timespec(&its.it_value, &its32.it_value); 558 559 return copyout(&its32, SCARG_P32(uap, value), sizeof(its32)); 560 } 561 562 int 563 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval) 564 { 565 /* { 566 syscallarg(netbsd32_timer_t) timerid; 567 } */ 568 struct sys_timer_getoverrun_args ua; 569 570 NETBSD32TO64_UAP(timerid); 571 return sys_timer_getoverrun(l, (void *)&ua, retval); 572 } 573 574 int 575 netbsd32_clock_getcpuclockid2(struct lwp *l, 576 const struct netbsd32_clock_getcpuclockid2_args *uap, 577 register_t *retval) 578 { 579 /* { 580 syscallarg(idtype_t) idtype; 581 syscallarg(id_t) id; 582 syscallarg(netbsd32_clockidp_t) clock_id; 583 } */ 584 pid_t pid; 585 lwpid_t lid; 586 clockid_t clock_id; 587 id_t id = SCARG(uap, id); 588 589 switch (SCARG(uap, idtype)) { 590 case P_PID: 591 pid = id == 0 ? l->l_proc->p_pid : id; 592 clock_id = CLOCK_PROCESS_CPUTIME_ID | pid; 593 break; 594 case P_LWPID: 595 lid = id == 0 ? l->l_lid : id; 596 clock_id = CLOCK_THREAD_CPUTIME_ID | lid; 597 break; 598 default: 599 return EINVAL; 600 } 601 return copyout(&clock_id, SCARG_P32(uap, clock_id), sizeof(clock_id)); 602 } 603 604