linux_pipe.c revision 1.24
1/* $NetBSD: linux_pipe.c,v 1.24 1995/12/18 14:35:08 fvdl 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 89 if (WIFSIGNALED(*status)) 90 *status = (*status & ~0177) | 91 bsd_to_linux_sig[WTERMSIG(*status)]; 92 else if (WIFSTOPPED(*status)) 93 *status = (*status & ~0xff00) | 94 (bsd_to_linux_sig[WSTOPSIG(*status)] << 8); 95} 96 97/* 98 * waitpid(2). Passed on to the NetBSD call, surrounded by code to 99 * reserve some space for a NetBSD-style wait status, and converting 100 * it to what Linux wants. 101 */ 102int 103linux_sys_waitpid(p, v, retval) 104 struct proc *p; 105 void *v; 106 register_t *retval; 107{ 108 struct linux_sys_waitpid_args /* { 109 syscallarg(int) pid; 110 syscallarg(int *) status; 111 syscallarg(int) options; 112 } */ *uap = v; 113 struct sys_wait4_args w4a; 114 int error, *status, tstat; 115 caddr_t sg; 116 117 if (SCARG(uap, status) != NULL) { 118 sg = stackgap_init(p->p_emul); 119 status = (int *) stackgap_alloc(&sg, sizeof status); 120 } else 121 status = NULL; 122 123 SCARG(&w4a, pid) = SCARG(uap, pid); 124 SCARG(&w4a, status) = status; 125 SCARG(&w4a, options) = SCARG(uap, options); 126 SCARG(&w4a, rusage) = NULL; 127 128 if ((error = sys_wait4(p, &w4a, retval))) 129 return error; 130 131 p->p_siglist &= ~sigmask(SIGCHLD); 132 133 if (status != NULL) { 134 if ((error = copyin(status, &tstat, sizeof tstat))) 135 return error; 136 137 bsd_to_linux_wstat(&tstat); 138 return copyout(&tstat, SCARG(uap, status), sizeof tstat); 139 } 140 141 return 0; 142} 143 144/* 145 * This is very much the same as waitpid() 146 */ 147int 148linux_sys_wait4(p, v, retval) 149 struct proc *p; 150 void *v; 151 register_t *retval; 152{ 153 struct linux_sys_wait4_args /* { 154 syscallarg(int) pid; 155 syscallarg(int *) status; 156 syscallarg(int) options; 157 syscallarg(struct rusage *) rusage; 158 } */ *uap = v; 159 struct sys_wait4_args w4a; 160 int error, *status, tstat; 161 caddr_t sg; 162 163 if (SCARG(uap, status) != NULL) { 164 sg = stackgap_init(p->p_emul); 165 status = (int *) stackgap_alloc(&sg, sizeof status); 166 } else 167 status = NULL; 168 169 SCARG(&w4a, pid) = SCARG(uap, pid); 170 SCARG(&w4a, status) = status; 171 SCARG(&w4a, options) = SCARG(uap, options); 172 SCARG(&w4a, rusage) = SCARG(uap, rusage); 173 174 if ((error = sys_wait4(p, &w4a, retval))) 175 return error; 176 177 p->p_siglist &= ~sigmask(SIGCHLD); 178 179 if (status != NULL) { 180 if ((error = copyin(status, &tstat, sizeof tstat))) 181 return error; 182 183 bsd_to_linux_wstat(&tstat); 184 185 return copyout(&tstat, SCARG(uap, status), sizeof tstat); 186 } 187 188 return 0; 189} 190 191/* 192 * This is the old brk(2) call. I don't think anything in the Linux 193 * world uses this anymore 194 */ 195int 196linux_sys_break(p, v, retval) 197 struct proc *p; 198 void *v; 199 register_t *retval; 200{ 201 struct linux_sys_brk_args /* { 202 syscallarg(char *) nsize; 203 } */ *uap = v; 204 205 return ENOSYS; 206} 207 208/* 209 * Linux brk(2). The check if the new address is >= the old one is 210 * done in the kernel in Linux. NetBSD does it in the library. 211 */ 212int 213linux_sys_brk(p, v, retval) 214 struct proc *p; 215 void *v; 216 register_t *retval; 217{ 218 struct linux_sys_brk_args /* { 219 syscallarg(char *) nsize; 220 } */ *uap = v; 221 char *nbrk = SCARG(uap, nsize); 222 struct sys_obreak_args oba; 223 struct vmspace *vm = p->p_vmspace; 224 int error = 0; 225 caddr_t oldbrk, newbrk; 226 227 oldbrk = vm->vm_daddr + ctob(vm->vm_dsize); 228 /* 229 * XXX inconsistent.. Linux always returns at least the old 230 * brk value, but it will be page-aligned if this fails, 231 * and possibly not page aligned if it succeeds (the user 232 * supplied pointer is returned). 233 */ 234 SCARG(&oba, nsize) = nbrk; 235 236 if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0) 237 retval[0] = (register_t)nbrk; 238 else 239 retval[0] = (register_t)oldbrk; 240 241 return 0; 242} 243 244/* 245 * I wonder why Linux has gettimeofday() _and_ time().. Still, we 246 * need to deal with it. 247 */ 248int 249linux_sys_time(p, v, retval) 250 struct proc *p; 251 void *v; 252 register_t *retval; 253{ 254 struct linux_sys_time_args /* { 255 linux_time_t *t; 256 } */ *uap = v; 257 struct timeval atv; 258 linux_time_t tt; 259 int error; 260 261 microtime(&atv); 262 263 tt = atv.tv_sec; 264 if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt))) 265 return error; 266 267 retval[0] = tt; 268 return 0; 269} 270 271/* 272 * Convert BSD statfs structure to Linux statfs structure. 273 * The Linux structure has less fields, and it also wants 274 * the length of a name in a dir entry in a field, which 275 * we fake (probably the wrong way). 276 */ 277static void 278bsd_to_linux_statfs(bsp, lsp) 279 struct statfs *bsp; 280 struct linux_statfs *lsp; 281{ 282 283 lsp->l_ftype = bsp->f_type; 284 lsp->l_fbsize = bsp->f_bsize; 285 lsp->l_fblocks = bsp->f_blocks; 286 lsp->l_fbfree = bsp->f_bfree; 287 lsp->l_fbavail = bsp->f_bavail; 288 lsp->l_ffiles = bsp->f_files; 289 lsp->l_fffree = bsp->f_ffree; 290 lsp->l_ffsid.val[0] = bsp->f_fsid.val[0]; 291 lsp->l_ffsid.val[1] = bsp->f_fsid.val[1]; 292 lsp->l_fnamelen = MAXNAMLEN; /* XXX */ 293} 294 295/* 296 * Implement the fs stat functions. Straightforward. 297 */ 298int 299linux_sys_statfs(p, v, retval) 300 struct proc *p; 301 void *v; 302 register_t *retval; 303{ 304 struct linux_sys_statfs_args /* { 305 syscallarg(char *) path; 306 syscallarg(struct linux_statfs *) sp; 307 } */ *uap = v; 308 struct statfs btmp, *bsp; 309 struct linux_statfs ltmp; 310 struct sys_statfs_args bsa; 311 caddr_t sg; 312 int error; 313 314 sg = stackgap_init(p->p_emul); 315 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs)); 316 317 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 318 319 SCARG(&bsa, path) = SCARG(uap, path); 320 SCARG(&bsa, buf) = bsp; 321 322 if ((error = sys_statfs(p, &bsa, retval))) 323 return error; 324 325 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp))) 326 return error; 327 328 bsd_to_linux_statfs(&btmp, <mp); 329 330 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp); 331} 332 333int 334linux_sys_fstatfs(p, v, retval) 335 struct proc *p; 336 void *v; 337 register_t *retval; 338{ 339 struct linux_sys_fstatfs_args /* { 340 syscallarg(int) fd; 341 syscallarg(struct linux_statfs *) sp; 342 } */ *uap = v; 343 struct statfs btmp, *bsp; 344 struct linux_statfs ltmp; 345 struct sys_fstatfs_args bsa; 346 caddr_t sg; 347 int error; 348 349 sg = stackgap_init(p->p_emul); 350 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs)); 351 352 SCARG(&bsa, fd) = SCARG(uap, fd); 353 SCARG(&bsa, buf) = bsp; 354 355 if ((error = sys_fstatfs(p, &bsa, retval))) 356 return error; 357 358 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp))) 359 return error; 360 361 bsd_to_linux_statfs(&btmp, <mp); 362 363 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp); 364} 365 366/* 367 * uname(). Just copy the info from the various strings stored in the 368 * kernel, and put it in the Linux utsname structure. That structure 369 * is almost the same as the NetBSD one, only it has fields 65 characters 370 * long, and an extra domainname field. 371 */ 372int 373linux_sys_uname(p, v, retval) 374 struct proc *p; 375 void *v; 376 register_t *retval; 377{ 378 struct linux_sys_uname_args /* { 379 syscallarg(struct linux_utsname *) up; 380 } */ *uap = v; 381 extern char ostype[], hostname[], osrelease[], version[], machine[], 382 domainname[]; 383 struct linux_utsname luts; 384 int len; 385 char *cp; 386 387 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname)); 388 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename)); 389 strncpy(luts.l_release, osrelease, sizeof(luts.l_release)); 390 strncpy(luts.l_version, version, sizeof(luts.l_version)); 391 strncpy(luts.l_machine, machine, sizeof(luts.l_machine)); 392 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname)); 393 394 /* This part taken from the the uname() in libc */ 395 len = sizeof(luts.l_version); 396 for (cp = luts.l_version; len--; ++cp) 397 if (*cp == '\n' || *cp == '\t') 398 if (len > 1) 399 *cp = ' '; 400 else 401 *cp = '\0'; 402 403 return copyout(&luts, SCARG(uap, up), sizeof(luts)); 404} 405 406int 407linux_sys_olduname(p, v, retval) 408 struct proc *p; 409 void *v; 410 register_t *retval; 411{ 412 struct linux_sys_uname_args /* { 413 syscallarg(struct linux_oldutsname *) up; 414 } */ *uap = v; 415 extern char ostype[], hostname[], osrelease[], version[], machine[]; 416 struct linux_oldutsname luts; 417 int len; 418 char *cp; 419 420 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname)); 421 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename)); 422 strncpy(luts.l_release, osrelease, sizeof(luts.l_release)); 423 strncpy(luts.l_version, version, sizeof(luts.l_version)); 424 strncpy(luts.l_machine, machine, sizeof(luts.l_machine)); 425 426 /* This part taken from the the uname() in libc */ 427 len = sizeof(luts.l_version); 428 for (cp = luts.l_version; len--; ++cp) 429 if (*cp == '\n' || *cp == '\t') 430 if (len > 1) 431 *cp = ' '; 432 else 433 *cp = '\0'; 434 435 return copyout(&luts, SCARG(uap, up), sizeof(luts)); 436} 437 438int 439linux_sys_oldolduname(p, v, retval) 440 struct proc *p; 441 void *v; 442 register_t *retval; 443{ 444 struct linux_sys_uname_args /* { 445 syscallarg(struct linux_oldoldutsname *) up; 446 } */ *uap = v; 447 extern char ostype[], hostname[], osrelease[], version[], machine[]; 448 struct linux_oldoldutsname luts; 449 int len; 450 char *cp; 451 452 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname)); 453 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename)); 454 strncpy(luts.l_release, osrelease, sizeof(luts.l_release)); 455 strncpy(luts.l_version, version, sizeof(luts.l_version)); 456 strncpy(luts.l_machine, machine, sizeof(luts.l_machine)); 457 458 /* This part taken from the the uname() in libc */ 459 len = sizeof(luts.l_version); 460 for (cp = luts.l_version; len--; ++cp) 461 if (*cp == '\n' || *cp == '\t') 462 if (len > 1) 463 *cp = ' '; 464 else 465 *cp = '\0'; 466 467 return copyout(&luts, SCARG(uap, up), sizeof(luts)); 468} 469 470/* 471 * Linux wants to pass everything to a syscall in registers. However, 472 * mmap() has 6 of them. Oops: out of register error. They just pass 473 * everything in a structure. 474 */ 475int 476linux_sys_mmap(p, v, retval) 477 struct proc *p; 478 void *v; 479 register_t *retval; 480{ 481 struct linux_sys_mmap_args /* { 482 syscallarg(struct linux_mmap *) lmp; 483 } */ *uap = v; 484 struct linux_mmap lmap; 485 struct sys_mmap_args cma; 486 int error, flags; 487 488 if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap))) 489 return error; 490 491 flags = 0; 492 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED); 493 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE); 494 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED); 495 flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON); 496 497 SCARG(&cma,addr) = lmap.lm_addr; 498 SCARG(&cma,len) = lmap.lm_len; 499 SCARG(&cma,prot) = lmap.lm_prot; 500 SCARG(&cma,flags) = flags; 501 SCARG(&cma,fd) = lmap.lm_fd; 502 SCARG(&cma,pad) = 0; 503 SCARG(&cma,pos) = lmap.lm_pos; 504 505 return sys_mmap(p, &cma, retval); 506} 507 508int 509linux_sys_msync(p, v, retval) 510 struct proc *p; 511 void *v; 512 register_t *retval; 513{ 514 struct linux_sys_msync_args /* { 515 syscallarg(caddr_t) addr; 516 syscallarg(int) len; 517 syscallarg(int) fl; 518 } */ *uap = v; 519 520 struct sys_msync_args bma; 521 522 /* flags are ignored */ 523 SCARG(&bma, addr) = SCARG(uap, addr); 524 SCARG(&bma, len) = SCARG(uap, len); 525 526 return sys_msync(p, &bma, retval); 527} 528 529/* 530 * This code is partly stolen from src/lib/libc/compat-43/times.c 531 * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here 532 */ 533 534#define CLK_TCK 100 535#define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 536 537int 538linux_sys_times(p, v, retval) 539 struct proc *p; 540 void *v; 541 register_t *retval; 542{ 543 struct linux_sys_times_args /* { 544 syscallarg(struct times *) tms; 545 } */ *uap = v; 546 struct timeval t; 547 struct linux_tms ltms; 548 struct rusage ru; 549 int error, s; 550 551 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL); 552 ltms.ltms_utime = CONVTCK(ru.ru_utime); 553 ltms.ltms_stime = CONVTCK(ru.ru_stime); 554 555 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime); 556 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime); 557 558 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms))) 559 return error; 560 561 s = splclock(); 562 timersub(&time, &boottime, &t); 563 splx(s); 564 565 retval[0] = ((linux_clock_t)(CONVTCK(t))); 566 return 0; 567} 568 569/* 570 * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1]. 571 * Linux directly passes the pointer. 572 */ 573int 574linux_sys_pipe(p, v, retval) 575 struct proc *p; 576 void *v; 577 register_t *retval; 578{ 579 struct linux_sys_pipe_args /* { 580 syscallarg(int *) pfds; 581 } */ *uap = v; 582 int error; 583 584 if ((error = sys_pipe(p, 0, retval))) 585 return error; 586 587 /* Assumes register_t is an int */ 588 589 if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int)))) 590 return error; 591 592 retval[0] = 0; 593 return 0; 594} 595 596/* 597 * Alarm. This is a libc call which uses setitimer(2) in NetBSD. 598 * Fiddle with the timers to make it work. 599 */ 600int 601linux_sys_alarm(p, v, retval) 602 struct proc *p; 603 void *v; 604 register_t *retval; 605{ 606 struct linux_sys_alarm_args /* { 607 syscallarg(unsigned int) secs; 608 } */ *uap = v; 609 int error, s; 610 struct itimerval *itp, it; 611 612 itp = &p->p_realtimer; 613 s = splclock(); 614 /* 615 * Clear any pending timer alarms. 616 */ 617 untimeout(realitexpire, p); 618 timerclear(&itp->it_interval); 619 if (timerisset(&itp->it_value) && 620 timercmp(&itp->it_value, &time, >)) 621 timersub(&itp->it_value, &time, &itp->it_value); 622 /* 623 * Return how many seconds were left (rounded up) 624 */ 625 retval[0] = itp->it_value.tv_sec; 626 if (itp->it_value.tv_usec) 627 retval[0]++; 628 629 /* 630 * alarm(0) just resets the timer. 631 */ 632 if (SCARG(uap, secs) == 0) { 633 timerclear(&itp->it_value); 634 splx(s); 635 return 0; 636 } 637 638 /* 639 * Check the new alarm time for sanity, and set it. 640 */ 641 timerclear(&it.it_interval); 642 it.it_value.tv_sec = SCARG(uap, secs); 643 it.it_value.tv_usec = 0; 644 if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) { 645 splx(s); 646 return (EINVAL); 647 } 648 649 if (timerisset(&it.it_value)) { 650 timeradd(&it.it_value, &time, &it.it_value); 651 timeout(realitexpire, p, hzto(&it.it_value)); 652 } 653 p->p_realtimer = it; 654 splx(s); 655 656 return 0; 657} 658 659/* 660 * utime(). Do conversion to things that utimes() understands, 661 * and pass it on. 662 */ 663int 664linux_sys_utime(p, v, retval) 665 struct proc *p; 666 void *v; 667 register_t *retval; 668{ 669 struct linux_sys_utime_args /* { 670 syscallarg(char *) path; 671 syscallarg(struct linux_utimbuf *)times; 672 } */ *uap = v; 673 caddr_t sg; 674 int error; 675 struct sys_utimes_args ua; 676 struct timeval tv[2], *tvp; 677 struct linux_utimbuf lut; 678 679 sg = stackgap_init(p->p_emul); 680 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 681 682 SCARG(&ua, path) = SCARG(uap, path); 683 684 if (SCARG(uap, times) != NULL) { 685 if ((error = copyin(SCARG(uap, times), &lut, sizeof lut))) 686 return error; 687 tv[0].tv_usec = tv[1].tv_usec = 0; 688 tv[0].tv_sec = lut.l_actime; 689 tv[1].tv_sec = lut.l_modtime; 690 tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv)); 691 if ((error = copyout(tv, tvp, sizeof tv))) 692 return error; 693 SCARG(&ua, tptr) = tvp; 694 } 695 else 696 SCARG(&ua, tptr) = NULL; 697 698 return sys_utimes(p, uap, retval); 699} 700 701/* 702 * The old Linux readdir was only able to read one entry at a time, 703 * even though it had a 'count' argument. In fact, the emulation 704 * of the old call was better than the original, because it did handle 705 * the count arg properly. Don't bother with it anymore now, and use 706 * it to distinguish between old and new. The difference is that the 707 * newer one actually does multiple entries, and the reclen field 708 * really is the reclen, not the namelength. 709 */ 710int 711linux_sys_readdir(p, v, retval) 712 struct proc *p; 713 void *v; 714 register_t *retval; 715{ 716 struct linux_sys_readdir_args /* { 717 syscallarg(int) fd; 718 syscallarg(struct linux_dirent *) dent; 719 syscallarg(unsigned int) count; 720 } */ *uap = v; 721 722 SCARG(uap, count) = 1; 723 return linux_sys_getdents(p, uap, retval); 724} 725 726/* 727 * Linux 'readdir' call. This code is mostly taken from the 728 * SunOS getdents call (see compat/sunos/sunos_misc.c), though 729 * an attempt has been made to keep it a little cleaner (failing 730 * miserably, because of the cruft needed if count 1 is passed). 731 * 732 * The d_off field should contain the offset of the next valid entry, 733 * but in Linux it has the offset of the entry itself. We emulate 734 * that bug here. 735 * 736 * Read in BSD-style entries, convert them, and copy them out. 737 * 738 * Note that this doesn't handle union-mounted filesystems. 739 */ 740int 741linux_sys_getdents(p, v, retval) 742 struct proc *p; 743 void *v; 744 register_t *retval; 745{ 746 struct linux_sys_readdir_args /* { 747 syscallarg(int) fd; 748 syscallarg(caddr_t) dent; 749 syscallarg(unsigned int) count; 750 } */ *uap = v; 751 register struct dirent *bdp; 752 struct vnode *vp; 753 caddr_t inp, buf; /* BSD-format */ 754 int len, reclen; /* BSD-format */ 755 caddr_t outp; /* Linux-format */ 756 int resid, linux_reclen;/* Linux-format */ 757 struct file *fp; 758 struct uio auio; 759 struct iovec aiov; 760 struct linux_dirent idb; 761 off_t off; /* true file offset */ 762 int buflen, error, eofflag, nbytes, oldcall; 763 struct vattr va; 764 u_long *cookiebuf, *cookie; 765 int ncookies; 766 767 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 768 return (error); 769 770 if ((fp->f_flag & FREAD) == 0) 771 return (EBADF); 772 773 vp = (struct vnode *)fp->f_data; 774 775 if (vp->v_type != VDIR) /* XXX vnode readdir op should do this */ 776 return (EINVAL); 777 778 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) 779 return error; 780 781 nbytes = SCARG(uap, count); 782 if (nbytes == 1) { /* emulating old, broken behaviour */ 783 nbytes = sizeof (struct linux_dirent); 784 buflen = max(va.va_blocksize, nbytes); 785 oldcall = 1; 786 } else { 787 buflen = min(MAXBSIZE, nbytes); 788 oldcall = 0; 789 } 790 buf = malloc(buflen, M_TEMP, M_WAITOK); 791 ncookies = buflen / 16; 792 cookiebuf = malloc(ncookies * sizeof(*cookiebuf), M_TEMP, M_WAITOK); 793 VOP_LOCK(vp); 794 off = fp->f_offset; 795again: 796 aiov.iov_base = buf; 797 aiov.iov_len = buflen; 798 auio.uio_iov = &aiov; 799 auio.uio_iovcnt = 1; 800 auio.uio_rw = UIO_READ; 801 auio.uio_segflg = UIO_SYSSPACE; 802 auio.uio_procp = p; 803 auio.uio_resid = buflen; 804 auio.uio_offset = off; 805 /* 806 * First we read into the malloc'ed buffer, then 807 * we massage it into user space, one record at a time. 808 */ 809 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookiebuf, 810 ncookies); 811 if (error) 812 goto out; 813 814 inp = buf; 815 outp = SCARG(uap, dent); 816 resid = nbytes; 817 if ((len = buflen - auio.uio_resid) == 0) 818 goto eof; 819 820 for (cookie = cookiebuf; len > 0; len -= reclen) { 821 bdp = (struct dirent *)inp; 822 reclen = bdp->d_reclen; 823 if (reclen & 3) 824 panic("linux_readdir"); 825 if (bdp->d_fileno == 0) { 826 inp += reclen; /* it is a hole; squish it out */ 827 off = *cookie++; 828 continue; 829 } 830 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen); 831 if (reclen > len || resid < linux_reclen) { 832 /* entry too big for buffer, so just stop */ 833 outp++; 834 off = *cookie++; 835 break; 836 } 837 /* 838 * Massage in place to make a Linux-shaped dirent (otherwise 839 * we have to worry about touching user memory outside of 840 * the copyout() call). 841 */ 842 idb.d_ino = (linux_ino_t)bdp->d_fileno; 843 /* 844 * The old readdir() call misuses the offset and reclen fields. 845 */ 846 if (oldcall) { 847 idb.d_off = (linux_off_t)linux_reclen; 848 idb.d_reclen = (u_short)bdp->d_namlen; 849 } else { 850 idb.d_off = (linux_off_t)off; 851 idb.d_reclen = (u_short)linux_reclen; 852 } 853 strcpy(idb.d_name, bdp->d_name); 854 if ((error = copyout((caddr_t)&idb, outp, linux_reclen))) 855 goto out; 856 /* advance past this real entry */ 857 inp += reclen; 858 off = *cookie++; /* each entry points to itself */ 859 /* advance output past Linux-shaped entry */ 860 outp += linux_reclen; 861 resid -= linux_reclen; 862 if (oldcall) 863 break; 864 } 865 866 /* if we squished out the whole block, try again */ 867 if (outp == SCARG(uap, dent)) 868 goto again; 869 fp->f_offset = off; /* update the vnode offset */ 870 871 if (oldcall) 872 nbytes = resid + linux_reclen; 873 874eof: 875 *retval = nbytes - resid; 876out: 877 VOP_UNLOCK(vp); 878 free(cookiebuf, M_TEMP); 879 free(buf, M_TEMP); 880 return error; 881} 882 883/* 884 * Not sure why the arguments to this older version of select() were put 885 * into a structure, because there are 5, and that can all be handled 886 * in registers on the i386 like Linux wants to. 887 */ 888int 889linux_sys_oldselect(p, v, retval) 890 struct proc *p; 891 void *v; 892 register_t *retval; 893{ 894 struct linux_sys_oldselect_args /* { 895 syscallarg(struct linux_select *) lsp; 896 } */ *uap = v; 897 struct linux_select ls; 898 int error; 899 900 if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls)))) 901 return error; 902 903 return linux_select1(p, retval, ls.nfds, ls.readfds, ls.writefds, 904 ls.exceptfds, ls.timeout); 905} 906 907/* 908 * Even when just using registers to pass arguments to syscalls you can 909 * have 5 of them on the i386. So this newer version of select() does 910 * this. 911 */ 912int 913linux_sys_select(p, v, retval) 914 struct proc *p; 915 void *v; 916 register_t *retval; 917{ 918 struct linux_sys_select_args /* { 919 syscallarg(int) nfds; 920 syscallarg(fd_set *) readfds; 921 syscallarg(fd_set *) writefds; 922 syscallarg(fd_set *) exceptfds; 923 syscallarg(struct timeval *) timeout; 924 } */ *uap = v; 925 926 return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds), 927 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout)); 928} 929 930/* 931 * Common code for the old and new versions of select(). A couple of 932 * things are important: 933 * 1) return the amount of time left in the 'timeout' parameter 934 * 2) select never returns ERESTART on Linux, always return EINTR 935 */ 936int 937linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout) 938 struct proc *p; 939 register_t *retval; 940 int nfds; 941 fd_set *readfds, *writefds, *exceptfds; 942 struct timeval *timeout; 943{ 944 struct sys_select_args bsa; 945 struct timeval tv0, tv1, utv, *tvp; 946 caddr_t sg; 947 int error; 948 949 SCARG(&bsa, nd) = nfds; 950 SCARG(&bsa, in) = readfds; 951 SCARG(&bsa, ou) = writefds; 952 SCARG(&bsa, ex) = exceptfds; 953 SCARG(&bsa, tv) = timeout; 954 955 /* 956 * Store current time for computation of the amount of 957 * time left. 958 */ 959 if (timeout) { 960 if ((error = copyin(timeout, &utv, sizeof(utv)))) 961 return error; 962 if (itimerfix(&utv)) { 963 /* 964 * The timeval was invalid. Convert it to something 965 * valid that will act as it does under Linux. 966 */ 967 sg = stackgap_init(p->p_emul); 968 tvp = stackgap_alloc(&sg, sizeof(utv)); 969 utv.tv_sec += utv.tv_usec / 1000000; 970 utv.tv_usec %= 1000000; 971 if (utv.tv_usec < 0) { 972 utv.tv_sec -= 1; 973 utv.tv_usec += 1000000; 974 } 975 if (utv.tv_sec < 0) 976 timerclear(&utv); 977 if ((error = copyout(&utv, tvp, sizeof(utv)))) 978 return error; 979 SCARG(&bsa, tv) = tvp; 980 } 981 microtime(&tv0); 982 } 983 984 error = sys_select(p, &bsa, retval); 985 if (error) { 986 /* 987 * See fs/select.c in the Linux kernel. Without this, 988 * Maelstrom doesn't work. 989 */ 990 if (error == ERESTART) 991 error = EINTR; 992 return error; 993 } 994 995 if (timeout) { 996 if (*retval) { 997 /* 998 * Compute how much time was left of the timeout, 999 * by subtracting the current time and the time 1000 * before we started the call, and subtracting 1001 * that result from the user-supplied value. 1002 */ 1003 microtime(&tv1); 1004 timersub(&tv1, &tv0, &tv1); 1005 timersub(&utv, &tv1, &utv); 1006 if (utv.tv_sec < 0) 1007 timerclear(&utv); 1008 } else 1009 timerclear(&utv); 1010 if ((error = copyout(&utv, timeout, sizeof(utv)))) 1011 return error; 1012 } 1013 1014 return 0; 1015} 1016 1017/* 1018 * Get the process group of a certain process. Look it up 1019 * and return the value. 1020 */ 1021int 1022linux_sys_getpgid(p, v, retval) 1023 struct proc *p; 1024 void *v; 1025 register_t *retval; 1026{ 1027 struct linux_sys_getpgid_args /* { 1028 syscallarg(int) pid; 1029 } */ *uap = v; 1030 struct proc *targp; 1031 1032 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) 1033 if ((targp = pfind(SCARG(uap, pid))) == 0) 1034 return ESRCH; 1035 else 1036 targp = p; 1037 1038 retval[0] = targp->p_pgid; 1039 return 0; 1040} 1041 1042/* 1043 * Set the 'personality' (emulation mode) for the current process. Only 1044 * accept the Linux personality here (0). This call is needed because 1045 * the Linux ELF crt0 issues it in an ugly kludge to make sure that 1046 * ELF binaries run in Linux mode, not SVR4 mode. 1047 */ 1048int 1049linux_sys_personality(p, v, retval) 1050 struct proc *p; 1051 void *v; 1052 register_t *retval; 1053{ 1054 struct linux_sys_personality_args /* { 1055 syscallarg(int) per; 1056 } */ *uap = v; 1057 1058 if (SCARG(uap, per) != 0) 1059 return EINVAL; 1060 retval[0] = 0; 1061 return 0; 1062} 1063 1064/* 1065 * The calls are here because of type conversions. 1066 */ 1067int 1068linux_sys_setreuid(p, v, retval) 1069 struct proc *p; 1070 void *v; 1071 register_t *retval; 1072{ 1073 struct linux_sys_setreuid_args /* { 1074 syscallarg(int) ruid; 1075 syscallarg(int) euid; 1076 } */ *uap = v; 1077 struct compat_43_sys_setreuid_args bsa; 1078 1079 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ? 1080 (uid_t)-1 : SCARG(uap, ruid); 1081 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ? 1082 (uid_t)-1 : SCARG(uap, euid); 1083 1084 return compat_43_sys_setreuid(p, &bsa, retval); 1085} 1086 1087int 1088linux_sys_setregid(p, v, retval) 1089 struct proc *p; 1090 void *v; 1091 register_t *retval; 1092{ 1093 struct linux_sys_setregid_args /* { 1094 syscallarg(int) rgid; 1095 syscallarg(int) egid; 1096 } */ *uap = v; 1097 struct compat_43_sys_setregid_args bsa; 1098 1099 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ? 1100 (uid_t)-1 : SCARG(uap, rgid); 1101 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ? 1102 (uid_t)-1 : SCARG(uap, egid); 1103 1104 return compat_43_sys_setregid(p, &bsa, retval); 1105} 1106