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