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