1 /* $NetBSD: linux_misc_notalpha.c,v 1.77 2006/02/09 19:18:56 manu Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe 9 * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: linux_misc_notalpha.c,v 1.77 2006/02/09 19:18:56 manu Exp $"); 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/mman.h> 47 #include <sys/mount.h> 48 #include <sys/malloc.h> 49 #include <sys/mbuf.h> 50 #include <sys/namei.h> 51 #include <sys/proc.h> 52 #include <sys/ptrace.h> 53 #include <sys/resource.h> 54 #include <sys/resourcevar.h> 55 #include <sys/time.h> 56 #include <sys/wait.h> 57 58 #include <sys/sa.h> 59 #include <sys/syscallargs.h> 60 61 #include <compat/linux/common/linux_types.h> 62 #include <compat/linux/common/linux_fcntl.h> 63 #include <compat/linux/common/linux_misc.h> 64 #include <compat/linux/common/linux_mmap.h> 65 #include <compat/linux/common/linux_signal.h> 66 #include <compat/linux/common/linux_util.h> 67 68 #include <compat/linux/linux_syscallargs.h> 69 70 /* 71 * This file contains routines which are used 72 * on every linux architechture except the Alpha. 73 */ 74 75 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */ 76 /* Not used on: alpha */ 77 78 #ifdef DEBUG_LINUX 79 #define DPRINTF(a) uprintf a 80 #else 81 #define DPRINTF(a) 82 #endif 83 84 #ifndef COMPAT_LINUX32 85 #if !defined(__m68k__) 86 static void bsd_to_linux_statfs64(const struct statvfs *, 87 struct linux_statfs64 *); 88 #endif 89 90 /* 91 * Alarm. This is a libc call which uses setitimer(2) in NetBSD. 92 * Fiddle with the timers to make it work. 93 */ 94 int 95 linux_sys_alarm(l, v, retval) 96 struct lwp *l; 97 void *v; 98 register_t *retval; 99 { 100 struct linux_sys_alarm_args /* { 101 syscallarg(unsigned int) secs; 102 } */ *uap = v; 103 struct proc *p = l->l_proc; 104 int s; 105 struct itimerval *itp, it; 106 struct ptimer *ptp; 107 108 if (p->p_timers && p->p_timers->pts_timers[ITIMER_REAL]) 109 itp = &p->p_timers->pts_timers[ITIMER_REAL]->pt_time; 110 else 111 itp = NULL; 112 s = splclock(); 113 /* 114 * Clear any pending timer alarms. 115 */ 116 if (itp) { 117 callout_stop(&p->p_timers->pts_timers[ITIMER_REAL]->pt_ch); 118 timerclear(&itp->it_interval); 119 if (timerisset(&itp->it_value) && 120 timercmp(&itp->it_value, &time, >)) 121 timersub(&itp->it_value, &time, &itp->it_value); 122 /* 123 * Return how many seconds were left (rounded up) 124 */ 125 retval[0] = itp->it_value.tv_sec; 126 if (itp->it_value.tv_usec) 127 retval[0]++; 128 } else { 129 retval[0] = 0; 130 } 131 132 /* 133 * alarm(0) just resets the timer. 134 */ 135 if (SCARG(uap, secs) == 0) { 136 if (itp) 137 timerclear(&itp->it_value); 138 splx(s); 139 return 0; 140 } 141 142 /* 143 * Check the new alarm time for sanity, and set it. 144 */ 145 timerclear(&it.it_interval); 146 it.it_value.tv_sec = SCARG(uap, secs); 147 it.it_value.tv_usec = 0; 148 if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) { 149 splx(s); 150 return (EINVAL); 151 } 152 153 if (p->p_timers == NULL) 154 timers_alloc(p); 155 ptp = p->p_timers->pts_timers[ITIMER_REAL]; 156 if (ptp == NULL) { 157 ptp = pool_get(&ptimer_pool, PR_WAITOK); 158 ptp->pt_ev.sigev_notify = SIGEV_SIGNAL; 159 ptp->pt_ev.sigev_signo = SIGALRM; 160 ptp->pt_overruns = 0; 161 ptp->pt_proc = p; 162 ptp->pt_type = CLOCK_REALTIME; 163 ptp->pt_entry = CLOCK_REALTIME; 164 callout_init(&ptp->pt_ch); 165 p->p_timers->pts_timers[ITIMER_REAL] = ptp; 166 } 167 168 if (timerisset(&it.it_value)) { 169 /* 170 * Don't need to check hzto() return value, here. 171 * callout_reset() does it for us. 172 */ 173 timeradd(&it.it_value, &time, &it.it_value); 174 callout_reset(&ptp->pt_ch, hzto(&it.it_value), 175 realtimerexpire, ptp); 176 } 177 ptp->pt_time = it; 178 splx(s); 179 180 return 0; 181 } 182 #endif /* !COMPAT_LINUX32 */ 183 184 #if !defined(__amd64__) || defined(COMPAT_LINUX32) 185 int 186 linux_sys_nice(l, v, retval) 187 struct lwp *l; 188 void *v; 189 register_t *retval; 190 { 191 struct linux_sys_nice_args /* { 192 syscallarg(int) incr; 193 } */ *uap = v; 194 struct sys_setpriority_args bsa; 195 196 SCARG(&bsa, which) = PRIO_PROCESS; 197 SCARG(&bsa, who) = 0; 198 SCARG(&bsa, prio) = SCARG(uap, incr); 199 return sys_setpriority(l, &bsa, retval); 200 } 201 #endif /* !__amd64__ || COMPAT_LINUX32 */ 202 203 #ifndef COMPAT_LINUX32 204 #ifndef __amd64__ 205 /* 206 * The old Linux readdir was only able to read one entry at a time, 207 * even though it had a 'count' argument. In fact, the emulation 208 * of the old call was better than the original, because it did handle 209 * the count arg properly. Don't bother with it anymore now, and use 210 * it to distinguish between old and new. The difference is that the 211 * newer one actually does multiple entries, and the reclen field 212 * really is the reclen, not the namelength. 213 */ 214 int 215 linux_sys_readdir(l, v, retval) 216 struct lwp *l; 217 void *v; 218 register_t *retval; 219 { 220 struct linux_sys_readdir_args /* { 221 syscallarg(int) fd; 222 syscallarg(struct linux_dirent *) dent; 223 syscallarg(unsigned int) count; 224 } */ *uap = v; 225 226 SCARG(uap, count) = 1; 227 return linux_sys_getdents(l, uap, retval); 228 } 229 #endif /* !amd64 */ 230 231 /* 232 * I wonder why Linux has gettimeofday() _and_ time().. Still, we 233 * need to deal with it. 234 */ 235 int 236 linux_sys_time(l, v, retval) 237 struct lwp *l; 238 void *v; 239 register_t *retval; 240 { 241 struct linux_sys_time_args /* { 242 linux_time_t *t; 243 } */ *uap = v; 244 struct timeval atv; 245 linux_time_t tt; 246 int error; 247 248 microtime(&atv); 249 250 tt = atv.tv_sec; 251 if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt))) 252 return error; 253 254 retval[0] = tt; 255 return 0; 256 } 257 258 /* 259 * utime(). Do conversion to things that utimes() understands, 260 * and pass it on. 261 */ 262 int 263 linux_sys_utime(l, v, retval) 264 struct lwp *l; 265 void *v; 266 register_t *retval; 267 { 268 struct linux_sys_utime_args /* { 269 syscallarg(const char *) path; 270 syscallarg(struct linux_utimbuf *)times; 271 } */ *uap = v; 272 struct proc *p = l->l_proc; 273 caddr_t sg; 274 int error; 275 struct sys_utimes_args ua; 276 struct timeval tv[2], *tvp; 277 struct linux_utimbuf lut; 278 279 sg = stackgap_init(p, 0); 280 tvp = (struct timeval *) stackgap_alloc(p, &sg, sizeof(tv)); 281 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path)); 282 283 SCARG(&ua, path) = SCARG(uap, path); 284 285 if (SCARG(uap, times) != NULL) { 286 if ((error = copyin(SCARG(uap, times), &lut, sizeof lut))) 287 return error; 288 tv[0].tv_usec = tv[1].tv_usec = 0; 289 tv[0].tv_sec = lut.l_actime; 290 tv[1].tv_sec = lut.l_modtime; 291 if ((error = copyout(tv, tvp, sizeof tv))) 292 return error; 293 SCARG(&ua, tptr) = tvp; 294 } 295 else 296 SCARG(&ua, tptr) = NULL; 297 298 return sys_utimes(l, &ua, retval); 299 } 300 301 #ifndef __amd64__ 302 /* 303 * waitpid(2). Just forward on to linux_sys_wait4 with a NULL rusage. 304 */ 305 int 306 linux_sys_waitpid(l, v, retval) 307 struct lwp *l; 308 void *v; 309 register_t *retval; 310 { 311 struct linux_sys_waitpid_args /* { 312 syscallarg(int) pid; 313 syscallarg(int *) status; 314 syscallarg(int) options; 315 } */ *uap = v; 316 struct linux_sys_wait4_args linux_w4a; 317 318 SCARG(&linux_w4a, pid) = SCARG(uap, pid); 319 SCARG(&linux_w4a, status) = SCARG(uap, status); 320 SCARG(&linux_w4a, options) = SCARG(uap, options); 321 SCARG(&linux_w4a, rusage) = NULL; 322 323 return linux_sys_wait4(l, &linux_w4a, retval); 324 } 325 #endif /* !amd64 */ 326 327 int 328 linux_sys_setresgid(l, v, retval) 329 struct lwp *l; 330 void *v; 331 register_t *retval; 332 { 333 struct linux_sys_setresgid_args /* { 334 syscallarg(gid_t) rgid; 335 syscallarg(gid_t) egid; 336 syscallarg(gid_t) sgid; 337 } */ *uap = v; 338 339 /* 340 * Note: These checks are a little different than the NetBSD 341 * setregid(2) call performs. This precisely follows the 342 * behavior of the Linux kernel. 343 */ 344 return do_setresgid(l, SCARG(uap,rgid), SCARG(uap, egid), 345 SCARG(uap, sgid), 346 ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S | 347 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S | 348 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S ); 349 } 350 351 int 352 linux_sys_getresgid(l, v, retval) 353 struct lwp *l; 354 void *v; 355 register_t *retval; 356 { 357 struct linux_sys_getresgid_args /* { 358 syscallarg(gid_t *) rgid; 359 syscallarg(gid_t *) egid; 360 syscallarg(gid_t *) sgid; 361 } */ *uap = v; 362 struct proc *p = l->l_proc; 363 struct pcred *pc = p->p_cred; 364 int error; 365 366 /* 367 * Linux copies these values out to userspace like so: 368 * 369 * 1. Copy out rgid. 370 * 2. If that succeeds, copy out egid. 371 * 3. If both of those succeed, copy out sgid. 372 */ 373 if ((error = copyout(&pc->p_rgid, SCARG(uap, rgid), 374 sizeof(gid_t))) != 0) 375 return (error); 376 377 if ((error = copyout(&pc->pc_ucred->cr_gid, SCARG(uap, egid), 378 sizeof(gid_t))) != 0) 379 return (error); 380 381 return (copyout(&pc->p_svgid, SCARG(uap, sgid), sizeof(gid_t))); 382 } 383 384 #ifndef __amd64__ 385 /* 386 * I wonder why Linux has settimeofday() _and_ stime().. Still, we 387 * need to deal with it. 388 */ 389 int 390 linux_sys_stime(l, v, retval) 391 struct lwp *l; 392 void *v; 393 register_t *retval; 394 { 395 struct linux_sys_time_args /* { 396 linux_time_t *t; 397 } */ *uap = v; 398 struct proc *p = l->l_proc; 399 struct timespec ats; 400 linux_time_t tt; 401 int error; 402 403 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 404 return (error); 405 406 if ((error = copyin(&tt, SCARG(uap, t), sizeof tt)) != 0) 407 return error; 408 409 ats.tv_sec = tt; 410 ats.tv_nsec = 0; 411 412 if ((error = settime(p, &ats))) 413 return (error); 414 415 return 0; 416 } 417 #endif /* !amd64 */ 418 419 #if !defined(__m68k__) 420 /* 421 * Convert NetBSD statvfs structure to Linux statfs64 structure. 422 * See comments in bsd_to_linux_statfs() for further background. 423 * We can safely pass correct bsize and frsize here, since Linux glibc 424 * statvfs() doesn't use statfs64(). 425 */ 426 static void 427 bsd_to_linux_statfs64(bsp, lsp) 428 const struct statvfs *bsp; 429 struct linux_statfs64 *lsp; 430 { 431 int i, div; 432 433 for (i = 0; i < linux_fstypes_cnt; i++) { 434 if (strcmp(bsp->f_fstypename, linux_fstypes[i].bsd) == 0) { 435 lsp->l_ftype = linux_fstypes[i].linux; 436 break; 437 } 438 } 439 440 if (i == linux_fstypes_cnt) { 441 DPRINTF(("unhandled fstype in linux emulation: %s\n", 442 bsp->f_fstypename)); 443 lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC; 444 } 445 446 div = bsp->f_bsize / bsp->f_frsize; 447 lsp->l_fbsize = bsp->f_bsize; 448 lsp->l_ffrsize = bsp->f_frsize; 449 lsp->l_fblocks = bsp->f_blocks / div; 450 lsp->l_fbfree = bsp->f_bfree / div; 451 lsp->l_fbavail = bsp->f_bavail / div; 452 lsp->l_ffiles = bsp->f_files; 453 lsp->l_fffree = bsp->f_ffree / div; 454 /* Linux sets the fsid to 0..., we don't */ 455 lsp->l_ffsid.val[0] = bsp->f_fsidx.__fsid_val[0]; 456 lsp->l_ffsid.val[1] = bsp->f_fsidx.__fsid_val[1]; 457 lsp->l_fnamelen = bsp->f_namemax; 458 (void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare)); 459 } 460 461 /* 462 * Implement the fs stat functions. Straightforward. 463 */ 464 int 465 linux_sys_statfs64(l, v, retval) 466 struct lwp *l; 467 void *v; 468 register_t *retval; 469 { 470 struct linux_sys_statfs64_args /* { 471 syscallarg(const char *) path; 472 syscallarg(size_t) sz; 473 syscallarg(struct linux_statfs64 *) sp; 474 } */ *uap = v; 475 struct proc *p = l->l_proc; 476 struct statvfs btmp, *bsp; 477 struct linux_statfs64 ltmp; 478 struct sys_statvfs1_args bsa; 479 caddr_t sg; 480 int error; 481 482 if (SCARG(uap, sz) != sizeof ltmp) 483 return (EINVAL); 484 485 sg = stackgap_init(p, 0); 486 bsp = (struct statvfs *) stackgap_alloc(p, &sg, sizeof (struct statvfs)); 487 488 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path)); 489 490 SCARG(&bsa, path) = SCARG(uap, path); 491 SCARG(&bsa, buf) = bsp; 492 SCARG(&bsa, flags) = ST_WAIT; 493 494 if ((error = sys_statvfs1(l, &bsa, retval))) 495 return error; 496 497 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp))) 498 return error; 499 500 bsd_to_linux_statfs64(&btmp, <mp); 501 502 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp); 503 } 504 505 int 506 linux_sys_fstatfs64(l, v, retval) 507 struct lwp *l; 508 void *v; 509 register_t *retval; 510 { 511 struct linux_sys_fstatfs64_args /* { 512 syscallarg(int) fd; 513 syscallarg(size_t) sz; 514 syscallarg(struct linux_statfs64 *) sp; 515 } */ *uap = v; 516 struct proc *p = l->l_proc; 517 struct statvfs btmp, *bsp; 518 struct linux_statfs64 ltmp; 519 struct sys_fstatvfs1_args bsa; 520 caddr_t sg; 521 int error; 522 523 if (SCARG(uap, sz) != sizeof ltmp) 524 return (EINVAL); 525 526 sg = stackgap_init(p, 0); 527 bsp = (struct statvfs *) stackgap_alloc(p, &sg, sizeof (struct statvfs)); 528 529 SCARG(&bsa, fd) = SCARG(uap, fd); 530 SCARG(&bsa, buf) = bsp; 531 SCARG(&bsa, flags) = ST_WAIT; 532 533 if ((error = sys_fstatvfs1(l, &bsa, retval))) 534 return error; 535 536 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp))) 537 return error; 538 539 bsd_to_linux_statfs64(&btmp, <mp); 540 541 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp); 542 } 543 #endif /* !__m68k__ */ 544 #endif /* !COMPAT_LINUX32 */ 545