linux_pipe.c revision 1.15
1/* $NetBSD: linux_pipe.c,v 1.15 1995/08/21 03:42:09 mycroft Exp $ */ 2 3/* 4 * Copyright (c) 1995 Frank van der Linden 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the NetBSD Project 18 * by Frank van der Linden 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34/* 35 * Linux compatibility module. Try to deal with various Linux system calls. 36 */ 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/namei.h> 41#include <sys/proc.h> 42#include <sys/dir.h> 43#include <sys/file.h> 44#include <sys/stat.h> 45#include <sys/filedesc.h> 46#include <sys/ioctl.h> 47#include <sys/kernel.h> 48#include <sys/malloc.h> 49#include <sys/mbuf.h> 50#include <sys/mman.h> 51#include <sys/mount.h> 52#include <sys/ptrace.h> 53#include <sys/resource.h> 54#include <sys/resourcevar.h> 55#include <sys/signal.h> 56#include <sys/signalvar.h> 57#include <sys/socket.h> 58#include <sys/time.h> 59#include <sys/times.h> 60#include <sys/vnode.h> 61#include <sys/uio.h> 62#include <sys/wait.h> 63#include <sys/utsname.h> 64#include <sys/unistd.h> 65 66#include <sys/syscallargs.h> 67 68#include <vm/vm.h> 69#include <vm/vm_param.h> 70 71#include <compat/linux/linux_types.h> 72#include <compat/linux/linux_fcntl.h> 73#include <compat/linux/linux_mmap.h> 74#include <compat/linux/linux_signal.h> 75#include <compat/linux/linux_syscallargs.h> 76#include <compat/linux/linux_util.h> 77#include <compat/linux/linux_dirent.h> 78 79/* 80 * The information on a terminated (or stopped) process needs 81 * to be converted in order for Linux binaries to get a valid signal 82 * number out of it. 83 */ 84static int 85bsd_to_linux_wstat(status) 86 int *status; 87{ 88 if (WIFSIGNALED(*status)) 89 *status = (*status & ~0177) | 90 bsd_to_linux_sig[WTERMSIG(*status)]; 91 else if (WIFSTOPPED(*status)) 92 *status = (*status & ~0xff00) | 93 (bsd_to_linux_sig[WSTOPSIG(*status)] << 8); 94} 95 96/* 97 * waitpid(2). Passed on to the NetBSD call, surrounded by code to 98 * reserve some space for a NetBSD-style wait status, and converting 99 * it to what Linux wants. 100 */ 101int 102linux_waitpid(p, uap, retval) 103 struct proc *p; 104 struct linux_waitpid_args /* { 105 syscallarg(int) pid; 106 syscallarg(int *) status; 107 syscallarg(int) options; 108 } */ *uap; 109 register_t *retval; 110{ 111 struct wait4_args w4a; 112 int error, *status, tstat; 113 caddr_t sg; 114 115 sg = stackgap_init(p->p_emul); 116 status = (int *) stackgap_alloc(&sg, sizeof status); 117 118 SCARG(&w4a, pid) = SCARG(uap, pid); 119 SCARG(&w4a, status) = status; 120 SCARG(&w4a, options) = SCARG(uap, options); 121 SCARG(&w4a, rusage) = NULL; 122 123 if ((error = wait4(p, &w4a, retval))) 124 return error; 125 126 if ((error = copyin(status, &tstat, sizeof tstat))) 127 return error; 128 129 bsd_to_linux_wstat(&tstat); 130 131 return copyout(&tstat, SCARG(uap, status), sizeof tstat); 132} 133 134/* 135 * This is very much the same as waitpid() 136 */ 137int 138linux_wait4(p, uap, retval) 139 struct proc *p; 140 struct linux_wait4_args /* { 141 syscallarg(int) pid; 142 syscallarg(int *) status; 143 syscallarg(int) options; 144 syscallarg(struct rusage *) rusage; 145 } */ *uap; 146 register_t *retval; 147{ 148 struct wait4_args w4a; 149 int error, *status, tstat; 150 caddr_t sg; 151 152 sg = stackgap_init(p->p_emul); 153 status = (int *) stackgap_alloc(&sg, sizeof status); 154 155 SCARG(&w4a, pid) = SCARG(uap, pid); 156 SCARG(&w4a, status) = status; 157 SCARG(&w4a, options) = SCARG(uap, options); 158 SCARG(&w4a, rusage) = SCARG(uap, rusage); 159 160 if ((error = wait4(p, &w4a, retval))) 161 return error; 162 163 if ((error = copyin(status, &tstat, sizeof tstat))) 164 return error; 165 166 bsd_to_linux_wstat(&tstat); 167 168 return copyout(&tstat, SCARG(uap, status), sizeof tstat); 169} 170 171/* 172 * This is the old brk(2) call. I don't think anything in the Linux 173 * world uses this anymore 174 */ 175int 176linux_break(p, uap, retval) 177 struct proc *p; 178 struct linux_brk_args /* { 179 syscallarg(char *) nsize; 180 } */ *uap; 181 register_t *retval; 182{ 183 return ENOSYS; 184} 185 186/* 187 * Linux brk(2). The check if the new address is >= the old one is 188 * done in the kernel in Linux. NetBSD does it in the library. 189 */ 190int 191linux_brk(p, uap, retval) 192 struct proc *p; 193 struct linux_brk_args /* { 194 syscallarg(char *) nsize; 195 } */ *uap; 196 register_t *retval; 197{ 198 char *nbrk = SCARG(uap, nsize); 199 struct obreak_args oba; 200 struct vmspace *vm = p->p_vmspace; 201 int error = 0; 202 caddr_t oldbrk, newbrk; 203 204 oldbrk = vm->vm_daddr + ctob(vm->vm_dsize); 205 /* 206 * XXX inconsistent.. Linux always returns at least the old 207 * brk value, but it will be page-aligned if this fails, 208 * and possibly not page aligned if it succeeds (the user 209 * supplied pointer is returned). 210 */ 211 SCARG(&oba, nsize) = nbrk; 212 213 if ((caddr_t) nbrk > vm->vm_daddr && obreak(p, &oba, retval) == 0) 214 retval[0] = (register_t) nbrk; 215 else 216 retval[0] = (register_t) oldbrk; 217 218 return 0; 219} 220 221/* 222 * I wonder why Linux has gettimeofday() _and_ time().. Still, we 223 * need to deal with it. 224 */ 225int 226linux_time(p, uap, retval) 227 struct proc *p; 228 struct linux_time_args /* { 229 linux_time_t *t; 230 } */ *uap; 231 register_t *retval; 232{ 233 struct timeval atv; 234 linux_time_t tt; 235 int error; 236 237 microtime(&atv); 238 239 tt = atv.tv_sec; 240 if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt))) 241 return error; 242 243 retval[0] = tt; 244 return 0; 245} 246 247/* 248 * Convert BSD statfs structure to Linux statfs structure. 249 * The Linux structure has less fields, and it also wants 250 * the length of a name in a dir entry in a field, which 251 * we fake (probably the wrong way). 252 */ 253static void 254bsd_to_linux_statfs(bsp, lsp) 255 struct statfs *bsp; 256 struct linux_statfs *lsp; 257{ 258 lsp->l_ftype = bsp->f_type; 259 lsp->l_fbsize = bsp->f_bsize; 260 lsp->l_fblocks = bsp->f_blocks; 261 lsp->l_fbfree = bsp->f_bfree; 262 lsp->l_fbavail = bsp->f_bavail; 263 lsp->l_ffiles = bsp->f_files; 264 lsp->l_fffree = bsp->f_ffree; 265 lsp->l_ffsid.val[0] = bsp->f_fsid.val[0]; 266 lsp->l_ffsid.val[1] = bsp->f_fsid.val[1]; 267 lsp->l_fnamelen = MAXNAMLEN; /* XXX */ 268} 269 270/* 271 * Implement the fs stat functions. Straightforward. 272 */ 273int 274linux_statfs(p, uap, retval) 275 struct proc *p; 276 struct linux_statfs_args /* { 277 syscallarg(char *) path; 278 syscallarg(struct linux_statfs *) sp; 279 } */ *uap; 280 register_t *retval; 281{ 282 struct statfs btmp, *bsp; 283 struct linux_statfs ltmp; 284 struct statfs_args bsa; 285 caddr_t sg; 286 int error; 287 288 sg = stackgap_init(p->p_emul); 289 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs)); 290 291 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 292 293 SCARG(&bsa, path) = SCARG(uap, path); 294 SCARG(&bsa, buf) = bsp; 295 296 if ((error = statfs(p, &bsa, retval))) 297 return error; 298 299 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp))) 300 return error; 301 302 bsd_to_linux_statfs(&btmp, <mp); 303 304 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp); 305} 306 307int 308linux_fstatfs(p, uap, retval) 309 struct proc *p; 310 struct linux_fstatfs_args /* { 311 syscallarg(int) fd; 312 syscallarg(struct linux_statfs *) sp; 313 } */ *uap; 314 register_t *retval; 315{ 316 struct statfs btmp, *bsp; 317 struct linux_statfs ltmp; 318 struct fstatfs_args bsa; 319 caddr_t sg; 320 int error; 321 322 sg = stackgap_init(p->p_emul); 323 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs)); 324 325 SCARG(&bsa, fd) = SCARG(uap, fd); 326 SCARG(&bsa, buf) = bsp; 327 328 if ((error = statfs(p, &bsa, retval))) 329 return error; 330 331 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp))) 332 return error; 333 334 bsd_to_linux_statfs(&btmp, <mp); 335 336 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp); 337} 338 339/* 340 * uname(). Just copy the info from the various strings stored in the 341 * kernel, and put it in the Linux utsname structure. That structure 342 * is almost the same as the NetBSD one, only it has fields 65 characters 343 * long, and an extra domainname field. 344 */ 345int 346linux_uname(p, uap, retval) 347 struct proc *p; 348 struct linux_uname_args /* { 349 syscallarg(struct linux_utsname *) up; 350 } */ *uap; 351 register_t *retval; 352{ 353 extern char ostype[], hostname[], osrelease[], version[], machine[], 354 domainname[]; 355 struct linux_utsname luts; 356 int len; 357 char *cp; 358 359 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname)); 360 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename)); 361 strncpy(luts.l_release, osrelease, sizeof(luts.l_release)); 362 strncpy(luts.l_version, version, sizeof(luts.l_version)); 363 strncpy(luts.l_machine, machine, sizeof(luts.l_machine)); 364 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname)); 365 366 /* This part taken from the the uname() in libc */ 367 len = sizeof(luts.l_version); 368 for (cp = luts.l_version; len--; ++cp) 369 if (*cp == '\n' || *cp == '\t') 370 if (len > 1) 371 *cp = ' '; 372 else 373 *cp = '\0'; 374 375 return copyout(&luts, SCARG(uap, up), sizeof(luts)); 376} 377 378int 379linux_olduname(p, uap, retval) 380 struct proc *p; 381 struct linux_uname_args /* { 382 syscallarg(struct linux_oldutsname *) up; 383 } */ *uap; 384 register_t *retval; 385{ 386 extern char ostype[], hostname[], osrelease[], version[], machine[]; 387 struct linux_oldutsname luts; 388 int len; 389 char *cp; 390 391 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname)); 392 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename)); 393 strncpy(luts.l_release, osrelease, sizeof(luts.l_release)); 394 strncpy(luts.l_version, version, sizeof(luts.l_version)); 395 strncpy(luts.l_machine, machine, sizeof(luts.l_machine)); 396 397 /* This part taken from the the uname() in libc */ 398 len = sizeof(luts.l_version); 399 for (cp = luts.l_version; len--; ++cp) 400 if (*cp == '\n' || *cp == '\t') 401 if (len > 1) 402 *cp = ' '; 403 else 404 *cp = '\0'; 405 406 return copyout(&luts, SCARG(uap, up), sizeof(luts)); 407} 408 409int 410linux_oldolduname(p, uap, retval) 411 struct proc *p; 412 struct linux_uname_args /* { 413 syscallarg(struct linux_oldoldutsname *) up; 414 } */ *uap; 415 register_t *retval; 416{ 417 extern char ostype[], hostname[], osrelease[], version[], machine[]; 418 struct linux_oldoldutsname luts; 419 int len; 420 char *cp; 421 422 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname)); 423 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename)); 424 strncpy(luts.l_release, osrelease, sizeof(luts.l_release)); 425 strncpy(luts.l_version, version, sizeof(luts.l_version)); 426 strncpy(luts.l_machine, machine, sizeof(luts.l_machine)); 427 428 /* This part taken from the the uname() in libc */ 429 len = sizeof(luts.l_version); 430 for (cp = luts.l_version; len--; ++cp) 431 if (*cp == '\n' || *cp == '\t') 432 if (len > 1) 433 *cp = ' '; 434 else 435 *cp = '\0'; 436 437 return copyout(&luts, SCARG(uap, up), sizeof(luts)); 438} 439 440/* 441 * Linux wants to pass everything to a syscall in registers. However, 442 * mmap() has 6 of them. Oops: out of register error. They just pass 443 * everything in a structure. 444 */ 445int 446linux_mmap(p, uap, retval) 447 struct proc *p; 448 struct linux_mmap_args /* { 449 syscallarg(struct linux_mmap *) lmp; 450 } */ *uap; 451 register_t *retval; 452{ 453 struct linux_mmap lmap; 454 struct mmap_args cma; 455 int error, flags; 456 457 if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap))) 458 return error; 459 460 flags = 0; 461 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED); 462 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE); 463 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED); 464 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON); 465 466 SCARG(&cma,addr) = lmap.lm_addr; 467 SCARG(&cma,len) = lmap.lm_len; 468 SCARG(&cma,prot) = lmap.lm_prot; 469 SCARG(&cma,flags) = flags; 470 SCARG(&cma,fd) = lmap.lm_fd; 471 SCARG(&cma,pad) = 0; 472 SCARG(&cma,pos) = lmap.lm_pos; 473 474 return mmap(p, &cma, retval); 475} 476 477/* 478 * Linux doesn't use the retval[1] value to determine whether 479 * we are the child or parent. 480 */ 481int 482linux_fork(p, uap, retval) 483 struct proc *p; 484 void *uap; 485 register_t *retval; 486{ 487 int error; 488 489 if ((error = fork(p, uap, retval))) 490 return error; 491 492 if (retval[1] == 1) 493 retval[0] = 0; 494 495 return 0; 496} 497 498/* 499 * This code is partly stolen from src/lib/libc/compat-43/times.c 500 * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here 501 */ 502 503#define CLK_TCK 100 504#define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 505 506int 507linux_times(p, uap, retval) 508 struct proc *p; 509 struct linux_times_args /* { 510 syscallarg(struct times *) tms; 511 } */ *uap; 512 register_t *retval; 513{ 514 struct timeval t; 515 struct linux_tms ltms; 516 struct rusage ru; 517 int error, s; 518 519 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL); 520 ltms.ltms_utime = CONVTCK(ru.ru_utime); 521 ltms.ltms_stime = CONVTCK(ru.ru_stime); 522 523 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime); 524 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime); 525 526 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms))) 527 return error; 528 529 s = splclock(); 530 timersub(&time, &boottime, &t); 531 splx(s); 532 533 retval[0] = ((linux_clock_t)(CONVTCK(t))); 534 return 0; 535} 536 537/* 538 * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1]. 539 * Linux directly passes the pointer. 540 */ 541int 542linux_pipe(p, uap, retval) 543 struct proc *p; 544 struct linux_pipe_args /* { 545 syscallarg(int *) pfds; 546 } */ *uap; 547 register_t *retval; 548{ 549 int error; 550 551 if ((error = pipe(p, 0, retval))) 552 return error; 553 554 /* Assumes register_t is an int */ 555 556 if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int)))) 557 return error; 558 559 retval[0] = 0; 560 return 0; 561} 562 563/* 564 * Alarm. This is a libc call which used setitimer(2) in NetBSD. 565 * Fiddle with the timers to make it work. 566 */ 567int 568linux_alarm(p, uap, retval) 569 struct proc *p; 570 struct linux_alarm_args /* { 571 syscallarg(unsigned int) secs; 572 } */ *uap; 573 register_t *retval; 574{ 575 int error, s; 576 struct itimerval *itp, it; 577 578 itp = &p->p_realtimer; 579 s = splclock(); 580 /* 581 * Clear any pending timer alarms. 582 */ 583 untimeout(realitexpire, p); 584 timerclear(&itp->it_interval); 585 if (timerisset(&itp->it_value) && 586 timercmp(&itp->it_value, &time, >)) 587 timersub(&itp->it_value, &time, &itp->it_value); 588 /* 589 * Return how many seconds were left (rounded up) 590 */ 591 retval[0] = itp->it_value.tv_sec; 592 if (itp->it_value.tv_usec) 593 retval[0]++; 594 595 /* 596 * alarm(0) just resets the timer. 597 */ 598 if (SCARG(uap, secs) == 0) { 599 timerclear(&itp->it_value); 600 splx(s); 601 return 0; 602 } 603 604 /* 605 * Check the new alarm time for sanity, and set it. 606 */ 607 timerclear(&it.it_interval); 608 it.it_value.tv_sec = SCARG(uap, secs); 609 it.it_value.tv_usec = 0; 610 if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) { 611 splx(s); 612 return (EINVAL); 613 } 614 615 if (timerisset(&it.it_value)) { 616 timeradd(&it.it_value, &time, &it.it_value); 617 timeout(realitexpire, p, hzto(&it.it_value)); 618 } 619 p->p_realtimer = it; 620 splx(s); 621 622 return 0; 623} 624 625/* 626 * utime(). Do conversion to things that utimes() understands, 627 * and pass it on. 628 */ 629int 630linux_utime(p, uap, retval) 631 struct proc *p; 632 struct linux_utime_args /* { 633 syscallarg(char *) path; 634 syscallarg(struct linux_utimbuf *)times; 635 } */ *uap; 636 register_t *retval; 637{ 638 caddr_t sg; 639 int error; 640 struct utimes_args ua; 641 struct timeval tv[2], *tvp; 642 struct linux_utimbuf lut; 643 644 sg = stackgap_init(p->p_emul); 645 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 646 647 SCARG(&ua, path) = SCARG(uap, path); 648 649 if (SCARG(uap, times) != NULL) { 650 if ((error = copyin(SCARG(uap, times), &lut, sizeof lut))) 651 return error; 652 tv[0].tv_usec = tv[1].tv_usec = 0; 653 tv[0].tv_sec = lut.l_actime; 654 tv[1].tv_sec = lut.l_modtime; 655 tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv)); 656 if ((error = copyout(tv, tvp, sizeof tv))) 657 return error; 658 SCARG(&ua, tptr) = tvp; 659 } 660 else 661 SCARG(&ua, tptr) = NULL; 662 663 return utimes(p, uap, retval); 664} 665 666/* 667 * Linux 'readdir' call. This code is mostly taken from the 668 * SunOS getdents call (see compat/sunos/sunos_misc.c), though 669 * an attempt has been made to keep it a little cleaner (failing 670 * miserably, because of the cruft needed if count 1 is passed). 671 * 672 * Read in BSD-style entries, convert them, and copy them out. 673 * Note that the Linux d_reclen is actually the name length, 674 * and d_off is the reclen. 675 * 676 * Note that this doesn't handle union-mounted filesystems. 677 */ 678int 679linux_readdir(p, uap, retval) 680 struct proc *p; 681 struct linux_readdir_args /* { 682 syscallarg(int) fd; 683 syscallarg(struct linux_dirent *) dent; 684 syscallarg(unsigned int) count; 685 } */ *uap; 686 register_t *retval; 687{ 688 register struct dirent *bdp; 689 struct vnode *vp; 690 caddr_t inp, buf; /* BSD-format */ 691 int len, reclen; /* BSD-format */ 692 caddr_t outp; /* Linux-format */ 693 int resid, linuxreclen; /* Linux-format */ 694 struct file *fp; 695 struct uio auio; 696 struct iovec aiov; 697 struct linux_dirent idb; 698 off_t off; /* true file offset */ 699 linux_off_t soff; /* Linux file offset */ 700 int buflen, error, eofflag, nbytes, justone; 701 struct vattr va; 702 703 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 704 return (error); 705 706 if ((fp->f_flag & FREAD) == 0) 707 return (EBADF); 708 709 vp = (struct vnode *)fp->f_data; 710 711 if (vp->v_type != VDIR) /* XXX vnode readdir op should do this */ 712 return (EINVAL); 713 714 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) 715 return error; 716 717 nbytes = SCARG(uap, count); 718 if (nbytes == 1) { /* Need this for older Linux libs, apparently */ 719 nbytes = sizeof (struct linux_dirent); 720 buflen = max(va.va_blocksize, nbytes); 721 justone = 1; 722 } else { 723 buflen = min(MAXBSIZE, nbytes); 724 justone = 0; 725 } 726 buf = malloc(buflen, M_TEMP, M_WAITOK); 727 VOP_LOCK(vp); 728 off = fp->f_offset; 729again: 730 aiov.iov_base = buf; 731 aiov.iov_len = buflen; 732 auio.uio_iov = &aiov; 733 auio.uio_iovcnt = 1; 734 auio.uio_rw = UIO_READ; 735 auio.uio_segflg = UIO_SYSSPACE; 736 auio.uio_procp = p; 737 auio.uio_resid = buflen; 738 auio.uio_offset = off; 739 /* 740 * First we read into the malloc'ed buffer, then 741 * we massage it into user space, one record at a time. 742 */ 743 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0); 744 if (error) 745 goto out; 746 747 inp = buf; 748 outp = (caddr_t) SCARG(uap, dent); 749 resid = nbytes; 750 if ((len = buflen - auio.uio_resid) == 0) 751 goto eof; 752 753 for (; len > 0; len -= reclen) { 754 bdp = (struct dirent *)inp; 755 reclen = bdp->d_reclen; 756 if (reclen & 3) 757 panic("linux_readdir"); 758 off += reclen; /* each entry points to next */ 759 if (bdp->d_fileno == 0) { 760 inp += reclen; /* it is a hole; squish it out */ 761 continue; 762 } 763 linuxreclen = LINUX_RECLEN(&idb, bdp->d_namlen); 764 if (reclen > len || resid < linuxreclen) { 765 /* entry too big for buffer, so just stop */ 766 outp++; 767 break; 768 } 769 /* 770 * Massage in place to make a Linux-shaped dirent (otherwise 771 * we have to worry about touching user memory outside of 772 * the copyout() call). 773 */ 774 idb.d_ino = (long)bdp->d_fileno; 775 idb.d_off = (linux_off_t)linuxreclen; 776 idb.d_reclen = (u_short)bdp->d_namlen; 777 strcpy(idb.d_name, bdp->d_name); 778 if ((error = copyout((caddr_t)&idb, outp, linuxreclen))) 779 goto out; 780 /* advance past this real entry */ 781 inp += reclen; 782 /* advance output past Linux-shaped entry */ 783 outp += linuxreclen; 784 resid -= linuxreclen; 785 if (justone) 786 break; 787 } 788 789 /* if we squished out the whole block, try again */ 790 if (outp == (caddr_t) SCARG(uap, dent)) 791 goto again; 792 fp->f_offset = off; /* update the vnode offset */ 793 794 if (justone) 795 nbytes = resid + linuxreclen; 796 797eof: 798 *retval = nbytes - resid; 799out: 800 VOP_UNLOCK(vp); 801 free(buf, M_TEMP); 802 return error; 803} 804 805/* 806 * Out of register error once more.. Also, Linux copies the amount of 807 * time left into the user-supplied timeval structure. 808 */ 809int 810linux_select(p, uap, retval) 811 struct proc *p; 812 struct linux_select_args /* { 813 syscallarg(struct linux_select *) lsp; 814 } */ *uap; 815 register_t *retval; 816{ 817 struct linux_select ls; 818 struct select_args bsa; 819 struct timeval tv0, tv1, utv, *tvp; 820 caddr_t sg; 821 int error; 822 823 if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls)))) 824 return error; 825 826 SCARG(&bsa, nd) = ls.nfds; 827 SCARG(&bsa, in) = ls.readfds; 828 SCARG(&bsa, ou) = ls.writefds; 829 SCARG(&bsa, ex) = ls.exceptfds; 830 SCARG(&bsa, tv) = ls.timeout; 831 832 /* 833 * Store current time for computation of the amount of 834 * time left. 835 */ 836 if (ls.timeout) { 837 if ((error = copyin(ls.timeout, &utv, sizeof(utv)))) 838 return error; 839 if (itimerfix(&utv)) { 840 /* 841 * The timeval was invalid. Convert it to something 842 * valid that will act as it does under Linux. 843 */ 844 sg = stackgap_init(p->p_emul); 845 tvp = stackgap_alloc(&sg, sizeof(utv)); 846 utv.tv_sec += utv.tv_usec / 1000000; 847 utv.tv_usec %= 1000000; 848 if (utv.tv_usec < 0) { 849 utv.tv_sec -= 1; 850 utv.tv_usec += 1000000; 851 } 852 if (utv.tv_sec < 0) 853 timerclear(&utv); 854 if ((error = copyout(&utv, tvp, sizeof(utv)))) 855 return error; 856 SCARG(&bsa, tv) = tvp; 857 } 858 microtime(&tv0); 859 } 860 861 error = select(p, &bsa, retval); 862 if (error) { 863 /* 864 * See fs/select.c in the Linux kernel. Without this, 865 * Maelstrom doesn't work. 866 */ 867 if (error == ERESTART) 868 error = EINTR; 869 return error; 870 } 871 872 if (ls.timeout) { 873 if (*retval) { 874 /* 875 * Compute how much time was left of the timeout, 876 * by subtracting the current time and the time 877 * before we started the call, and subtracting 878 * that result from the user-supplied value. 879 */ 880 microtime(&tv1); 881 timersub(&tv1, &tv0, &tv1); 882 timersub(&utv, &tv1, &utv); 883 if (utv.tv_sec < 0) 884 timerclear(&utv); 885 } else 886 timerclear(&utv); 887 if ((error = copyout(&utv, ls.timeout, sizeof(utv)))) 888 return error; 889 } 890 891 return 0; 892} 893 894/* 895 * Get the process group of a certain process. Look it up 896 * and return the value. 897 */ 898int 899linux_getpgid(p, uap, retval) 900 struct proc *p; 901 struct linux_getpgid_args /* { 902 syscallarg(int) pid; 903 } */ *uap; 904 register_t *retval; 905{ 906 struct proc *targp; 907 908 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) 909 if ((targp = pfind(SCARG(uap, pid))) == 0) 910 return ESRCH; 911 else 912 targp = p; 913 914 retval[0] = targp->p_pgid; 915 return 0; 916} 917 918/* 919 * Set the 'personality' (emulation mode) for the current process. Only 920 * accept the Linux personality here (0). This call is needed because 921 * the Linux ELF crt0 issues it in an ugly kludge to make sure that 922 * ELF binaries run in Linux mode, not SVR4 mode. 923 */ 924int 925linux_personality(p, uap, retval) 926 struct proc *p; 927 struct linux_personality_args /* P 928 syscallarg(int) per; 929 } */ *uap; 930 register_t *retval; 931{ 932 if (SCARG(uap, per) != 0) 933 return EINVAL; 934 retval[0] = 0; 935 return 0; 936} 937