1 /* $NetBSD: sunos_misc.c,v 1.130 2004/09/17 14:11:24 skrll Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratory. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)sunos_misc.c 8.1 (Berkeley) 6/18/93 41 * 42 * Header: sunos_misc.c,v 1.16 93/04/07 02:46:27 torek Exp 43 */ 44 45 /* 46 * SunOS compatibility module. 47 * 48 * SunOS system calls that are implemented differently in BSD are 49 * handled here. 50 */ 51 52 #include <sys/cdefs.h> 53 __KERNEL_RCSID(0, "$NetBSD: sunos_misc.c,v 1.130 2004/09/17 14:11:24 skrll Exp $"); 54 55 #if defined(_KERNEL_OPT) 56 #include "opt_nfsserver.h" 57 #include "fs_nfs.h" 58 #endif 59 60 #include <sys/param.h> 61 #include <sys/systm.h> 62 #include <sys/namei.h> 63 #include <sys/proc.h> 64 #include <sys/dirent.h> 65 #include <sys/file.h> 66 #include <sys/stat.h> 67 #include <sys/filedesc.h> 68 #include <sys/ioctl.h> 69 #include <sys/kernel.h> 70 #include <sys/reboot.h> 71 #include <sys/malloc.h> 72 #include <sys/mbuf.h> 73 #include <sys/mman.h> 74 #include <sys/mount.h> 75 #include <sys/ptrace.h> 76 #include <sys/resource.h> 77 #include <sys/resourcevar.h> 78 #include <sys/signal.h> 79 #include <sys/signalvar.h> 80 #include <sys/socket.h> 81 #include <sys/tty.h> 82 #include <sys/vnode.h> 83 #include <sys/uio.h> 84 #include <sys/wait.h> 85 #include <sys/utsname.h> 86 #include <sys/unistd.h> 87 #include <sys/sa.h> 88 #include <sys/syscallargs.h> 89 #include <sys/conf.h> 90 #include <sys/socketvar.h> 91 #include <sys/exec.h> 92 #include <sys/swap.h> 93 94 #include <compat/sunos/sunos.h> 95 #include <compat/sunos/sunos_syscallargs.h> 96 #include <compat/common/compat_util.h> 97 #include <compat/sunos/sunos_dirent.h> 98 99 #include <netinet/in.h> 100 101 #include <miscfs/specfs/specdev.h> 102 103 #include <nfs/rpcv2.h> 104 #include <nfs/nfsproto.h> 105 #include <nfs/nfs.h> 106 #include <nfs/nfsmount.h> 107 108 static int sunstatfs __P((struct statvfs *, caddr_t)); 109 110 int 111 sunos_sys_stime(l, v, retval) 112 struct lwp *l; 113 void *v; 114 register_t *retval; 115 { 116 struct sunos_sys_stime_args *uap = v; 117 struct sys_settimeofday_args ap; 118 struct proc *p = l->l_proc; 119 caddr_t sg = stackgap_init(p, 0); 120 struct timeval tv, *sgtvp; 121 int error; 122 123 error = copyin(SCARG(uap, tp), &tv.tv_sec, sizeof(tv.tv_sec)); 124 if (error) 125 return error; 126 tv.tv_usec = 0; 127 128 SCARG(&ap, tv) = sgtvp = stackgap_alloc(p, &sg, sizeof(struct timeval)); 129 SCARG(&ap, tzp) = NULL; 130 131 error = copyout(&tv, sgtvp, sizeof(struct timeval)); 132 if (error) 133 return error; 134 135 return sys_settimeofday(l, &ap, retval); 136 } 137 138 int 139 sunos_sys_wait4(l, v, retval) 140 struct lwp *l; 141 void *v; 142 register_t *retval; 143 { 144 struct sunos_sys_wait4_args *uap = v; 145 if (SCARG(uap, pid) == 0) 146 SCARG(uap, pid) = WAIT_ANY; 147 return (sys_wait4(l, uap, retval)); 148 } 149 150 int 151 sunos_sys_creat(l, v, retval) 152 struct lwp *l; 153 void *v; 154 register_t *retval; 155 { 156 struct sunos_sys_creat_args *uap = v; 157 struct proc *p = l->l_proc; 158 struct sys_open_args ouap; 159 160 caddr_t sg = stackgap_init(p, 0); 161 CHECK_ALT_CREAT(p, &sg, SCARG(uap, path)); 162 163 SCARG(&ouap, path) = SCARG(uap, path); 164 SCARG(&ouap, flags) = O_WRONLY | O_CREAT | O_TRUNC; 165 SCARG(&ouap, mode) = SCARG(uap, mode); 166 167 return (sys_open(l, &ouap, retval)); 168 } 169 170 int 171 sunos_sys_access(l, v, retval) 172 struct lwp *l; 173 void *v; 174 register_t *retval; 175 { 176 struct sunos_sys_access_args *uap = v; 177 struct proc *p = l->l_proc; 178 caddr_t sg = stackgap_init(p, 0); 179 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 180 181 return (sys_access(l, uap, retval)); 182 } 183 184 int 185 sunos_sys_stat(l, v, retval) 186 struct lwp *l; 187 void *v; 188 register_t *retval; 189 { 190 struct sunos_sys_stat_args *uap = v; 191 struct proc *p = l->l_proc; 192 caddr_t sg = stackgap_init(p, 0); 193 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 194 195 return (compat_43_sys_stat(l, uap, retval)); 196 } 197 198 int 199 sunos_sys_lstat(l, v, retval) 200 struct lwp *l; 201 void *v; 202 register_t *retval; 203 { 204 struct sunos_sys_lstat_args *uap = v; 205 struct proc *p = l->l_proc; 206 caddr_t sg = stackgap_init(p, 0); 207 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 208 209 return (compat_43_sys_lstat(l, uap, retval)); 210 } 211 212 int 213 sunos_sys_execv(l, v, retval) 214 struct lwp *l; 215 void *v; 216 register_t *retval; 217 { 218 struct sunos_sys_execv_args /* { 219 syscallarg(const char *) path; 220 syscallarg(char **) argv; 221 } */ *uap = v; 222 struct proc *p = l->l_proc; 223 struct sys_execve_args ap; 224 caddr_t sg; 225 226 sg = stackgap_init(p, 0); 227 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 228 229 SCARG(&ap, path) = SCARG(uap, path); 230 SCARG(&ap, argp) = SCARG(uap, argp); 231 SCARG(&ap, envp) = NULL; 232 233 return (sys_execve(l, &ap, retval)); 234 } 235 236 int 237 sunos_sys_execve(l, v, retval) 238 struct lwp *l; 239 void *v; 240 register_t *retval; 241 { 242 struct sunos_sys_execve_args /* { 243 syscallarg(const char *) path; 244 syscallarg(char **) argv; 245 syscallarg(char **) envp; 246 } */ *uap = v; 247 struct sys_execve_args ap; 248 struct proc *p = l->l_proc; 249 caddr_t sg; 250 251 sg = stackgap_init(p, 0); 252 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 253 254 SCARG(&ap, path) = SCARG(uap, path); 255 SCARG(&ap, argp) = SCARG(uap, argp); 256 SCARG(&ap, envp) = SCARG(uap, envp); 257 258 return (sys_execve(l, &ap, retval)); 259 } 260 261 int 262 sunos_sys_omsync(l, v, retval) 263 struct lwp *l; 264 void *v; 265 register_t *retval; 266 { 267 struct sunos_sys_omsync_args *uap = v; 268 struct sys___msync13_args ouap; 269 270 SCARG(&ouap, addr) = SCARG(uap, addr); 271 SCARG(&ouap, len) = SCARG(uap, len); 272 SCARG(&ouap, flags) = SCARG(uap, flags); 273 274 return (sys___msync13(l, &ouap, retval)); 275 } 276 277 int 278 sunos_sys_unmount(l, v, retval) 279 struct lwp *l; 280 void *v; 281 register_t *retval; 282 { 283 struct sunos_sys_unmount_args *uap = v; 284 struct sys_unmount_args ouap; 285 286 SCARG(&ouap, path) = SCARG(uap, path); 287 SCARG(&ouap, flags) = 0; 288 289 return (sys_unmount(l, &ouap, retval)); 290 } 291 292 /* 293 * Conversion table for SunOS NFS mount flags. 294 */ 295 static struct { 296 int sun_flg; 297 int bsd_flg; 298 } sunnfs_flgtab[] = { 299 { SUNNFS_SOFT, NFSMNT_SOFT }, 300 { SUNNFS_WSIZE, NFSMNT_WSIZE }, 301 { SUNNFS_RSIZE, NFSMNT_RSIZE }, 302 { SUNNFS_TIMEO, NFSMNT_TIMEO }, 303 { SUNNFS_RETRANS, NFSMNT_RETRANS }, 304 { SUNNFS_HOSTNAME, 0 }, /* Ignored */ 305 { SUNNFS_INT, NFSMNT_INT }, 306 { SUNNFS_NOAC, 0 }, /* Ignored */ 307 { SUNNFS_ACREGMIN, 0 }, /* Ignored */ 308 { SUNNFS_ACREGMAX, 0 }, /* Ignored */ 309 { SUNNFS_ACDIRMIN, 0 }, /* Ignored */ 310 { SUNNFS_ACDIRMAX, 0 }, /* Ignored */ 311 { SUNNFS_SECURE, 0 }, /* Ignored */ 312 { SUNNFS_NOCTO, 0 }, /* Ignored */ 313 { SUNNFS_POSIX, 0 } /* Ignored */ 314 }; 315 316 int 317 sunos_sys_mount(l, v, retval) 318 struct lwp *l; 319 void *v; 320 register_t *retval; 321 { 322 struct sunos_sys_mount_args *uap = v; 323 struct proc *p = l->l_proc; 324 int oflags = SCARG(uap, flags), nflags, error; 325 char fsname[MFSNAMELEN]; 326 caddr_t sg = stackgap_init(p, 0); 327 328 if (oflags & (SUNM_NOSUB | SUNM_SYS5)) 329 return (EINVAL); 330 if ((oflags & SUNM_NEWTYPE) == 0) 331 return (EINVAL); 332 nflags = 0; 333 if (oflags & SUNM_RDONLY) 334 nflags |= MNT_RDONLY; 335 if (oflags & SUNM_NOSUID) 336 nflags |= MNT_NOSUID; 337 if (oflags & SUNM_REMOUNT) 338 nflags |= MNT_UPDATE; 339 SCARG(uap, flags) = nflags; 340 341 error = copyinstr((caddr_t)SCARG(uap, type), fsname, 342 sizeof fsname, (size_t *)0); 343 if (error) 344 return (error); 345 346 if (strncmp(fsname, "4.2", sizeof fsname) == 0) { 347 SCARG(uap, type) = stackgap_alloc(p, &sg, sizeof("ffs")); 348 error = copyout("ffs", SCARG(uap, type), sizeof("ffs")); 349 if (error) 350 return (error); 351 } else if (strncmp(fsname, "nfs", sizeof fsname) == 0) { 352 struct sunos_nfs_args sna; 353 struct sockaddr_in sain; 354 struct nfs_args na; 355 struct sockaddr sa; 356 int n; 357 358 error = copyin(SCARG(uap, data), &sna, sizeof sna); 359 if (error) 360 return (error); 361 error = copyin(sna.addr, &sain, sizeof sain); 362 if (error) 363 return (error); 364 memcpy(&sa, &sain, sizeof sa); 365 sa.sa_len = sizeof(sain); 366 SCARG(uap, data) = stackgap_alloc(p, &sg, sizeof(na)); 367 na.version = NFS_ARGSVERSION; 368 na.addr = stackgap_alloc(p, &sg, sizeof(struct sockaddr)); 369 na.addrlen = sizeof(struct sockaddr); 370 na.sotype = SOCK_DGRAM; 371 na.proto = IPPROTO_UDP; 372 na.fh = (void *)sna.fh; 373 na.fhsize = NFSX_V2FH; 374 na.flags = 0; 375 n = sizeof(sunnfs_flgtab) / sizeof(sunnfs_flgtab[0]); 376 while (--n >= 0) 377 if (sna.flags & sunnfs_flgtab[n].sun_flg) 378 na.flags |= sunnfs_flgtab[n].bsd_flg; 379 na.wsize = sna.wsize; 380 na.rsize = sna.rsize; 381 if (na.flags & NFSMNT_RSIZE) { 382 na.flags |= NFSMNT_READDIRSIZE; 383 na.readdirsize = na.rsize; 384 } 385 na.timeo = sna.timeo; 386 na.retrans = sna.retrans; 387 na.hostname = (char *)(u_long)sna.hostname; 388 389 error = copyout(&sa, na.addr, sizeof sa); 390 if (error) 391 return (error); 392 error = copyout(&na, SCARG(uap, data), sizeof na); 393 if (error) 394 return (error); 395 } 396 return (sys_mount(l, (struct sys_mount_args *)uap, retval)); 397 } 398 399 #if defined(NFS) 400 int 401 async_daemon(l, v, retval) 402 struct lwp *l; 403 void *v; 404 register_t *retval; 405 { 406 struct sys_nfssvc_args ouap; 407 408 SCARG(&ouap, flag) = NFSSVC_BIOD; 409 SCARG(&ouap, argp) = NULL; 410 411 return (sys_nfssvc(l, &ouap, retval)); 412 } 413 #endif /* NFS */ 414 415 void native_to_sunos_sigset __P((const sigset_t *, int *)); 416 void sunos_to_native_sigset __P((const int, sigset_t *)); 417 418 __inline__ void 419 native_to_sunos_sigset(ss, mask) 420 const sigset_t *ss; 421 int *mask; 422 { 423 *mask = ss->__bits[0]; 424 } 425 426 __inline__ void 427 sunos_to_native_sigset(mask, ss) 428 const int mask; 429 sigset_t *ss; 430 { 431 432 ss->__bits[0] = mask; 433 ss->__bits[1] = 0; 434 ss->__bits[2] = 0; 435 ss->__bits[3] = 0; 436 } 437 438 int 439 sunos_sys_sigpending(l, v, retval) 440 struct lwp *l; 441 void *v; 442 register_t *retval; 443 { 444 struct sunos_sys_sigpending_args *uap = v; 445 sigset_t ss; 446 int mask; 447 448 sigpending1(l->l_proc, &ss); 449 native_to_sunos_sigset(&ss, &mask); 450 451 return (copyout((caddr_t)&mask, (caddr_t)SCARG(uap, mask), sizeof(int))); 452 } 453 454 int 455 sunos_sys_sigsuspend(l, v, retval) 456 struct lwp *l; 457 void *v; 458 register_t *retval; 459 { 460 struct sunos_sys_sigsuspend_args /* { 461 syscallarg(int) mask; 462 } */ *uap = v; 463 int mask; 464 sigset_t ss; 465 466 mask = SCARG(uap, mask); 467 sunos_to_native_sigset(mask, &ss); 468 return (sigsuspend1(l->l_proc, &ss)); 469 } 470 471 /* 472 * Read Sun-style directory entries. We suck them into kernel space so 473 * that they can be massaged before being copied out to user code. Like 474 * SunOS, we squish out `empty' entries. 475 * 476 * This is quite ugly, but what do you expect from compatibility code? 477 */ 478 int 479 sunos_sys_getdents(l, v, retval) 480 struct lwp *l; 481 void *v; 482 register_t *retval; 483 { 484 struct sunos_sys_getdents_args *uap = v; 485 struct proc *p = l->l_proc; 486 struct dirent *bdp; 487 struct vnode *vp; 488 caddr_t inp, buf; /* BSD-format */ 489 int len, reclen; /* BSD-format */ 490 caddr_t outp; /* Sun-format */ 491 int resid, sunos_reclen;/* Sun-format */ 492 struct file *fp; 493 struct uio auio; 494 struct iovec aiov; 495 struct sunos_dirent idb; 496 off_t off; /* true file offset */ 497 int buflen, error, eofflag; 498 off_t *cookiebuf, *cookie; 499 int ncookies; 500 501 /* getvnode() will use the descriptor for us */ 502 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 503 return (error); 504 505 if ((fp->f_flag & FREAD) == 0) { 506 error = EBADF; 507 goto out1; 508 } 509 510 vp = (struct vnode *)fp->f_data; 511 if (vp->v_type != VDIR) { 512 error = EINVAL; 513 goto out1; 514 } 515 516 buflen = min(MAXBSIZE, SCARG(uap, nbytes)); 517 buf = malloc(buflen, M_TEMP, M_WAITOK); 518 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 519 off = fp->f_offset; 520 again: 521 aiov.iov_base = buf; 522 aiov.iov_len = buflen; 523 auio.uio_iov = &aiov; 524 auio.uio_iovcnt = 1; 525 auio.uio_rw = UIO_READ; 526 auio.uio_segflg = UIO_SYSSPACE; 527 auio.uio_procp = NULL; 528 auio.uio_resid = buflen; 529 auio.uio_offset = off; 530 /* 531 * First we read into the malloc'ed buffer, then 532 * we massage it into user space, one record at a time. 533 */ 534 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf, 535 &ncookies); 536 if (error) 537 goto out; 538 539 inp = buf; 540 outp = SCARG(uap, buf); 541 resid = SCARG(uap, nbytes); 542 if ((len = buflen - auio.uio_resid) == 0) 543 goto eof; 544 545 for (cookie = cookiebuf; len > 0; len -= reclen) { 546 bdp = (struct dirent *)inp; 547 reclen = bdp->d_reclen; 548 if (reclen & 3) 549 panic("sunos_getdents"); 550 if ((*cookie >> 32) != 0) { 551 compat_offseterr(vp, "sunos_getdents"); 552 error = EINVAL; 553 goto out; 554 } 555 if (bdp->d_fileno == 0) { 556 inp += reclen; /* it is a hole; squish it out */ 557 off = *cookie++; 558 continue; 559 } 560 sunos_reclen = SUNOS_RECLEN(&idb, bdp->d_namlen); 561 if (reclen > len || resid < sunos_reclen) { 562 /* entry too big for buffer, so just stop */ 563 outp++; 564 break; 565 } 566 off = *cookie++; /* each entry points to next */ 567 /* 568 * Massage in place to make a Sun-shaped dirent (otherwise 569 * we have to worry about touching user memory outside of 570 * the copyout() call). 571 */ 572 idb.d_fileno = bdp->d_fileno; 573 idb.d_off = off; 574 idb.d_reclen = sunos_reclen; 575 idb.d_namlen = bdp->d_namlen; 576 strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name)); 577 if ((error = copyout((caddr_t)&idb, outp, sunos_reclen)) != 0) 578 goto out; 579 /* advance past this real entry */ 580 inp += reclen; 581 /* advance output past Sun-shaped entry */ 582 outp += sunos_reclen; 583 resid -= sunos_reclen; 584 } 585 586 /* if we squished out the whole block, try again */ 587 if (outp == SCARG(uap, buf)) 588 goto again; 589 fp->f_offset = off; /* update the vnode offset */ 590 591 eof: 592 *retval = SCARG(uap, nbytes) - resid; 593 out: 594 VOP_UNLOCK(vp, 0); 595 free(cookiebuf, M_TEMP); 596 free(buf, M_TEMP); 597 out1: 598 FILE_UNUSE(fp, p); 599 return (error); 600 } 601 602 #define SUNOS__MAP_NEW 0x80000000 /* if not, old mmap & cannot handle */ 603 604 int 605 sunos_sys_mmap(l, v, retval) 606 struct lwp *l; 607 void *v; 608 register_t *retval; 609 { 610 struct sunos_sys_mmap_args *uap = v; 611 struct sys_mmap_args ouap; 612 613 /* 614 * Verify the arguments. 615 */ 616 if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC)) 617 return (EINVAL); /* XXX still needed? */ 618 619 if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0) 620 return (EINVAL); 621 622 SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW; 623 SCARG(&ouap, addr) = SCARG(uap, addr); 624 SCARG(&ouap, len) = SCARG(uap, len); 625 SCARG(&ouap, prot) = SCARG(uap, prot); 626 SCARG(&ouap, fd) = SCARG(uap, fd); 627 SCARG(&ouap, pos) = SCARG(uap, pos); 628 629 return (sys_mmap(l, &ouap, retval)); 630 } 631 632 #define MC_SYNC 1 633 #define MC_LOCK 2 634 #define MC_UNLOCK 3 635 #define MC_ADVISE 4 636 #define MC_LOCKAS 5 637 #define MC_UNLOCKAS 6 638 639 int 640 sunos_sys_mctl(l, v, retval) 641 struct lwp *l; 642 void *v; 643 register_t *retval; 644 { 645 struct sunos_sys_mctl_args *uap = v; 646 647 switch (SCARG(uap, func)) { 648 case MC_ADVISE: /* ignore for now */ 649 return (0); 650 case MC_SYNC: /* translate to msync */ 651 return (sys___msync13(l, uap, retval)); 652 default: 653 return (EINVAL); 654 } 655 } 656 657 int 658 sunos_sys_setsockopt(l, v, retval) 659 struct lwp *l; 660 void *v; 661 register_t *retval; 662 { 663 struct sunos_sys_setsockopt_args *uap = v; 664 struct proc *p = l->l_proc; 665 struct file *fp; 666 struct mbuf *m = NULL; 667 int error; 668 669 /* getsock() will use the descriptor for us */ 670 if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0) 671 return (error); 672 #define SO_DONTLINGER (~SO_LINGER) 673 if (SCARG(uap, name) == SO_DONTLINGER) { 674 m = m_get(M_WAIT, MT_SOOPTS); 675 mtod(m, struct linger *)->l_onoff = 0; 676 m->m_len = sizeof(struct linger); 677 error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level), 678 SO_LINGER, m); 679 goto out; 680 } 681 if (SCARG(uap, level) == IPPROTO_IP) { 682 #define SUNOS_IP_MULTICAST_IF 2 683 #define SUNOS_IP_MULTICAST_TTL 3 684 #define SUNOS_IP_MULTICAST_LOOP 4 685 #define SUNOS_IP_ADD_MEMBERSHIP 5 686 #define SUNOS_IP_DROP_MEMBERSHIP 6 687 static const int ipoptxlat[] = { 688 IP_MULTICAST_IF, 689 IP_MULTICAST_TTL, 690 IP_MULTICAST_LOOP, 691 IP_ADD_MEMBERSHIP, 692 IP_DROP_MEMBERSHIP 693 }; 694 if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF && 695 SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) { 696 SCARG(uap, name) = 697 ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF]; 698 } 699 } 700 if (SCARG(uap, valsize) > MLEN) { 701 error = EINVAL; 702 goto out; 703 } 704 if (SCARG(uap, val)) { 705 m = m_get(M_WAIT, MT_SOOPTS); 706 error = copyin(SCARG(uap, val), mtod(m, caddr_t), 707 (u_int)SCARG(uap, valsize)); 708 if (error) { 709 (void) m_free(m); 710 goto out; 711 } 712 m->m_len = SCARG(uap, valsize); 713 } 714 error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level), 715 SCARG(uap, name), m); 716 out: 717 FILE_UNUSE(fp, p); 718 return (error); 719 } 720 721 static __inline__ int sunos_sys_socket_common(struct lwp *, register_t *, 722 int type); 723 static __inline__ int 724 sunos_sys_socket_common(l, retval, type) 725 struct lwp *l; 726 register_t *retval; 727 int type; 728 { 729 struct socket *so; 730 struct file *fp; 731 int error, fd; 732 733 /* getsock() will use the descriptor for us */ 734 fd = (int)*retval; 735 if ((error = getsock(l->l_proc->p_fd, fd, &fp)) == 0) { 736 so = (struct socket *)fp->f_data; 737 if (type == SOCK_DGRAM) 738 so->so_options |= SO_BROADCAST; 739 } 740 FILE_UNUSE(fp, l->l_proc); 741 return (error); 742 } 743 744 int 745 sunos_sys_socket(l, v, retval) 746 struct lwp *l; 747 void *v; 748 register_t *retval; 749 { 750 struct sunos_sys_socket_args /* { 751 syscallarg(int) domain; 752 syscallarg(int) type; 753 syscallarg(int) protocol; 754 } */ *uap = v; 755 int error; 756 757 error = sys_socket(l, v, retval); 758 if (error) 759 return (error); 760 return sunos_sys_socket_common(l, retval, SCARG(uap, type)); 761 } 762 763 int 764 sunos_sys_socketpair(l, v, retval) 765 struct lwp *l; 766 void *v; 767 register_t *retval; 768 { 769 struct sunos_sys_socketpair_args /* { 770 syscallarg(int) domain; 771 syscallarg(int) type; 772 syscallarg(int) protocol; 773 syscallarg(int *) rsv; 774 } */ *uap = v; 775 int error; 776 777 error = sys_socketpair(l, v, retval); 778 if (error) 779 return (error); 780 return sunos_sys_socket_common(l, retval, SCARG(uap, type)); 781 } 782 783 /* 784 * XXX: This needs cleaning up. 785 */ 786 int 787 sunos_sys_auditsys(l, v, retval) 788 struct lwp *l; 789 void *v; 790 register_t *retval; 791 { 792 return 0; 793 } 794 795 int 796 sunos_sys_uname(l, v, retval) 797 struct lwp *l; 798 void *v; 799 register_t *retval; 800 { 801 struct sunos_sys_uname_args *uap = v; 802 struct sunos_utsname sut; 803 804 memset(&sut, 0, sizeof(sut)); 805 806 memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1); 807 memcpy(sut.nodename, hostname, sizeof(sut.nodename)); 808 sut.nodename[sizeof(sut.nodename)-1] = '\0'; 809 memcpy(sut.release, osrelease, sizeof(sut.release) - 1); 810 memcpy(sut.version, "1", sizeof(sut.version) - 1); 811 memcpy(sut.machine, machine, sizeof(sut.machine) - 1); 812 813 return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name), 814 sizeof(struct sunos_utsname)); 815 } 816 817 int 818 sunos_sys_setpgrp(l, v, retval) 819 struct lwp *l; 820 void *v; 821 register_t *retval; 822 { 823 struct sunos_sys_setpgrp_args *uap = v; 824 struct proc *p = l->l_proc; 825 826 /* 827 * difference to our setpgid call is to include backwards 828 * compatibility to pre-setsid() binaries. Do setsid() 829 * instead of setpgid() in those cases where the process 830 * tries to create a new session the old way. 831 */ 832 if (!SCARG(uap, pgid) && 833 (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid)) 834 return sys_setsid(l, uap, retval); 835 else 836 return sys_setpgid(l, uap, retval); 837 } 838 839 int 840 sunos_sys_open(l, v, retval) 841 struct lwp *l; 842 void *v; 843 register_t *retval; 844 { 845 struct sunos_sys_open_args *uap = v; 846 struct proc *p = l->l_proc; 847 int smode, nmode; 848 int noctty; 849 int ret; 850 851 caddr_t sg = stackgap_init(p, 0); 852 853 /* convert mode into NetBSD mode */ 854 smode = SCARG(uap, flags); 855 noctty = smode & 0x8000; 856 nmode = smode & 857 (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800); 858 nmode |= ((smode & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0); 859 nmode |= ((smode & 0x0080) ? O_SHLOCK : 0); 860 nmode |= ((smode & 0x0100) ? O_EXLOCK : 0); 861 nmode |= ((smode & 0x2000) ? O_FSYNC : 0); 862 863 if (nmode & O_CREAT) 864 CHECK_ALT_CREAT(p, &sg, SCARG(uap, path)); 865 else 866 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 867 868 SCARG(uap, flags) = nmode; 869 ret = sys_open(l, (struct sys_open_args *)uap, retval); 870 871 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) { 872 struct filedesc *fdp = p->p_fd; 873 struct file *fp; 874 875 fp = fd_getfile(fdp, *retval); 876 simple_unlock(&fp->f_slock); 877 878 /* ignore any error, just give it a try */ 879 if (fp != NULL && fp->f_type == DTYPE_VNODE) 880 (fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p); 881 } 882 return ret; 883 } 884 885 #if defined (NFSSERVER) 886 int 887 sunos_sys_nfssvc(l, v, retval) 888 struct lwp *l; 889 void *v; 890 register_t *retval; 891 { 892 #if 0 893 struct sunos_sys_nfssvc_args *uap = v; 894 struct proc *p = l->l_proc; 895 struct emul *e = p->p_emul; 896 struct sys_nfssvc_args outuap; 897 struct sockaddr sa; 898 int error; 899 caddr_t sg = stackgap_init(p, 0); 900 901 memset(&outuap, 0, sizeof outuap); 902 SCARG(&outuap, fd) = SCARG(uap, fd); 903 SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof(sa)); 904 SCARG(&outuap, msklen) = sizeof(sa); 905 SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof(sa)); 906 SCARG(&outuap, mtchlen) = sizeof(sa); 907 908 memset(&sa, 0, sizeof sa); 909 if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen))) 910 return (error); 911 if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen))) 912 return (error); 913 914 return nfssvc(l, &outuap, retval); 915 #else 916 return (ENOSYS); 917 #endif 918 } 919 #endif /* NFSSERVER */ 920 921 int 922 sunos_sys_ustat(l, v, retval) 923 struct lwp *l; 924 void *v; 925 register_t *retval; 926 { 927 struct sunos_sys_ustat_args *uap = v; 928 struct sunos_ustat us; 929 int error; 930 931 memset(&us, 0, sizeof us); 932 933 /* 934 * XXX: should set f_tfree and f_tinode at least 935 * How do we translate dev -> fstat? (and then to sunos_ustat) 936 */ 937 938 if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0) 939 return (error); 940 return 0; 941 } 942 943 int 944 sunos_sys_quotactl(l, v, retval) 945 struct lwp *l; 946 void *v; 947 register_t *retval; 948 { 949 950 return EINVAL; 951 } 952 953 int 954 sunos_sys_vhangup(l, v, retval) 955 struct lwp *l; 956 void *v; 957 register_t *retval; 958 { 959 struct proc *p = l->l_proc; 960 struct session *sp = p->p_session; 961 962 if (sp->s_ttyvp == 0) 963 return 0; 964 965 if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp) 966 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 967 968 (void) ttywait(sp->s_ttyp); 969 if (sp->s_ttyvp) 970 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 971 if (sp->s_ttyvp) 972 vrele(sp->s_ttyvp); 973 sp->s_ttyvp = NULL; 974 975 return 0; 976 } 977 978 static int 979 sunstatfs(sp, buf) 980 struct statvfs *sp; 981 caddr_t buf; 982 { 983 struct sunos_statfs ssfs; 984 985 memset(&ssfs, 0, sizeof ssfs); 986 ssfs.f_type = 0; 987 ssfs.f_bsize = sp->f_bsize; 988 ssfs.f_blocks = sp->f_blocks; 989 ssfs.f_bfree = sp->f_bfree; 990 ssfs.f_bavail = sp->f_bavail; 991 ssfs.f_files = sp->f_files; 992 ssfs.f_ffree = sp->f_ffree; 993 ssfs.f_fsid = sp->f_fsidx; 994 return copyout((caddr_t)&ssfs, buf, sizeof ssfs); 995 } 996 997 int 998 sunos_sys_statfs(l, v, retval) 999 struct lwp *l; 1000 void *v; 1001 register_t *retval; 1002 { 1003 struct sunos_sys_statfs_args *uap = v; 1004 struct proc *p = l->l_proc; 1005 struct mount *mp; 1006 struct statvfs *sp; 1007 int error; 1008 struct nameidata nd; 1009 1010 caddr_t sg = stackgap_init(p, 0); 1011 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 1012 1013 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 1014 if ((error = namei(&nd)) != 0) 1015 return (error); 1016 mp = nd.ni_vp->v_mount; 1017 sp = &mp->mnt_stat; 1018 vrele(nd.ni_vp); 1019 if ((error = VFS_STATVFS(mp, sp, p)) != 0) 1020 return (error); 1021 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK; 1022 return sunstatfs(sp, (caddr_t)SCARG(uap, buf)); 1023 } 1024 1025 int 1026 sunos_sys_fstatfs(l, v, retval) 1027 struct lwp *l; 1028 void *v; 1029 register_t *retval; 1030 { 1031 struct sunos_sys_fstatfs_args *uap = v; 1032 struct proc *p = l->l_proc; 1033 struct file *fp; 1034 struct mount *mp; 1035 struct statvfs *sp; 1036 int error; 1037 1038 /* getvnode() will use the descriptor for us */ 1039 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 1040 return (error); 1041 mp = ((struct vnode *)fp->f_data)->v_mount; 1042 sp = &mp->mnt_stat; 1043 if ((error = VFS_STATVFS(mp, sp, p)) != 0) 1044 goto out; 1045 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK; 1046 error = sunstatfs(sp, (caddr_t)SCARG(uap, buf)); 1047 out: 1048 FILE_UNUSE(fp, p); 1049 return (error); 1050 } 1051 1052 int 1053 sunos_sys_exportfs(l, v, retval) 1054 struct lwp *l; 1055 void *v; 1056 register_t *retval; 1057 { 1058 /* 1059 * XXX: should perhaps translate into a mount(2) 1060 * with MOUNT_EXPORT? 1061 */ 1062 return 0; 1063 } 1064 1065 int 1066 sunos_sys_mknod(l, v, retval) 1067 struct lwp *l; 1068 void *v; 1069 register_t *retval; 1070 { 1071 struct sunos_sys_mknod_args *uap = v; 1072 struct proc *p = l->l_proc; 1073 1074 caddr_t sg = stackgap_init(p, 0); 1075 CHECK_ALT_CREAT(p, &sg, SCARG(uap, path)); 1076 1077 if (S_ISFIFO(SCARG(uap, mode))) 1078 return sys_mkfifo(l, uap, retval); 1079 1080 return sys_mknod(l, (struct sys_mknod_args *)uap, retval); 1081 } 1082 1083 #define SUNOS_SC_ARG_MAX 1 1084 #define SUNOS_SC_CHILD_MAX 2 1085 #define SUNOS_SC_CLK_TCK 3 1086 #define SUNOS_SC_NGROUPS_MAX 4 1087 #define SUNOS_SC_OPEN_MAX 5 1088 #define SUNOS_SC_JOB_CONTROL 6 1089 #define SUNOS_SC_SAVED_IDS 7 1090 #define SUNOS_SC_VERSION 8 1091 1092 int 1093 sunos_sys_sysconf(l, v, retval) 1094 struct lwp *l; 1095 void *v; 1096 register_t *retval; 1097 { 1098 struct sunos_sys_sysconf_args *uap = v; 1099 extern int maxfiles; 1100 1101 switch(SCARG(uap, name)) { 1102 case SUNOS_SC_ARG_MAX: 1103 *retval = ARG_MAX; 1104 break; 1105 case SUNOS_SC_CHILD_MAX: 1106 *retval = maxproc; 1107 break; 1108 case SUNOS_SC_CLK_TCK: 1109 *retval = 60; /* should this be `hz', ie. 100? */ 1110 break; 1111 case SUNOS_SC_NGROUPS_MAX: 1112 *retval = NGROUPS_MAX; 1113 break; 1114 case SUNOS_SC_OPEN_MAX: 1115 *retval = maxfiles; 1116 break; 1117 case SUNOS_SC_JOB_CONTROL: 1118 *retval = 1; 1119 break; 1120 case SUNOS_SC_SAVED_IDS: 1121 #ifdef _POSIX_SAVED_IDS 1122 *retval = 1; 1123 #else 1124 *retval = 0; 1125 #endif 1126 break; 1127 case SUNOS_SC_VERSION: 1128 *retval = 198808; 1129 break; 1130 default: 1131 return EINVAL; 1132 } 1133 return 0; 1134 } 1135 1136 #define SUNOS_RLIMIT_NOFILE 6 /* Other RLIMIT_* are the same */ 1137 #define SUNOS_RLIM_NLIMITS 7 1138 1139 int 1140 sunos_sys_getrlimit(l, v, retval) 1141 struct lwp *l; 1142 void *v; 1143 register_t *retval; 1144 { 1145 struct sunos_sys_getrlimit_args *uap = v; 1146 1147 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS) 1148 return EINVAL; 1149 1150 if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE) 1151 SCARG(uap, which) = RLIMIT_NOFILE; 1152 1153 return compat_43_sys_getrlimit(l, uap, retval); 1154 } 1155 1156 int 1157 sunos_sys_setrlimit(l, v, retval) 1158 struct lwp *l; 1159 void *v; 1160 register_t *retval; 1161 { 1162 struct sunos_sys_getrlimit_args *uap = v; 1163 1164 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS) 1165 return EINVAL; 1166 1167 if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE) 1168 SCARG(uap, which) = RLIMIT_NOFILE; 1169 1170 return compat_43_sys_setrlimit(l, uap, retval); 1171 } 1172 1173 /* for the m68k machines */ 1174 #ifndef PT_GETFPREGS 1175 #define PT_GETFPREGS -1 1176 #endif 1177 #ifndef PT_SETFPREGS 1178 #define PT_SETFPREGS -1 1179 #endif 1180 1181 static const int sreq2breq[] = { 1182 PT_TRACE_ME, PT_READ_I, PT_READ_D, -1, 1183 PT_WRITE_I, PT_WRITE_D, -1, PT_CONTINUE, 1184 PT_KILL, -1, PT_ATTACH, PT_DETACH, 1185 PT_GETREGS, PT_SETREGS, PT_GETFPREGS, PT_SETFPREGS 1186 }; 1187 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]); 1188 1189 int 1190 sunos_sys_ptrace(l, v, retval) 1191 struct lwp *l; 1192 void *v; 1193 register_t *retval; 1194 { 1195 struct sunos_sys_ptrace_args *uap = v; 1196 struct sys_ptrace_args pa; 1197 int req; 1198 1199 req = SCARG(uap, req); 1200 1201 if (req < 0 || req >= nreqs) 1202 return (EINVAL); 1203 1204 req = sreq2breq[req]; 1205 if (req == -1) 1206 return (EINVAL); 1207 1208 SCARG(&pa, req) = req; 1209 SCARG(&pa, pid) = (pid_t)SCARG(uap, pid); 1210 SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr); 1211 SCARG(&pa, data) = SCARG(uap, data); 1212 1213 return sys_ptrace(l, &pa, retval); 1214 } 1215 1216 /* 1217 * SunOS reboot system call (for compatibility). 1218 * Sun lets you pass in a boot string which the PROM 1219 * saves and provides to the next boot program. 1220 */ 1221 1222 #define SUNOS_RB_ASKNAME 0x001 1223 #define SUNOS_RB_SINGLE 0x002 1224 #define SUNOS_RB_NOSYNC 0x004 1225 #define SUNOS_RB_HALT 0x008 1226 #define SUNOS_RB_DUMP 0x080 1227 #define SUNOS_RB_STRING 0x200 1228 1229 static struct sunos_howto_conv { 1230 int sun_howto; 1231 int bsd_howto; 1232 } sunos_howto_conv[] = { 1233 { SUNOS_RB_ASKNAME, RB_ASKNAME }, 1234 { SUNOS_RB_SINGLE, RB_SINGLE }, 1235 { SUNOS_RB_NOSYNC, RB_NOSYNC }, 1236 { SUNOS_RB_HALT, RB_HALT }, 1237 { SUNOS_RB_DUMP, RB_DUMP }, 1238 { SUNOS_RB_STRING, RB_STRING }, 1239 { 0x000, 0 }, 1240 }; 1241 1242 int 1243 sunos_sys_reboot(l, v, retval) 1244 struct lwp *l; 1245 void *v; 1246 register_t *retval; 1247 { 1248 struct sunos_sys_reboot_args *uap = v; 1249 struct proc *p = l->l_proc; 1250 struct sys_reboot_args ua; 1251 struct sunos_howto_conv *convp; 1252 int error, bsd_howto, sun_howto; 1253 char *bootstr; 1254 1255 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1256 return (error); 1257 1258 /* 1259 * Convert howto bits to BSD format. 1260 */ 1261 sun_howto = SCARG(uap, howto); 1262 bsd_howto = 0; 1263 convp = sunos_howto_conv; 1264 while (convp->sun_howto) { 1265 if (sun_howto & convp->sun_howto) 1266 bsd_howto |= convp->bsd_howto; 1267 convp++; 1268 } 1269 1270 /* 1271 * Sun RB_STRING (Get user supplied bootstring.) 1272 * If the machine supports passing a string to the 1273 * next booted kernel. 1274 */ 1275 if (sun_howto & SUNOS_RB_STRING) { 1276 char bs[128]; 1277 1278 error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0); 1279 1280 if (error) 1281 bootstr = NULL; 1282 else 1283 bootstr = bs; 1284 } else 1285 bootstr = NULL; 1286 1287 SCARG(&ua, opt) = bsd_howto; 1288 SCARG(&ua, bootstr) = bootstr; 1289 sys_reboot(l, &ua, retval); 1290 return(0); 1291 } 1292 1293 /* 1294 * Generalized interface signal handler, 4.3-compatible. 1295 */ 1296 /* ARGSUSED */ 1297 int 1298 sunos_sys_sigvec(l, v, retval) 1299 struct lwp *l; 1300 void *v; 1301 register_t *retval; 1302 { 1303 struct sunos_sys_sigvec_args /* { 1304 syscallarg(int) signum; 1305 syscallarg(struct sigvec *) nsv; 1306 syscallarg(struct sigvec *) osv; 1307 } */ *uap = v; 1308 struct sigvec nsv, osv; 1309 struct sigaction nsa, osa; 1310 int error; 1311 /*XXX*/extern void compat_43_sigvec_to_sigaction 1312 __P((const struct sigvec *, struct sigaction *)); 1313 /*XXX*/extern void compat_43_sigaction_to_sigvec 1314 __P((const struct sigaction *, struct sigvec *)); 1315 1316 if (SCARG(uap, nsv)) { 1317 error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv)); 1318 if (error != 0) 1319 return (error); 1320 1321 /* 1322 * SunOS uses the mask 0x0004 as SV_RESETHAND 1323 * meaning: `reset to SIG_DFL on delivery'. 1324 * We support only the bits in: 0xF 1325 * (those bits are the same as ours) 1326 */ 1327 if (nsv.sv_flags & ~0xF) 1328 return (EINVAL); 1329 1330 compat_43_sigvec_to_sigaction(&nsv, &nsa); 1331 } 1332 error = sigaction1(l->l_proc, SCARG(uap, signum), 1333 SCARG(uap, nsv) ? &nsa : 0, 1334 SCARG(uap, osv) ? &osa : 0, 1335 NULL, 0); 1336 if (error != 0) 1337 return (error); 1338 1339 if (SCARG(uap, osv)) { 1340 compat_43_sigaction_to_sigvec(&osa, &osv); 1341 error = copyout(&osv, SCARG(uap, osv), sizeof(osv)); 1342 if (error != 0) 1343 return (error); 1344 } 1345 1346 return (0); 1347 } 1348