1 /* $NetBSD: netbsd32_time.c,v 1.51 2019/01/27 02:08:40 pgoyette 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.51 2019/01/27 02:08:40 pgoyette 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 TIMEVAL_TO_TIMESPEC(&atv, &ats); 247 return settime(p, &ats); 248 } 249 250 int 251 netbsd32___adjtime50(struct lwp *l, const struct netbsd32___adjtime50_args *uap, register_t *retval) 252 { 253 /* { 254 syscallarg(const netbsd32_timevalp_t) delta; 255 syscallarg(netbsd32_timevalp_t) olddelta; 256 } */ 257 struct netbsd32_timeval atv; 258 int error; 259 260 extern int time_adjusted; /* in kern_ntptime.c */ 261 extern int64_t time_adjtime; /* in kern_ntptime.c */ 262 263 if ((error = kauth_authorize_system(l->l_cred, 264 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, 265 NULL)) != 0) 266 return (error); 267 268 if (SCARG_P32(uap, olddelta)) { 269 atv.tv_sec = time_adjtime / 1000000; 270 atv.tv_usec = time_adjtime % 1000000; 271 if (atv.tv_usec < 0) { 272 atv.tv_usec += 1000000; 273 atv.tv_sec--; 274 } 275 error = copyout(&atv, SCARG_P32(uap, olddelta), sizeof(atv)); 276 if (error) 277 return (error); 278 } 279 280 if (SCARG_P32(uap, delta)) { 281 error = copyin(SCARG_P32(uap, delta), &atv, sizeof(atv)); 282 if (error) 283 return (error); 284 285 time_adjtime = (int64_t)atv.tv_sec * 1000000 + atv.tv_usec; 286 287 if (time_adjtime) 288 /* We need to save the system time during shutdown */ 289 time_adjusted |= 1; 290 } 291 292 return (0); 293 } 294 295 int 296 netbsd32___clock_gettime50(struct lwp *l, const struct netbsd32___clock_gettime50_args *uap, register_t *retval) 297 { 298 /* { 299 syscallarg(netbsd32_clockid_t) clock_id; 300 syscallarg(netbsd32_timespecp_t) tp; 301 } */ 302 int error; 303 struct timespec ats; 304 struct netbsd32_timespec ts32; 305 306 error = clock_gettime1(SCARG(uap, clock_id), &ats); 307 if (error != 0) 308 return error; 309 310 netbsd32_from_timespec(&ats, &ts32); 311 return copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32)); 312 } 313 314 int 315 netbsd32___clock_settime50(struct lwp *l, const struct netbsd32___clock_settime50_args *uap, register_t *retval) 316 { 317 /* { 318 syscallarg(netbsd32_clockid_t) clock_id; 319 syscallarg(const netbsd32_timespecp_t) tp; 320 } */ 321 struct netbsd32_timespec ts32; 322 struct timespec ats; 323 int error; 324 325 if ((error = copyin(SCARG_P32(uap, tp), &ts32, sizeof(ts32))) != 0) 326 return (error); 327 328 netbsd32_to_timespec(&ts32, &ats); 329 return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true); 330 } 331 332 int 333 netbsd32___clock_getres50(struct lwp *l, const struct netbsd32___clock_getres50_args *uap, register_t *retval) 334 { 335 /* { 336 syscallarg(netbsd32_clockid_t) clock_id; 337 syscallarg(netbsd32_timespecp_t) tp; 338 } */ 339 struct netbsd32_timespec ts32; 340 struct timespec ts; 341 int error = 0; 342 343 error = clock_getres1(SCARG(uap, clock_id), &ts); 344 if (error != 0) 345 return error; 346 347 if (SCARG_P32(uap, tp)) { 348 netbsd32_from_timespec(&ts, &ts32); 349 error = copyout(&ts32, SCARG_P32(uap, tp), sizeof(ts32)); 350 } 351 352 return error; 353 } 354 355 int 356 netbsd32___nanosleep50(struct lwp *l, const struct netbsd32___nanosleep50_args *uap, register_t *retval) 357 { 358 /* { 359 syscallarg(const netbsd32_timespecp_t) rqtp; 360 syscallarg(netbsd32_timespecp_t) rmtp; 361 } */ 362 struct netbsd32_timespec ts32; 363 struct timespec rqt, rmt; 364 int error, error1; 365 366 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32)); 367 if (error) 368 return (error); 369 netbsd32_to_timespec(&ts32, &rqt); 370 371 error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt, 372 SCARG_P32(uap, rmtp) ? &rmt : NULL); 373 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 374 return error; 375 376 netbsd32_from_timespec(&rmt, &ts32); 377 error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32)); 378 return error1 ? error1 : error; 379 } 380 381 int 382 netbsd32_clock_nanosleep(struct lwp *l, const struct netbsd32_clock_nanosleep_args *uap, register_t *retval) 383 { 384 /* { 385 syscallarg(clockid_t) clock_id; 386 syscallarg(int) flags; 387 syscallarg(const netbsd32_timespecp_t) rqtp; 388 syscallarg(netbsd32_timespecp_t) rmtp; 389 } */ 390 struct netbsd32_timespec ts32; 391 struct timespec rqt, rmt; 392 int error, error1; 393 394 error = copyin(SCARG_P32(uap, rqtp), &ts32, sizeof(ts32)); 395 if (error) 396 goto out; 397 netbsd32_to_timespec(&ts32, &rqt); 398 399 error = nanosleep1(l, SCARG(uap, clock_id), SCARG(uap, flags), 400 &rqt, SCARG_P32(uap, rmtp) ? &rmt : NULL); 401 if (SCARG_P32(uap, rmtp) == NULL || (error != 0 && error != EINTR)) 402 goto out; 403 404 netbsd32_from_timespec(&rmt, &ts32); 405 if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0 && 406 (error1 = copyout(&ts32, SCARG_P32(uap, rmtp), sizeof(ts32))) != 0) 407 error = error1; 408 out: 409 *retval = error; 410 return 0; 411 } 412 413 static int 414 netbsd32_timer_create_fetch(const void *src, void *dst, size_t size) 415 { 416 struct sigevent *evp = dst; 417 struct netbsd32_sigevent ev32; 418 int error; 419 420 error = copyin(src, &ev32, sizeof(ev32)); 421 if (error) 422 return error; 423 424 netbsd32_to_sigevent(&ev32, evp); 425 return 0; 426 } 427 428 int 429 netbsd32_timer_create(struct lwp *l, const struct netbsd32_timer_create_args *uap, register_t *retval) 430 { 431 /* { 432 syscallarg(netbsd32_clockid_t) clock_id; 433 syscallarg(netbsd32_sigeventp_t) evp; 434 syscallarg(netbsd32_timerp_t) timerid; 435 } */ 436 437 return timer_create1(SCARG_P32(uap, timerid), 438 SCARG(uap, clock_id), SCARG_P32(uap, evp), 439 netbsd32_timer_create_fetch, l); 440 } 441 442 int 443 netbsd32_timer_delete(struct lwp *l, const struct netbsd32_timer_delete_args *uap, register_t *retval) 444 { 445 /* { 446 syscallarg(netbsd32_timer_t) timerid; 447 } */ 448 struct sys_timer_delete_args ua; 449 450 NETBSD32TO64_UAP(timerid); 451 return sys_timer_delete(l, (void *)&ua, retval); 452 } 453 454 int 455 netbsd32___timer_settime50(struct lwp *l, const struct netbsd32___timer_settime50_args *uap, register_t *retval) 456 { 457 /* { 458 syscallarg(netbsd32_timer_t) timerid; 459 syscallarg(int) flags; 460 syscallarg(const netbsd32_itimerspecp_t) value; 461 syscallarg(netbsd32_itimerspecp_t) ovalue; 462 } */ 463 int error; 464 struct itimerspec value, ovalue, *ovp = NULL; 465 struct netbsd32_itimerspec its32; 466 467 if ((error = copyin(SCARG_P32(uap, value), &its32, sizeof(its32))) != 0) 468 return (error); 469 netbsd32_to_timespec(&its32.it_interval, &value.it_interval); 470 netbsd32_to_timespec(&its32.it_value, &value.it_value); 471 472 if (SCARG_P32(uap, ovalue)) 473 ovp = &ovalue; 474 475 if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp, 476 SCARG(uap, flags), l->l_proc)) != 0) 477 return error; 478 479 if (ovp) { 480 netbsd32_from_timespec(&ovp->it_interval, &its32.it_interval); 481 netbsd32_from_timespec(&ovp->it_value, &its32.it_value); 482 return copyout(&its32, SCARG_P32(uap, ovalue), sizeof(its32)); 483 } 484 return 0; 485 } 486 487 int 488 netbsd32___timer_gettime50(struct lwp *l, const struct netbsd32___timer_gettime50_args *uap, register_t *retval) 489 { 490 /* { 491 syscallarg(netbsd32_timer_t) timerid; 492 syscallarg(netbsd32_itimerspecp_t) value; 493 } */ 494 int error; 495 struct itimerspec its; 496 struct netbsd32_itimerspec its32; 497 498 if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc, 499 &its)) != 0) 500 return error; 501 502 netbsd32_from_timespec(&its.it_interval, &its32.it_interval); 503 netbsd32_from_timespec(&its.it_value, &its32.it_value); 504 505 return copyout(&its32, SCARG_P32(uap, value), sizeof(its32)); 506 } 507 508 int 509 netbsd32_timer_getoverrun(struct lwp *l, const struct netbsd32_timer_getoverrun_args *uap, register_t *retval) 510 { 511 /* { 512 syscallarg(netbsd32_timer_t) timerid; 513 } */ 514 struct sys_timer_getoverrun_args ua; 515 516 NETBSD32TO64_UAP(timerid); 517 return sys_timer_getoverrun(l, (void *)&ua, retval); 518 } 519 520 int 521 netbsd32_clock_getcpuclockid2(struct lwp *l, 522 const struct netbsd32_clock_getcpuclockid2_args *uap, 523 register_t *retval) 524 { 525 /* { 526 syscallarg(idtype_t) idtype; 527 syscallarg(id_t) id; 528 syscallarg(netbsd32_clockidp_t) clock_id; 529 } */ 530 pid_t pid; 531 lwpid_t lid; 532 clockid_t clock_id; 533 id_t id = SCARG(uap, id); 534 535 switch (SCARG(uap, idtype)) { 536 case P_PID: 537 pid = id == 0 ? l->l_proc->p_pid : id; 538 clock_id = CLOCK_PROCESS_CPUTIME_ID | pid; 539 break; 540 case P_LWPID: 541 lid = id == 0 ? l->l_lid : id; 542 clock_id = CLOCK_THREAD_CPUTIME_ID | lid; 543 break; 544 default: 545 return EINVAL; 546 } 547 return copyout(&clock_id, SCARG_P32(uap, clock_id), sizeof(clock_id)); 548 } 549 550