The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/svr4/svr4_misc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1998 Mark Newton
    3  * Copyright (c) 1994 Christos Zoulas
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 /*
   29  * SVR4 compatibility module.
   30  *
   31  * SVR4 system calls that are implemented differently in BSD are
   32  * handled here.
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: releng/8.1/sys/compat/svr4/svr4_misc.c 204293 2010-02-24 22:16:16Z brooks $");
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/dirent.h>
   41 #include <sys/fcntl.h>
   42 #include <sys/filedesc.h>
   43 #include <sys/imgact.h>
   44 #include <sys/kernel.h>
   45 #include <sys/lock.h>
   46 #include <sys/malloc.h>
   47 #include <sys/file.h>           /* Must come after sys/malloc.h */
   48 #include <sys/mman.h>
   49 #include <sys/mount.h>
   50 #include <sys/msg.h>
   51 #include <sys/mutex.h>
   52 #include <sys/namei.h>
   53 #include <sys/priv.h>
   54 #include <sys/proc.h>
   55 #include <sys/ptrace.h>
   56 #include <sys/resource.h>
   57 #include <sys/resourcevar.h>
   58 #include <sys/sem.h>
   59 #include <sys/signalvar.h>
   60 #include <sys/stat.h>
   61 #include <sys/sx.h>
   62 #include <sys/syscallsubr.h>
   63 #include <sys/sysproto.h>
   64 #include <sys/time.h>
   65 #include <sys/times.h>
   66 #include <sys/uio.h>
   67 #include <sys/vnode.h>
   68 #include <sys/wait.h>
   69 
   70 #include <compat/svr4/svr4.h>
   71 #include <compat/svr4/svr4_types.h>
   72 #include <compat/svr4/svr4_signal.h>
   73 #include <compat/svr4/svr4_proto.h>
   74 #include <compat/svr4/svr4_util.h>
   75 #include <compat/svr4/svr4_sysconfig.h>
   76 #include <compat/svr4/svr4_dirent.h>
   77 #include <compat/svr4/svr4_acl.h>
   78 #include <compat/svr4/svr4_ulimit.h>
   79 #include <compat/svr4/svr4_statvfs.h>
   80 #include <compat/svr4/svr4_hrt.h>
   81 #include <compat/svr4/svr4_mman.h>
   82 #include <compat/svr4/svr4_wait.h>
   83 
   84 #include <security/mac/mac_framework.h>
   85 
   86 #include <machine/vmparam.h>
   87 #include <vm/vm.h>
   88 #include <vm/vm_param.h>
   89 #include <vm/vm_map.h>
   90 #if defined(__FreeBSD__)
   91 #include <vm/uma.h>
   92 #include <vm/vm_extern.h>
   93 #endif
   94 
   95 #if defined(NetBSD)
   96 # if defined(UVM)
   97 #  include <uvm/uvm_extern.h>
   98 # endif
   99 #endif
  100 
  101 #define BSD_DIRENT(cp)          ((struct dirent *)(cp))
  102 
  103 static int svr4_mknod(struct thread *, register_t *, char *,
  104     svr4_mode_t, svr4_dev_t);
  105 
  106 static __inline clock_t timeval_to_clock_t(struct timeval *);
  107 static int svr4_setinfo (pid_t , struct rusage *, int, svr4_siginfo_t *);
  108 
  109 struct svr4_hrtcntl_args;
  110 static int svr4_hrtcntl (struct thread *, struct svr4_hrtcntl_args *,
  111     register_t *);
  112 static void bsd_statfs_to_svr4_statvfs(const struct statfs *,
  113     struct svr4_statvfs *);
  114 static void bsd_statfs_to_svr4_statvfs64(const struct statfs *,
  115     struct svr4_statvfs64 *);
  116 static struct proc *svr4_pfind(pid_t pid);
  117 
  118 /* BOGUS noop */
  119 #if defined(BOGUS)
  120 int
  121 svr4_sys_setitimer(td, uap)
  122         struct thread *td;
  123         struct svr4_sys_setitimer_args *uap;
  124 {
  125         td->td_retval[0] = 0;
  126         return 0;
  127 }
  128 #endif
  129 
  130 int
  131 svr4_sys_wait(td, uap)
  132         struct thread *td;
  133         struct svr4_sys_wait_args *uap;
  134 {
  135         int error, st, sig;
  136 
  137         error = kern_wait(td, WAIT_ANY, &st, 0, NULL);
  138         if (error)
  139                 return (error);
  140       
  141         if (WIFSIGNALED(st)) {
  142                 sig = WTERMSIG(st);
  143                 if (sig >= 0 && sig < NSIG)
  144                         st = (st & ~0177) | SVR4_BSD2SVR4_SIG(sig);
  145         } else if (WIFSTOPPED(st)) {
  146                 sig = WSTOPSIG(st);
  147                 if (sig >= 0 && sig < NSIG)
  148                         st = (st & ~0xff00) | (SVR4_BSD2SVR4_SIG(sig) << 8);
  149         }
  150 
  151         /*
  152          * It looks like wait(2) on svr4/solaris/2.4 returns
  153          * the status in retval[1], and the pid on retval[0].
  154          */
  155         td->td_retval[1] = st;
  156 
  157         if (uap->status)
  158                 error = copyout(&st, uap->status, sizeof(st));
  159 
  160         return (error);
  161 }
  162 
  163 int
  164 svr4_sys_execv(td, uap)
  165         struct thread *td;
  166         struct svr4_sys_execv_args *uap;
  167 {
  168         struct image_args eargs;
  169         char *path;
  170         int error;
  171 
  172         CHECKALTEXIST(td, uap->path, &path);
  173 
  174         error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, uap->argp, NULL);
  175         free(path, M_TEMP);
  176         if (error == 0)
  177                 error = kern_execve(td, &eargs, NULL);
  178         return (error);
  179 }
  180 
  181 int
  182 svr4_sys_execve(td, uap)
  183         struct thread *td;
  184         struct svr4_sys_execve_args *uap;
  185 {
  186         struct image_args eargs;
  187         char *path;
  188         int error;
  189 
  190         CHECKALTEXIST(td, uap->path, &path);
  191 
  192         error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, uap->argp,
  193             uap->envp);
  194         free(path, M_TEMP);
  195         if (error == 0)
  196                 error = kern_execve(td, &eargs, NULL);
  197         return (error);
  198 }
  199 
  200 int
  201 svr4_sys_time(td, v)
  202         struct thread *td;
  203         struct svr4_sys_time_args *v;
  204 {
  205         struct svr4_sys_time_args *uap = v;
  206         int error = 0;
  207         struct timeval tv;
  208 
  209         microtime(&tv);
  210         if (uap->t)
  211                 error = copyout(&tv.tv_sec, uap->t,
  212                                 sizeof(*(uap->t)));
  213         td->td_retval[0] = (int) tv.tv_sec;
  214 
  215         return error;
  216 }
  217 
  218 
  219 /*
  220  * Read SVR4-style directory entries.  We suck them into kernel space so
  221  * that they can be massaged before being copied out to user code.  
  222  *
  223  * This code is ported from the Linux emulator:  Changes to the VFS interface
  224  * between FreeBSD and NetBSD have made it simpler to port it from there than
  225  * to adapt the NetBSD version.
  226  */
  227 int
  228 svr4_sys_getdents64(td, uap)
  229         struct thread *td;
  230         struct svr4_sys_getdents64_args *uap;
  231 {
  232         struct dirent *bdp;
  233         struct vnode *vp;
  234         caddr_t inp, buf;               /* BSD-format */
  235         int len, reclen;                /* BSD-format */
  236         caddr_t outp;                   /* SVR4-format */
  237         int resid, svr4reclen=0;        /* SVR4-format */
  238         struct file *fp;
  239         struct uio auio;
  240         struct iovec aiov;
  241         off_t off;
  242         struct svr4_dirent64 svr4_dirent;
  243         int buflen, error, eofflag, nbytes, justone, vfslocked;
  244         u_long *cookies = NULL, *cookiep;
  245         int ncookies;
  246 
  247         DPRINTF(("svr4_sys_getdents64(%d, *, %d)\n",
  248                 uap->fd, uap->nbytes));
  249         if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0) {
  250                 return (error);
  251         }
  252 
  253         if ((fp->f_flag & FREAD) == 0) {
  254                 fdrop(fp, td);
  255                 return (EBADF);
  256         }
  257 
  258         vp = fp->f_vnode;
  259         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  260         if (vp->v_type != VDIR) {
  261                 VFS_UNLOCK_GIANT(vfslocked);
  262                 fdrop(fp, td);
  263                 return (EINVAL);
  264         }
  265 
  266         nbytes = uap->nbytes;
  267         if (nbytes == 1) {
  268                 nbytes = sizeof (struct svr4_dirent64);
  269                 justone = 1;
  270         }
  271         else
  272                 justone = 0;
  273 
  274         off = fp->f_offset;
  275 #define DIRBLKSIZ       512             /* XXX we used to use ufs's DIRBLKSIZ */
  276         buflen = max(DIRBLKSIZ, nbytes);
  277         buflen = min(buflen, MAXBSIZE);
  278         buf = malloc(buflen, M_TEMP, M_WAITOK);
  279         vn_lock(vp, LK_SHARED | LK_RETRY);
  280 again:
  281         aiov.iov_base = buf;
  282         aiov.iov_len = buflen;
  283         auio.uio_iov = &aiov;
  284         auio.uio_iovcnt = 1;
  285         auio.uio_rw = UIO_READ;
  286         auio.uio_segflg = UIO_SYSSPACE;
  287         auio.uio_td = td;
  288         auio.uio_resid = buflen;
  289         auio.uio_offset = off;
  290 
  291         if (cookies) {
  292                 free(cookies, M_TEMP);
  293                 cookies = NULL;
  294         }
  295 
  296 #ifdef MAC
  297         error = mac_vnode_check_readdir(td->td_ucred, vp);
  298         if (error)
  299                 goto out;
  300 #endif
  301 
  302         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
  303                                                 &ncookies, &cookies);
  304         if (error) {
  305                 goto out;
  306         }
  307 
  308         inp = buf;
  309         outp = (caddr_t) uap->dp;
  310         resid = nbytes;
  311         if ((len = buflen - auio.uio_resid) <= 0) {
  312                 goto eof;
  313         }
  314 
  315         cookiep = cookies;
  316 
  317         if (cookies) {
  318                 /*
  319                  * When using cookies, the vfs has the option of reading from
  320                  * a different offset than that supplied (UFS truncates the
  321                  * offset to a block boundary to make sure that it never reads
  322                  * partway through a directory entry, even if the directory
  323                  * has been compacted).
  324                  */
  325                 while (len > 0 && ncookies > 0 && *cookiep <= off) {
  326                         bdp = (struct dirent *) inp;
  327                         len -= bdp->d_reclen;
  328                         inp += bdp->d_reclen;
  329                         cookiep++;
  330                         ncookies--;
  331                 }
  332         }
  333 
  334         while (len > 0) {
  335                 if (cookiep && ncookies == 0)
  336                         break;
  337                 bdp = (struct dirent *) inp;
  338                 reclen = bdp->d_reclen;
  339                 if (reclen & 3) {
  340                         DPRINTF(("svr4_readdir: reclen=%d\n", reclen));
  341                         error = EFAULT;
  342                         goto out;
  343                 }
  344   
  345                 if (bdp->d_fileno == 0) {
  346                         inp += reclen;
  347                         if (cookiep) {
  348                                 off = *cookiep++;
  349                                 ncookies--;
  350                         } else
  351                                 off += reclen;
  352                         len -= reclen;
  353                         continue;
  354                 }
  355                 svr4reclen = SVR4_RECLEN(&svr4_dirent, bdp->d_namlen);
  356                 if (reclen > len || resid < svr4reclen) {
  357                         outp++;
  358                         break;
  359                 }
  360                 svr4_dirent.d_ino = (long) bdp->d_fileno;
  361                 if (justone) {
  362                         /*
  363                          * old svr4-style readdir usage.
  364                          */
  365                         svr4_dirent.d_off = (svr4_off_t) svr4reclen;
  366                         svr4_dirent.d_reclen = (u_short) bdp->d_namlen;
  367                 } else {
  368                         svr4_dirent.d_off = (svr4_off_t)(off + reclen);
  369                         svr4_dirent.d_reclen = (u_short) svr4reclen;
  370                 }
  371                 strlcpy(svr4_dirent.d_name, bdp->d_name, sizeof(svr4_dirent.d_name));
  372                 if ((error = copyout((caddr_t)&svr4_dirent, outp, svr4reclen)))
  373                         goto out;
  374                 inp += reclen;
  375                 if (cookiep) {
  376                         off = *cookiep++;
  377                         ncookies--;
  378                 } else
  379                         off += reclen;
  380                 outp += svr4reclen;
  381                 resid -= svr4reclen;
  382                 len -= reclen;
  383                 if (justone)
  384                         break;
  385         }
  386 
  387         if (outp == (caddr_t) uap->dp)
  388                 goto again;
  389         fp->f_offset = off;
  390 
  391         if (justone)
  392                 nbytes = resid + svr4reclen;
  393 
  394 eof:
  395         td->td_retval[0] = nbytes - resid;
  396 out:
  397         VOP_UNLOCK(vp, 0);
  398         VFS_UNLOCK_GIANT(vfslocked);
  399         fdrop(fp, td);
  400         if (cookies)
  401                 free(cookies, M_TEMP);
  402         free(buf, M_TEMP);
  403         return error;
  404 }
  405 
  406 
  407 int
  408 svr4_sys_getdents(td, uap)
  409         struct thread *td;
  410         struct svr4_sys_getdents_args *uap;
  411 {
  412         struct dirent *bdp;
  413         struct vnode *vp;
  414         caddr_t inp, buf;       /* BSD-format */
  415         int len, reclen;        /* BSD-format */
  416         caddr_t outp;           /* SVR4-format */
  417         int resid, svr4_reclen; /* SVR4-format */
  418         struct file *fp;
  419         struct uio auio;
  420         struct iovec aiov;
  421         struct svr4_dirent idb;
  422         off_t off;              /* true file offset */
  423         int buflen, error, eofflag, vfslocked;
  424         u_long *cookiebuf = NULL, *cookie;
  425         int ncookies = 0, *retval = td->td_retval;
  426 
  427         if (uap->nbytes < 0)
  428                 return (EINVAL);
  429 
  430         if ((error = getvnode(td->td_proc->p_fd, uap->fd, &fp)) != 0)
  431                 return (error);
  432 
  433         if ((fp->f_flag & FREAD) == 0) {
  434                 fdrop(fp, td);
  435                 return (EBADF);
  436         }
  437 
  438         vp = fp->f_vnode;
  439         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  440         if (vp->v_type != VDIR) {
  441                 VFS_UNLOCK_GIANT(vfslocked);
  442                 fdrop(fp, td);
  443                 return (EINVAL);
  444         }
  445 
  446         buflen = min(MAXBSIZE, uap->nbytes);
  447         buf = malloc(buflen, M_TEMP, M_WAITOK);
  448         vn_lock(vp, LK_SHARED | LK_RETRY);
  449         off = fp->f_offset;
  450 again:
  451         aiov.iov_base = buf;
  452         aiov.iov_len = buflen;
  453         auio.uio_iov = &aiov;
  454         auio.uio_iovcnt = 1;
  455         auio.uio_rw = UIO_READ;
  456         auio.uio_segflg = UIO_SYSSPACE;
  457         auio.uio_td = td;
  458         auio.uio_resid = buflen;
  459         auio.uio_offset = off;
  460 
  461 #ifdef MAC
  462         error = mac_vnode_check_readdir(td->td_ucred, vp);
  463         if (error)
  464                 goto out;
  465 #endif
  466 
  467         /*
  468          * First we read into the malloc'ed buffer, then
  469          * we massage it into user space, one record at a time.
  470          */
  471         error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
  472             &cookiebuf);
  473         if (error) {
  474                 goto out;
  475         }
  476 
  477         inp = buf;
  478         outp = uap->buf;
  479         resid = uap->nbytes;
  480         if ((len = buflen - auio.uio_resid) == 0)
  481                 goto eof;
  482 
  483         for (cookie = cookiebuf; len > 0; len -= reclen) {
  484                 bdp = (struct dirent *)inp;
  485                 reclen = bdp->d_reclen;
  486                 if (reclen & 3)
  487                         panic("svr4_sys_getdents64: bad reclen");
  488                 if (cookie)
  489                         off = *cookie++; /* each entry points to the next */
  490                 else
  491                         off += reclen;
  492                 if ((off >> 32) != 0) {
  493                         uprintf("svr4_sys_getdents64: dir offset too large for emulated program");
  494                         error = EINVAL;
  495                         goto out;
  496                 }
  497                 if (bdp->d_fileno == 0) {
  498                         inp += reclen;  /* it is a hole; squish it out */
  499                         continue;
  500                 }
  501                 svr4_reclen = SVR4_RECLEN(&idb, bdp->d_namlen);
  502                 if (reclen > len || resid < svr4_reclen) {
  503                         /* entry too big for buffer, so just stop */
  504                         outp++;
  505                         break;
  506                 }
  507                 /*
  508                  * Massage in place to make a SVR4-shaped dirent (otherwise
  509                  * we have to worry about touching user memory outside of
  510                  * the copyout() call).
  511                  */
  512                 idb.d_ino = (svr4_ino_t)bdp->d_fileno;
  513                 idb.d_off = (svr4_off_t)off;
  514                 idb.d_reclen = (u_short)svr4_reclen;
  515                 strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
  516                 if ((error = copyout((caddr_t)&idb, outp, svr4_reclen)))
  517                         goto out;
  518                 /* advance past this real entry */
  519                 inp += reclen;
  520                 /* advance output past SVR4-shaped entry */
  521                 outp += svr4_reclen;
  522                 resid -= svr4_reclen;
  523         }
  524 
  525         /* if we squished out the whole block, try again */
  526         if (outp == uap->buf)
  527                 goto again;
  528         fp->f_offset = off;     /* update the vnode offset */
  529 
  530 eof:
  531         *retval = uap->nbytes - resid;
  532 out:
  533         VOP_UNLOCK(vp, 0);
  534         VFS_UNLOCK_GIANT(vfslocked);
  535         fdrop(fp, td);
  536         if (cookiebuf)
  537                 free(cookiebuf, M_TEMP);
  538         free(buf, M_TEMP);
  539         return error;
  540 }
  541 
  542 
  543 int
  544 svr4_sys_mmap(td, uap)
  545         struct thread *td;
  546         struct svr4_sys_mmap_args *uap;
  547 {
  548         struct mmap_args         mm;
  549         int             *retval;
  550 
  551         retval = td->td_retval;
  552 #define _MAP_NEW        0x80000000
  553         /*
  554          * Verify the arguments.
  555          */
  556         if (uap->prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
  557                 return EINVAL;  /* XXX still needed? */
  558 
  559         if (uap->len == 0)
  560                 return EINVAL;
  561 
  562         mm.prot = uap->prot;
  563         mm.len = uap->len;
  564         mm.flags = uap->flags & ~_MAP_NEW;
  565         mm.fd = uap->fd;
  566         mm.addr = uap->addr;
  567         mm.pos = uap->pos;
  568 
  569         return mmap(td, &mm);
  570 }
  571 
  572 int
  573 svr4_sys_mmap64(td, uap)
  574         struct thread *td;
  575         struct svr4_sys_mmap64_args *uap;
  576 {
  577         struct mmap_args         mm;
  578         void            *rp;
  579 
  580 #define _MAP_NEW        0x80000000
  581         /*
  582          * Verify the arguments.
  583          */
  584         if (uap->prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
  585                 return EINVAL;  /* XXX still needed? */
  586 
  587         if (uap->len == 0)
  588                 return EINVAL;
  589 
  590         mm.prot = uap->prot;
  591         mm.len = uap->len;
  592         mm.flags = uap->flags & ~_MAP_NEW;
  593         mm.fd = uap->fd;
  594         mm.addr = uap->addr;
  595         mm.pos = uap->pos;
  596 
  597         rp = (void *) round_page((vm_offset_t)(td->td_proc->p_vmspace->vm_daddr + maxdsiz));
  598         if ((mm.flags & MAP_FIXED) == 0 &&
  599             mm.addr != 0 && (void *)mm.addr < rp)
  600                 mm.addr = rp;
  601 
  602         return mmap(td, &mm);
  603 }
  604 
  605 
  606 int
  607 svr4_sys_fchroot(td, uap)
  608         struct thread *td;
  609         struct svr4_sys_fchroot_args *uap;
  610 {
  611         struct filedesc *fdp = td->td_proc->p_fd;
  612         struct vnode    *vp;
  613         struct file     *fp;
  614         int              error, vfslocked;
  615 
  616         if ((error = priv_check(td, PRIV_VFS_FCHROOT)) != 0)
  617                 return error;
  618         if ((error = getvnode(fdp, uap->fd, &fp)) != 0)
  619                 return error;
  620         vp = fp->f_vnode;
  621         VREF(vp);
  622         fdrop(fp, td);
  623         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
  624         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  625         error = change_dir(vp, td);
  626         if (error)
  627                 goto fail;
  628 #ifdef MAC
  629         error = mac_vnode_check_chroot(td->td_ucred, vp);
  630         if (error)
  631                 goto fail;
  632 #endif
  633         VOP_UNLOCK(vp, 0);
  634         error = change_root(vp, td);
  635         vrele(vp);
  636         VFS_UNLOCK_GIANT(vfslocked);
  637         return (error);
  638 fail:
  639         vput(vp);
  640         VFS_UNLOCK_GIANT(vfslocked);
  641         return (error);
  642 }
  643 
  644 
  645 static int
  646 svr4_mknod(td, retval, path, mode, dev)
  647         struct thread *td;
  648         register_t *retval;
  649         char *path;
  650         svr4_mode_t mode;
  651         svr4_dev_t dev;
  652 {
  653         char *newpath;
  654         int error;
  655 
  656         CHECKALTEXIST(td, path, &newpath);
  657 
  658         if (S_ISFIFO(mode))
  659                 error = kern_mkfifo(td, newpath, UIO_SYSSPACE, mode);
  660         else
  661                 error = kern_mknod(td, newpath, UIO_SYSSPACE, mode, dev);
  662         free(newpath, M_TEMP);
  663         return (error);
  664 }
  665 
  666 
  667 int
  668 svr4_sys_mknod(td, uap)
  669         struct thread *td;
  670         struct svr4_sys_mknod_args *uap;
  671 {
  672         int *retval = td->td_retval;
  673         return svr4_mknod(td, retval,
  674                           uap->path, uap->mode,
  675                           (svr4_dev_t)svr4_to_bsd_odev_t(uap->dev));
  676 }
  677 
  678 
  679 int
  680 svr4_sys_xmknod(td, uap)
  681         struct thread *td;
  682         struct svr4_sys_xmknod_args *uap;
  683 {
  684         int *retval = td->td_retval;
  685         return svr4_mknod(td, retval,
  686                           uap->path, uap->mode,
  687                           (svr4_dev_t)svr4_to_bsd_dev_t(uap->dev));
  688 }
  689 
  690 
  691 int
  692 svr4_sys_vhangup(td, uap)
  693         struct thread *td;
  694         struct svr4_sys_vhangup_args *uap;
  695 {
  696         return 0;
  697 }
  698 
  699 
  700 int
  701 svr4_sys_sysconfig(td, uap)
  702         struct thread *td;
  703         struct svr4_sys_sysconfig_args *uap;
  704 {
  705         int *retval;
  706 
  707         retval = &(td->td_retval[0]);
  708 
  709         switch (uap->name) {
  710         case SVR4_CONFIG_NGROUPS:
  711                 *retval = ngroups_max;
  712                 break;
  713         case SVR4_CONFIG_CHILD_MAX:
  714                 *retval = maxproc;
  715                 break;
  716         case SVR4_CONFIG_OPEN_FILES:
  717                 *retval = maxfiles;
  718                 break;
  719         case SVR4_CONFIG_POSIX_VER:
  720                 *retval = 198808;
  721                 break;
  722         case SVR4_CONFIG_PAGESIZE:
  723                 *retval = PAGE_SIZE;
  724                 break;
  725         case SVR4_CONFIG_CLK_TCK:
  726                 *retval = 60;   /* should this be `hz', ie. 100? */
  727                 break;
  728         case SVR4_CONFIG_XOPEN_VER:
  729                 *retval = 2;    /* XXX: What should that be? */
  730                 break;
  731         case SVR4_CONFIG_PROF_TCK:
  732                 *retval = 60;   /* XXX: What should that be? */
  733                 break;
  734         case SVR4_CONFIG_NPROC_CONF:
  735                 *retval = 1;    /* Only one processor for now */
  736                 break;
  737         case SVR4_CONFIG_NPROC_ONLN:
  738                 *retval = 1;    /* And it better be online */
  739                 break;
  740         case SVR4_CONFIG_AIO_LISTIO_MAX:
  741         case SVR4_CONFIG_AIO_MAX:
  742         case SVR4_CONFIG_AIO_PRIO_DELTA_MAX:
  743                 *retval = 0;    /* No aio support */
  744                 break;
  745         case SVR4_CONFIG_DELAYTIMER_MAX:
  746                 *retval = 0;    /* No delaytimer support */
  747                 break;
  748         case SVR4_CONFIG_MQ_OPEN_MAX:
  749                 *retval = msginfo.msgmni;
  750                 break;
  751         case SVR4_CONFIG_MQ_PRIO_MAX:
  752                 *retval = 0;    /* XXX: Don't know */
  753                 break;
  754         case SVR4_CONFIG_RTSIG_MAX:
  755                 *retval = 0;
  756                 break;
  757         case SVR4_CONFIG_SEM_NSEMS_MAX:
  758                 *retval = seminfo.semmni;
  759                 break;
  760         case SVR4_CONFIG_SEM_VALUE_MAX:
  761                 *retval = seminfo.semvmx;
  762                 break;
  763         case SVR4_CONFIG_SIGQUEUE_MAX:
  764                 *retval = 0;    /* XXX: Don't know */
  765                 break;
  766         case SVR4_CONFIG_SIGRT_MIN:
  767         case SVR4_CONFIG_SIGRT_MAX:
  768                 *retval = 0;    /* No real time signals */
  769                 break;
  770         case SVR4_CONFIG_TIMER_MAX:
  771                 *retval = 3;    /* XXX: real, virtual, profiling */
  772                 break;
  773 #if defined(NOTYET)
  774         case SVR4_CONFIG_PHYS_PAGES:
  775 #if defined(UVM)
  776                 *retval = uvmexp.free;  /* XXX: free instead of total */
  777 #else
  778                 *retval = cnt.v_free_count;     /* XXX: free instead of total */
  779 #endif
  780                 break;
  781         case SVR4_CONFIG_AVPHYS_PAGES:
  782 #if defined(UVM)
  783                 *retval = uvmexp.active;        /* XXX: active instead of avg */
  784 #else
  785                 *retval = cnt.v_active_count;   /* XXX: active instead of avg */
  786 #endif
  787                 break;
  788 #endif /* NOTYET */
  789         case SVR4_CONFIG_COHERENCY:
  790                 *retval = 0;    /* XXX */
  791                 break;
  792         case SVR4_CONFIG_SPLIT_CACHE:
  793                 *retval = 0;    /* XXX */
  794                 break;
  795         case SVR4_CONFIG_ICACHESZ:
  796                 *retval = 256;  /* XXX */
  797                 break;
  798         case SVR4_CONFIG_DCACHESZ:
  799                 *retval = 256;  /* XXX */
  800                 break;
  801         case SVR4_CONFIG_ICACHELINESZ:
  802                 *retval = 64;   /* XXX */
  803                 break;
  804         case SVR4_CONFIG_DCACHELINESZ:
  805                 *retval = 64;   /* XXX */
  806                 break;
  807         case SVR4_CONFIG_ICACHEBLKSZ:
  808                 *retval = 64;   /* XXX */
  809                 break;
  810         case SVR4_CONFIG_DCACHEBLKSZ:
  811                 *retval = 64;   /* XXX */
  812                 break;
  813         case SVR4_CONFIG_DCACHETBLKSZ:
  814                 *retval = 64;   /* XXX */
  815                 break;
  816         case SVR4_CONFIG_ICACHE_ASSOC:
  817                 *retval = 1;    /* XXX */
  818                 break;
  819         case SVR4_CONFIG_DCACHE_ASSOC:
  820                 *retval = 1;    /* XXX */
  821                 break;
  822         case SVR4_CONFIG_MAXPID:
  823                 *retval = PID_MAX;
  824                 break;
  825         case SVR4_CONFIG_STACK_PROT:
  826                 *retval = PROT_READ|PROT_WRITE|PROT_EXEC;
  827                 break;
  828         default:
  829                 return EINVAL;
  830         }
  831         return 0;
  832 }
  833 
  834 /* ARGSUSED */
  835 int
  836 svr4_sys_break(td, uap)
  837         struct thread *td;
  838         struct svr4_sys_break_args *uap;
  839 {
  840         struct obreak_args ap;
  841 
  842         ap.nsize = uap->nsize;
  843         return (obreak(td, &ap));
  844 }
  845 
  846 static __inline clock_t
  847 timeval_to_clock_t(tv)
  848         struct timeval *tv;
  849 {
  850         return tv->tv_sec * hz + tv->tv_usec / (1000000 / hz);
  851 }
  852 
  853 
  854 int
  855 svr4_sys_times(td, uap)
  856         struct thread *td;
  857         struct svr4_sys_times_args *uap;
  858 {
  859         struct timeval tv, utime, stime, cutime, cstime;
  860         struct tms tms;
  861         struct proc *p;
  862         int error;
  863 
  864         p = td->td_proc;
  865         PROC_LOCK(p);
  866         PROC_SLOCK(p);
  867         calcru(p, &utime, &stime);
  868         PROC_SUNLOCK(p);
  869         calccru(p, &cutime, &cstime);
  870         PROC_UNLOCK(p);
  871 
  872         tms.tms_utime = timeval_to_clock_t(&utime);
  873         tms.tms_stime = timeval_to_clock_t(&stime);
  874 
  875         tms.tms_cutime = timeval_to_clock_t(&cutime);
  876         tms.tms_cstime = timeval_to_clock_t(&cstime);
  877 
  878         error = copyout(&tms, uap->tp, sizeof(tms));
  879         if (error)
  880                 return (error);
  881 
  882         microtime(&tv);
  883         td->td_retval[0] = (int)timeval_to_clock_t(&tv);
  884         return (0);
  885 }
  886 
  887 
  888 int
  889 svr4_sys_ulimit(td, uap)
  890         struct thread *td;
  891         struct svr4_sys_ulimit_args *uap;
  892 {
  893         int *retval = td->td_retval;
  894         int error;
  895 
  896         switch (uap->cmd) {
  897         case SVR4_GFILLIM:
  898                 PROC_LOCK(td->td_proc);
  899                 *retval = lim_cur(td->td_proc, RLIMIT_FSIZE) / 512;
  900                 PROC_UNLOCK(td->td_proc);
  901                 if (*retval == -1)
  902                         *retval = 0x7fffffff;
  903                 return 0;
  904 
  905         case SVR4_SFILLIM:
  906                 {
  907                         struct rlimit krl;
  908 
  909                         krl.rlim_cur = uap->newlimit * 512;
  910                         PROC_LOCK(td->td_proc);
  911                         krl.rlim_max = lim_max(td->td_proc, RLIMIT_FSIZE);
  912                         PROC_UNLOCK(td->td_proc);
  913 
  914                         error = kern_setrlimit(td, RLIMIT_FSIZE, &krl);
  915                         if (error)
  916                                 return error;
  917 
  918                         PROC_LOCK(td->td_proc);
  919                         *retval = lim_cur(td->td_proc, RLIMIT_FSIZE);
  920                         PROC_UNLOCK(td->td_proc);
  921                         if (*retval == -1)
  922                                 *retval = 0x7fffffff;
  923                         return 0;
  924                 }
  925 
  926         case SVR4_GMEMLIM:
  927                 {
  928                         struct vmspace *vm = td->td_proc->p_vmspace;
  929                         register_t r;
  930 
  931                         PROC_LOCK(td->td_proc);
  932                         r = lim_cur(td->td_proc, RLIMIT_DATA);
  933                         PROC_UNLOCK(td->td_proc);
  934 
  935                         if (r == -1)
  936                                 r = 0x7fffffff;
  937                         r += (long) vm->vm_daddr;
  938                         if (r < 0)
  939                                 r = 0x7fffffff;
  940                         *retval = r;
  941                         return 0;
  942                 }
  943 
  944         case SVR4_GDESLIM:
  945                 PROC_LOCK(td->td_proc);
  946                 *retval = lim_cur(td->td_proc, RLIMIT_NOFILE);
  947                 PROC_UNLOCK(td->td_proc);
  948                 if (*retval == -1)
  949                         *retval = 0x7fffffff;
  950                 return 0;
  951 
  952         default:
  953                 return EINVAL;
  954         }
  955 }
  956 
  957 static struct proc *
  958 svr4_pfind(pid)
  959         pid_t pid;
  960 {
  961         struct proc *p;
  962 
  963         /* look in the live processes */
  964         if ((p = pfind(pid)) == NULL)
  965                 /* look in the zombies */
  966                 p = zpfind(pid);
  967 
  968         return p;
  969 }
  970 
  971 
  972 int
  973 svr4_sys_pgrpsys(td, uap)
  974         struct thread *td;
  975         struct svr4_sys_pgrpsys_args *uap;
  976 {
  977         int *retval = td->td_retval;
  978         struct proc *p = td->td_proc;
  979 
  980         switch (uap->cmd) {
  981         case 1:                 /* setpgrp() */
  982                 /*
  983                  * SVR4 setpgrp() (which takes no arguments) has the
  984                  * semantics that the session ID is also created anew, so
  985                  * in almost every sense, setpgrp() is identical to
  986                  * setsid() for SVR4.  (Under BSD, the difference is that
  987                  * a setpgid(0,0) will not create a new session.)
  988                  */
  989                 setsid(td, NULL);
  990                 /*FALLTHROUGH*/
  991 
  992         case 0:                 /* getpgrp() */
  993                 PROC_LOCK(p);
  994                 *retval = p->p_pgrp->pg_id;
  995                 PROC_UNLOCK(p);
  996                 return 0;
  997 
  998         case 2:                 /* getsid(pid) */
  999                 if (uap->pid == 0)
 1000                         PROC_LOCK(p);
 1001                 else if ((p = svr4_pfind(uap->pid)) == NULL)
 1002                         return ESRCH;
 1003                 /*
 1004                  * This has already been initialized to the pid of
 1005                  * the session leader.
 1006                  */
 1007                 *retval = (register_t) p->p_session->s_sid;
 1008                 PROC_UNLOCK(p);
 1009                 return 0;
 1010 
 1011         case 3:                 /* setsid() */
 1012                 return setsid(td, NULL);
 1013 
 1014         case 4:                 /* getpgid(pid) */
 1015 
 1016                 if (uap->pid == 0)
 1017                         PROC_LOCK(p);
 1018                 else if ((p = svr4_pfind(uap->pid)) == NULL)
 1019                         return ESRCH;
 1020 
 1021                 *retval = (int) p->p_pgrp->pg_id;
 1022                 PROC_UNLOCK(p);
 1023                 return 0;
 1024 
 1025         case 5:                 /* setpgid(pid, pgid); */
 1026                 {
 1027                         struct setpgid_args sa;
 1028 
 1029                         sa.pid = uap->pid;
 1030                         sa.pgid = uap->pgid;
 1031                         return setpgid(td, &sa);
 1032                 }
 1033 
 1034         default:
 1035                 return EINVAL;
 1036         }
 1037 }
 1038 
 1039 struct svr4_hrtcntl_args {
 1040         int                     cmd;
 1041         int                     fun;
 1042         int                     clk;
 1043         svr4_hrt_interval_t *   iv;
 1044         svr4_hrt_time_t *       ti;
 1045 };
 1046 
 1047 
 1048 static int
 1049 svr4_hrtcntl(td, uap, retval)
 1050         struct thread *td;
 1051         struct svr4_hrtcntl_args *uap;
 1052         register_t *retval;
 1053 {
 1054         switch (uap->fun) {
 1055         case SVR4_HRT_CNTL_RES:
 1056                 DPRINTF(("htrcntl(RES)\n"));
 1057                 *retval = SVR4_HRT_USEC;
 1058                 return 0;
 1059 
 1060         case SVR4_HRT_CNTL_TOFD:
 1061                 DPRINTF(("htrcntl(TOFD)\n"));
 1062                 {
 1063                         struct timeval tv;
 1064                         svr4_hrt_time_t t;
 1065                         if (uap->clk != SVR4_HRT_CLK_STD) {
 1066                                 DPRINTF(("clk == %d\n", uap->clk));
 1067                                 return EINVAL;
 1068                         }
 1069                         if (uap->ti == NULL) {
 1070                                 DPRINTF(("ti NULL\n"));
 1071                                 return EINVAL;
 1072                         }
 1073                         microtime(&tv);
 1074                         t.h_sec = tv.tv_sec;
 1075                         t.h_rem = tv.tv_usec;
 1076                         t.h_res = SVR4_HRT_USEC;
 1077                         return copyout(&t, uap->ti, sizeof(t));
 1078                 }
 1079 
 1080         case SVR4_HRT_CNTL_START:
 1081                 DPRINTF(("htrcntl(START)\n"));
 1082                 return ENOSYS;
 1083 
 1084         case SVR4_HRT_CNTL_GET:
 1085                 DPRINTF(("htrcntl(GET)\n"));
 1086                 return ENOSYS;
 1087         default:
 1088                 DPRINTF(("Bad htrcntl command %d\n", uap->fun));
 1089                 return ENOSYS;
 1090         }
 1091 }
 1092 
 1093 
 1094 int
 1095 svr4_sys_hrtsys(td, uap) 
 1096         struct thread *td;
 1097         struct svr4_sys_hrtsys_args *uap;
 1098 {
 1099         int *retval = td->td_retval;
 1100 
 1101         switch (uap->cmd) {
 1102         case SVR4_HRT_CNTL:
 1103                 return svr4_hrtcntl(td, (struct svr4_hrtcntl_args *) uap,
 1104                                     retval);
 1105 
 1106         case SVR4_HRT_ALRM:
 1107                 DPRINTF(("hrtalarm\n"));
 1108                 return ENOSYS;
 1109 
 1110         case SVR4_HRT_SLP:
 1111                 DPRINTF(("hrtsleep\n"));
 1112                 return ENOSYS;
 1113 
 1114         case SVR4_HRT_CAN:
 1115                 DPRINTF(("hrtcancel\n"));
 1116                 return ENOSYS;
 1117 
 1118         default:
 1119                 DPRINTF(("Bad hrtsys command %d\n", uap->cmd));
 1120                 return EINVAL;
 1121         }
 1122 }
 1123 
 1124 
 1125 static int
 1126 svr4_setinfo(pid, ru, st, s)
 1127         pid_t pid;
 1128         struct rusage *ru;
 1129         int st;
 1130         svr4_siginfo_t *s;
 1131 {
 1132         svr4_siginfo_t i;
 1133         int sig;
 1134 
 1135         memset(&i, 0, sizeof(i));
 1136 
 1137         i.svr4_si_signo = SVR4_SIGCHLD;
 1138         i.svr4_si_errno = 0;    /* XXX? */
 1139 
 1140         i.svr4_si_pid = pid;
 1141         if (ru) {
 1142                 i.svr4_si_stime = ru->ru_stime.tv_sec;
 1143                 i.svr4_si_utime = ru->ru_utime.tv_sec;
 1144         }
 1145 
 1146         if (WIFEXITED(st)) {
 1147                 i.svr4_si_status = WEXITSTATUS(st);
 1148                 i.svr4_si_code = SVR4_CLD_EXITED;
 1149         } else if (WIFSTOPPED(st)) {
 1150                 sig = WSTOPSIG(st);
 1151                 if (sig >= 0 && sig < NSIG)
 1152                         i.svr4_si_status = SVR4_BSD2SVR4_SIG(sig);
 1153 
 1154                 if (i.svr4_si_status == SVR4_SIGCONT)
 1155                         i.svr4_si_code = SVR4_CLD_CONTINUED;
 1156                 else
 1157                         i.svr4_si_code = SVR4_CLD_STOPPED;
 1158         } else {
 1159                 sig = WTERMSIG(st);
 1160                 if (sig >= 0 && sig < NSIG)
 1161                         i.svr4_si_status = SVR4_BSD2SVR4_SIG(sig);
 1162 
 1163                 if (WCOREDUMP(st))
 1164                         i.svr4_si_code = SVR4_CLD_DUMPED;
 1165                 else
 1166                         i.svr4_si_code = SVR4_CLD_KILLED;
 1167         }
 1168 
 1169         DPRINTF(("siginfo [pid %ld signo %d code %d errno %d status %d]\n",
 1170                  i.svr4_si_pid, i.svr4_si_signo, i.svr4_si_code, i.svr4_si_errno,
 1171                  i.svr4_si_status));
 1172 
 1173         return copyout(&i, s, sizeof(i));
 1174 }
 1175 
 1176 
 1177 int
 1178 svr4_sys_waitsys(td, uap)
 1179         struct thread *td;
 1180         struct svr4_sys_waitsys_args *uap;
 1181 {
 1182         struct rusage ru;
 1183         pid_t pid;
 1184         int nfound, status;
 1185         int error, *retval = td->td_retval;
 1186         struct proc *p, *q;
 1187 
 1188         DPRINTF(("waitsys(%d, %d, %p, %x)\n", 
 1189                  uap->grp, uap->id,
 1190                  uap->info, uap->options));
 1191 
 1192         q = td->td_proc;
 1193         switch (uap->grp) {
 1194         case SVR4_P_PID:
 1195                 pid = uap->id;
 1196                 break;
 1197 
 1198         case SVR4_P_PGID:
 1199                 PROC_LOCK(q);
 1200                 pid = -q->p_pgid;
 1201                 PROC_UNLOCK(q);
 1202                 break;
 1203 
 1204         case SVR4_P_ALL:
 1205                 pid = WAIT_ANY;
 1206                 break;
 1207 
 1208         default:
 1209                 return EINVAL;
 1210         }
 1211 
 1212         /* Hand off the easy cases to kern_wait(). */
 1213         if (!(uap->options & (SVR4_WNOWAIT)) &&
 1214             (uap->options & (SVR4_WEXITED | SVR4_WTRAPPED))) {
 1215                 int options;
 1216 
 1217                 options = 0;
 1218                 if (uap->options & SVR4_WSTOPPED)
 1219                         options |= WUNTRACED;
 1220                 if (uap->options & SVR4_WCONTINUED)
 1221                         options |= WCONTINUED;
 1222                 if (uap->options & SVR4_WNOHANG)
 1223                         options |= WNOHANG;
 1224 
 1225                 error = kern_wait(td, pid, &status, options, &ru);
 1226                 if (error)
 1227                         return (error);
 1228                 if (uap->options & SVR4_WNOHANG && *retval == 0)
 1229                         error = svr4_setinfo(*retval, NULL, 0, uap->info);
 1230                 else
 1231                         error = svr4_setinfo(*retval, &ru, status, uap->info);
 1232                 *retval = 0;
 1233                 return (error);
 1234         }
 1235 
 1236         /*
 1237          * Ok, handle the weird cases.  Either WNOWAIT is set (meaning we
 1238          * just want to see if there is a process to harvest, we don't
 1239          * want to actually harvest it), or WEXIT and WTRAPPED are clear
 1240          * meaning we want to ignore zombies.  Either way, we don't have
 1241          * to handle harvesting zombies here.  We do have to duplicate the
 1242          * other portions of kern_wait() though, especially for WCONTINUED
 1243          * and WSTOPPED.
 1244          */
 1245 loop:
 1246         nfound = 0;
 1247         sx_slock(&proctree_lock);
 1248         LIST_FOREACH(p, &q->p_children, p_sibling) {
 1249                 PROC_LOCK(p);
 1250                 if (pid != WAIT_ANY &&
 1251                     p->p_pid != pid && p->p_pgid != -pid) {
 1252                         PROC_UNLOCK(p);
 1253                         DPRINTF(("pid %d pgid %d != %d\n", p->p_pid,
 1254                                  p->p_pgid, pid));
 1255                         continue;
 1256                 }
 1257                 if (p_canwait(td, p)) {
 1258                         PROC_UNLOCK(p);
 1259                         continue;
 1260                 }
 1261 
 1262                 nfound++;
 1263 
 1264                 PROC_SLOCK(p);
 1265                 /*
 1266                  * See if we have a zombie.  If so, WNOWAIT should be set,
 1267                  * as otherwise we should have called kern_wait() up above.
 1268                  */
 1269                 if ((p->p_state == PRS_ZOMBIE) && 
 1270                     ((uap->options & (SVR4_WEXITED|SVR4_WTRAPPED)))) {
 1271                         PROC_SUNLOCK(p);
 1272                         KASSERT(uap->options & SVR4_WNOWAIT,
 1273                             ("WNOWAIT is clear"));
 1274 
 1275                         /* Found a zombie, so cache info in local variables. */
 1276                         pid = p->p_pid;
 1277                         status = p->p_xstat;
 1278                         ru = p->p_ru;
 1279                         PROC_SLOCK(p);
 1280                         calcru(p, &ru.ru_utime, &ru.ru_stime);
 1281                         PROC_SUNLOCK(p);
 1282                         PROC_UNLOCK(p);
 1283                         sx_sunlock(&proctree_lock);
 1284 
 1285                         /* Copy the info out to userland. */
 1286                         *retval = 0;
 1287                         DPRINTF(("found %d\n", pid));
 1288                         return (svr4_setinfo(pid, &ru, status, uap->info));
 1289                 }
 1290 
 1291                 /*
 1292                  * See if we have a stopped or continued process.
 1293                  * XXX: This duplicates the same code in kern_wait().
 1294                  */
 1295                 if ((p->p_flag & P_STOPPED_SIG) &&
 1296                     (p->p_suspcount == p->p_numthreads) &&
 1297                     (p->p_flag & P_WAITED) == 0 &&
 1298                     (p->p_flag & P_TRACED || uap->options & SVR4_WSTOPPED)) {
 1299                         PROC_SUNLOCK(p);
 1300                         if (((uap->options & SVR4_WNOWAIT)) == 0)
 1301                                 p->p_flag |= P_WAITED;
 1302                         sx_sunlock(&proctree_lock);
 1303                         pid = p->p_pid;
 1304                         status = W_STOPCODE(p->p_xstat);
 1305                         ru = p->p_ru;
 1306                         PROC_SLOCK(p);
 1307                         calcru(p, &ru.ru_utime, &ru.ru_stime);
 1308                         PROC_SUNLOCK(p);
 1309                         PROC_UNLOCK(p);
 1310 
 1311                         if (((uap->options & SVR4_WNOWAIT)) == 0) {
 1312                                 PROC_LOCK(q);
 1313                                 sigqueue_take(p->p_ksi);
 1314                                 PROC_UNLOCK(q);
 1315                         }
 1316 
 1317                         *retval = 0;
 1318                         DPRINTF(("jobcontrol %d\n", pid));
 1319                         return (svr4_setinfo(pid, &ru, status, uap->info));
 1320                 }
 1321                 PROC_SUNLOCK(p);
 1322                 if (uap->options & SVR4_WCONTINUED &&
 1323                     (p->p_flag & P_CONTINUED)) {
 1324                         sx_sunlock(&proctree_lock);
 1325                         if (((uap->options & SVR4_WNOWAIT)) == 0)
 1326                                 p->p_flag &= ~P_CONTINUED;
 1327                         pid = p->p_pid;
 1328                         ru = p->p_ru;
 1329                         status = SIGCONT;
 1330                         PROC_SLOCK(p);
 1331                         calcru(p, &ru.ru_utime, &ru.ru_stime);
 1332                         PROC_SUNLOCK(p);
 1333                         PROC_UNLOCK(p);
 1334 
 1335                         if (((uap->options & SVR4_WNOWAIT)) == 0) {
 1336                                 PROC_LOCK(q);
 1337                                 sigqueue_take(p->p_ksi);
 1338                                 PROC_UNLOCK(q);
 1339                         }
 1340 
 1341                         *retval = 0;
 1342                         DPRINTF(("jobcontrol %d\n", pid));
 1343                         return (svr4_setinfo(pid, &ru, status, uap->info));
 1344                 }
 1345                 PROC_UNLOCK(p);
 1346         }
 1347 
 1348         if (nfound == 0) {
 1349                 sx_sunlock(&proctree_lock);
 1350                 return (ECHILD);
 1351         }
 1352 
 1353         if (uap->options & SVR4_WNOHANG) {
 1354                 sx_sunlock(&proctree_lock);
 1355                 *retval = 0;
 1356                 return (svr4_setinfo(0, NULL, 0, uap->info));
 1357         }
 1358 
 1359         PROC_LOCK(q);
 1360         sx_sunlock(&proctree_lock);
 1361         if (q->p_flag & P_STATCHILD) {
 1362                 q->p_flag &= ~P_STATCHILD;
 1363                 error = 0;
 1364         } else
 1365                 error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "svr4_wait", 0);
 1366         PROC_UNLOCK(q);
 1367         if (error)
 1368                 return error;
 1369         goto loop;
 1370 }
 1371 
 1372 
 1373 static void
 1374 bsd_statfs_to_svr4_statvfs(bfs, sfs)
 1375         const struct statfs *bfs;
 1376         struct svr4_statvfs *sfs;
 1377 {
 1378         sfs->f_bsize = bfs->f_iosize; /* XXX */
 1379         sfs->f_frsize = bfs->f_bsize;
 1380         sfs->f_blocks = bfs->f_blocks;
 1381         sfs->f_bfree = bfs->f_bfree;
 1382         sfs->f_bavail = bfs->f_bavail;
 1383         sfs->f_files = bfs->f_files;
 1384         sfs->f_ffree = bfs->f_ffree;
 1385         sfs->f_favail = bfs->f_ffree;
 1386         sfs->f_fsid = bfs->f_fsid.val[0];
 1387         memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
 1388         sfs->f_flag = 0;
 1389         if (bfs->f_flags & MNT_RDONLY)
 1390                 sfs->f_flag |= SVR4_ST_RDONLY;
 1391         if (bfs->f_flags & MNT_NOSUID)
 1392                 sfs->f_flag |= SVR4_ST_NOSUID;
 1393         sfs->f_namemax = MAXNAMLEN;
 1394         memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
 1395         memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
 1396 }
 1397 
 1398 
 1399 static void
 1400 bsd_statfs_to_svr4_statvfs64(bfs, sfs)
 1401         const struct statfs *bfs;
 1402         struct svr4_statvfs64 *sfs;
 1403 {
 1404         sfs->f_bsize = bfs->f_iosize; /* XXX */
 1405         sfs->f_frsize = bfs->f_bsize;
 1406         sfs->f_blocks = bfs->f_blocks;
 1407         sfs->f_bfree = bfs->f_bfree;
 1408         sfs->f_bavail = bfs->f_bavail;
 1409         sfs->f_files = bfs->f_files;
 1410         sfs->f_ffree = bfs->f_ffree;
 1411         sfs->f_favail = bfs->f_ffree;
 1412         sfs->f_fsid = bfs->f_fsid.val[0];
 1413         memcpy(sfs->f_basetype, bfs->f_fstypename, sizeof(sfs->f_basetype));
 1414         sfs->f_flag = 0;
 1415         if (bfs->f_flags & MNT_RDONLY)
 1416                 sfs->f_flag |= SVR4_ST_RDONLY;
 1417         if (bfs->f_flags & MNT_NOSUID)
 1418                 sfs->f_flag |= SVR4_ST_NOSUID;
 1419         sfs->f_namemax = MAXNAMLEN;
 1420         memcpy(sfs->f_fstr, bfs->f_fstypename, sizeof(sfs->f_fstr)); /* XXX */
 1421         memset(sfs->f_filler, 0, sizeof(sfs->f_filler));
 1422 }
 1423 
 1424 
 1425 int
 1426 svr4_sys_statvfs(td, uap)
 1427         struct thread *td;
 1428         struct svr4_sys_statvfs_args *uap;
 1429 {
 1430         struct svr4_statvfs sfs;
 1431         struct statfs bfs;
 1432         char *path;
 1433         int error;
 1434 
 1435         CHECKALTEXIST(td, uap->path, &path);
 1436 
 1437         error = kern_statfs(td, path, UIO_SYSSPACE, &bfs);
 1438         free(path, M_TEMP);
 1439         if (error)
 1440                 return (error);
 1441         bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
 1442         return copyout(&sfs, uap->fs, sizeof(sfs));
 1443 }
 1444 
 1445 
 1446 int
 1447 svr4_sys_fstatvfs(td, uap)
 1448         struct thread *td;
 1449         struct svr4_sys_fstatvfs_args *uap;
 1450 {
 1451         struct svr4_statvfs sfs;
 1452         struct statfs bfs;
 1453         int error;
 1454 
 1455         error = kern_fstatfs(td, uap->fd, &bfs);
 1456         if (error)
 1457                 return (error);
 1458         bsd_statfs_to_svr4_statvfs(&bfs, &sfs);
 1459         return copyout(&sfs, uap->fs, sizeof(sfs));
 1460 }
 1461 
 1462 
 1463 int
 1464 svr4_sys_statvfs64(td, uap)
 1465         struct thread *td;
 1466         struct svr4_sys_statvfs64_args *uap;
 1467 {
 1468         struct svr4_statvfs64 sfs;
 1469         struct statfs bfs;
 1470         char *path;
 1471         int error;
 1472 
 1473         CHECKALTEXIST(td, uap->path, &path);
 1474 
 1475         error = kern_statfs(td, path, UIO_SYSSPACE, &bfs);
 1476         free(path, M_TEMP);
 1477         if (error)
 1478                 return (error);
 1479         bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
 1480         return copyout(&sfs, uap->fs, sizeof(sfs));
 1481 }
 1482 
 1483 
 1484 int
 1485 svr4_sys_fstatvfs64(td, uap) 
 1486         struct thread *td;
 1487         struct svr4_sys_fstatvfs64_args *uap;
 1488 {
 1489         struct svr4_statvfs64 sfs;
 1490         struct statfs bfs;
 1491         int error;
 1492 
 1493         error = kern_fstatfs(td, uap->fd, &bfs);
 1494         if (error)
 1495                 return (error);
 1496         bsd_statfs_to_svr4_statvfs64(&bfs, &sfs);
 1497         return copyout(&sfs, uap->fs, sizeof(sfs));
 1498 }
 1499 
 1500 int
 1501 svr4_sys_alarm(td, uap)
 1502         struct thread *td;
 1503         struct svr4_sys_alarm_args *uap;
 1504 {
 1505         struct itimerval itv, oitv;
 1506         int error;
 1507 
 1508         timevalclear(&itv.it_interval);
 1509         itv.it_value.tv_sec = uap->sec;
 1510         itv.it_value.tv_usec = 0;
 1511         error = kern_setitimer(td, ITIMER_REAL, &itv, &oitv);
 1512         if (error)
 1513                 return (error);
 1514         if (oitv.it_value.tv_usec != 0)
 1515                 oitv.it_value.tv_sec++;
 1516         td->td_retval[0] = oitv.it_value.tv_sec;
 1517         return (0);
 1518 }
 1519 
 1520 int
 1521 svr4_sys_gettimeofday(td, uap)
 1522         struct thread *td;
 1523         struct svr4_sys_gettimeofday_args *uap;
 1524 {
 1525         if (uap->tp) {
 1526                 struct timeval atv;
 1527 
 1528                 microtime(&atv);
 1529                 return copyout(&atv, uap->tp, sizeof (atv));
 1530         }
 1531 
 1532         return 0;
 1533 }
 1534 
 1535 int
 1536 svr4_sys_facl(td, uap)
 1537         struct thread *td;
 1538         struct svr4_sys_facl_args *uap;
 1539 {
 1540         int *retval;
 1541 
 1542         retval = td->td_retval;
 1543         *retval = 0;
 1544 
 1545         switch (uap->cmd) {
 1546         case SVR4_SYS_SETACL:
 1547                 /* We don't support acls on any filesystem */
 1548                 return ENOSYS;
 1549 
 1550         case SVR4_SYS_GETACL:
 1551                 return copyout(retval, &uap->num,
 1552                     sizeof(uap->num));
 1553 
 1554         case SVR4_SYS_GETACLCNT:
 1555                 return 0;
 1556 
 1557         default:
 1558                 return EINVAL;
 1559         }
 1560 }
 1561 
 1562 
 1563 int
 1564 svr4_sys_acl(td, uap)
 1565         struct thread *td;
 1566         struct svr4_sys_acl_args *uap;
 1567 {
 1568         /* XXX: for now the same */
 1569         return svr4_sys_facl(td, (struct svr4_sys_facl_args *)uap);
 1570 }
 1571 
 1572 int
 1573 svr4_sys_auditsys(td, uap)
 1574         struct thread *td;
 1575         struct svr4_sys_auditsys_args *uap;
 1576 {
 1577         /*
 1578          * XXX: Big brother is *not* watching.
 1579          */
 1580         return 0;
 1581 }
 1582 
 1583 int
 1584 svr4_sys_memcntl(td, uap)
 1585         struct thread *td;
 1586         struct svr4_sys_memcntl_args *uap;
 1587 {
 1588         switch (uap->cmd) {
 1589         case SVR4_MC_SYNC:
 1590                 {
 1591                         struct msync_args msa;
 1592 
 1593                         msa.addr = uap->addr;
 1594                         msa.len = uap->len;
 1595                         msa.flags = (int)uap->arg;
 1596 
 1597                         return msync(td, &msa);
 1598                 }
 1599         case SVR4_MC_ADVISE:
 1600                 {
 1601                         struct madvise_args maa;
 1602 
 1603                         maa.addr = uap->addr;
 1604                         maa.len = uap->len;
 1605                         maa.behav = (int)uap->arg;
 1606 
 1607                         return madvise(td, &maa);
 1608                 }
 1609         case SVR4_MC_LOCK:
 1610         case SVR4_MC_UNLOCK:
 1611         case SVR4_MC_LOCKAS:
 1612         case SVR4_MC_UNLOCKAS:
 1613                 return EOPNOTSUPP;
 1614         default:
 1615                 return ENOSYS;
 1616         }
 1617 }
 1618 
 1619 
 1620 int
 1621 svr4_sys_nice(td, uap)
 1622         struct thread *td;
 1623         struct svr4_sys_nice_args *uap;
 1624 {
 1625         struct setpriority_args ap;
 1626         int error;
 1627 
 1628         ap.which = PRIO_PROCESS;
 1629         ap.who = 0;
 1630         ap.prio = uap->prio;
 1631 
 1632         if ((error = setpriority(td, &ap)) != 0)
 1633                 return error;
 1634 
 1635         /* the cast is stupid, but the structures are the same */
 1636         if ((error = getpriority(td, (struct getpriority_args *)&ap)) != 0)
 1637                 return error;
 1638 
 1639         return 0;
 1640 }
 1641 
 1642 int
 1643 svr4_sys_resolvepath(td, uap)
 1644         struct thread *td;
 1645         struct svr4_sys_resolvepath_args *uap;
 1646 {
 1647         struct nameidata nd;
 1648         int error, *retval = td->td_retval;
 1649         unsigned int ncopy;
 1650 
 1651         NDINIT(&nd, LOOKUP, NOFOLLOW | SAVENAME | MPSAFE, UIO_USERSPACE,
 1652             uap->path, td);
 1653 
 1654         if ((error = namei(&nd)) != 0)
 1655                 return (error);
 1656         NDFREE(&nd, NDF_NO_FREE_PNBUF);
 1657         VFS_UNLOCK_GIANT(NDHASGIANT(&nd));
 1658 
 1659         ncopy = min(uap->bufsiz, strlen(nd.ni_cnd.cn_pnbuf) + 1);
 1660         if ((error = copyout(nd.ni_cnd.cn_pnbuf, uap->buf, ncopy)) != 0)
 1661                 goto bad;
 1662 
 1663         *retval = ncopy;
 1664 bad:
 1665         NDFREE(&nd, NDF_ONLY_PNBUF);
 1666         return error;
 1667 }

Cache object: ef82850b376dec4db126c2c567911e5f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.