Home | History | Annotate | Line # | Download | only in common
linux_llseek.c revision 1.13
      1  1.13      fvdl /*	$NetBSD: linux_llseek.c,v 1.13 1995/10/08 22:53:43 fvdl Exp $	*/
      2   1.1      fvdl 
      3   1.1      fvdl /*
      4   1.1      fvdl  * Copyright (c) 1995 Frank van der Linden
      5   1.1      fvdl  * All rights reserved.
      6   1.1      fvdl  *
      7   1.1      fvdl  * Redistribution and use in source and binary forms, with or without
      8   1.1      fvdl  * modification, are permitted provided that the following conditions
      9   1.1      fvdl  * are met:
     10   1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     11   1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     12   1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     15   1.1      fvdl  * 3. All advertising materials mentioning features or use of this software
     16   1.1      fvdl  *    must display the following acknowledgement:
     17   1.1      fvdl  *      This product includes software developed for the NetBSD Project
     18   1.1      fvdl  *      by Frank van der Linden
     19   1.1      fvdl  * 4. The name of the author may not be used to endorse or promote products
     20   1.1      fvdl  *    derived from this software without specific prior written permission
     21   1.1      fvdl  *
     22   1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1      fvdl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1      fvdl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1      fvdl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1      fvdl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1      fvdl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1      fvdl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1      fvdl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1      fvdl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1      fvdl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1      fvdl  */
     33   1.1      fvdl 
     34   1.1      fvdl #include <sys/param.h>
     35   1.1      fvdl #include <sys/systm.h>
     36   1.1      fvdl #include <sys/namei.h>
     37   1.1      fvdl #include <sys/proc.h>
     38   1.1      fvdl #include <sys/file.h>
     39   1.1      fvdl #include <sys/stat.h>
     40   1.1      fvdl #include <sys/filedesc.h>
     41   1.1      fvdl #include <sys/ioctl.h>
     42   1.1      fvdl #include <sys/kernel.h>
     43   1.1      fvdl #include <sys/mount.h>
     44   1.1      fvdl #include <sys/malloc.h>
     45  1.13      fvdl #include <sys/vnode.h>
     46  1.13      fvdl #include <sys/tty.h>
     47  1.13      fvdl #include <sys/conf.h>
     48   1.1      fvdl 
     49   1.1      fvdl #include <sys/syscallargs.h>
     50   1.1      fvdl 
     51   1.1      fvdl #include <compat/linux/linux_types.h>
     52   1.8   mycroft #include <compat/linux/linux_signal.h>
     53   1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     54   1.1      fvdl #include <compat/linux/linux_fcntl.h>
     55   1.1      fvdl #include <compat/linux/linux_util.h>
     56   1.1      fvdl 
     57   1.1      fvdl /*
     58   1.1      fvdl  * Some file-related calls are handled here. The usual flag conversion
     59   1.1      fvdl  * an structure conversion is done, and alternate emul path searching.
     60   1.1      fvdl  */
     61   1.1      fvdl 
     62   1.1      fvdl /*
     63   1.1      fvdl  * The next two functions convert between the Linux and NetBSD values
     64   1.1      fvdl  * of the flags used in open(2) and fcntl(2).
     65   1.1      fvdl  */
     66   1.1      fvdl static int
     67   1.1      fvdl linux_to_bsd_ioflags(int lflags)
     68   1.1      fvdl {
     69   1.1      fvdl 	int res = 0;
     70   1.1      fvdl 
     71   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
     72   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
     73   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
     74   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
     75   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
     76   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
     77   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
     78   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
     79   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
     80   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
     81   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
     82   1.1      fvdl 
     83   1.1      fvdl 	return res;
     84   1.1      fvdl }
     85   1.1      fvdl 
     86   1.1      fvdl static int
     87   1.1      fvdl bsd_to_linux_ioflags(int bflags)
     88   1.1      fvdl {
     89   1.1      fvdl 	int res = 0;
     90   1.1      fvdl 
     91   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
     92   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
     93   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
     94   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
     95   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
     96   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
     97   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
     98   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
     99   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
    100   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
    101   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
    102   1.1      fvdl 
    103   1.1      fvdl 	return res;
    104   1.1      fvdl }
    105   1.1      fvdl 
    106   1.1      fvdl /*
    107   1.1      fvdl  * creat(2) is an obsolete function, but it's present as a Linux
    108   1.1      fvdl  * system call, so let's deal with it.
    109   1.1      fvdl  *
    110   1.1      fvdl  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
    111   1.1      fvdl  */
    112   1.1      fvdl int
    113  1.12   mycroft linux_sys_creat(p, v, retval)
    114   1.1      fvdl 	struct proc *p;
    115  1.11   thorpej 	void *v;
    116  1.11   thorpej 	register_t *retval;
    117  1.11   thorpej {
    118  1.12   mycroft 	struct linux_sys_creat_args /* {
    119   1.1      fvdl 		syscallarg(char *) path;
    120   1.1      fvdl 		syscallarg(int) mode;
    121  1.11   thorpej 	} */ *uap = v;
    122  1.12   mycroft 	struct sys_open_args oa;
    123   1.1      fvdl 	caddr_t sg;
    124   1.1      fvdl 
    125   1.5  christos 	sg = stackgap_init(p->p_emul);
    126   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    127   1.1      fvdl 
    128   1.1      fvdl 	SCARG(&oa, path) = SCARG(uap, path);
    129   1.1      fvdl 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    130   1.1      fvdl 	SCARG(&oa, mode) = SCARG(uap, mode);
    131  1.12   mycroft 
    132  1.12   mycroft 	return sys_open(p, &oa, retval);
    133   1.1      fvdl }
    134   1.1      fvdl 
    135   1.1      fvdl /*
    136   1.1      fvdl  * open(2). Take care of the different flag values, and let the
    137   1.1      fvdl  * NetBSD syscall do the real work. See if this operation
    138   1.1      fvdl  * gives the current process a controlling terminal.
    139   1.1      fvdl  * (XXX is this necessary?)
    140   1.1      fvdl  */
    141   1.1      fvdl int
    142  1.12   mycroft linux_sys_open(p, v, retval)
    143   1.1      fvdl 	struct proc *p;
    144  1.11   thorpej 	void *v;
    145  1.11   thorpej 	register_t *retval;
    146  1.11   thorpej {
    147  1.12   mycroft 	struct linux_sys_open_args /* {
    148   1.1      fvdl 		syscallarg(char *) path;
    149   1.1      fvdl 		syscallarg(int) flags;
    150   1.1      fvdl 		syscallarg(int) mode;
    151  1.11   thorpej 	} */ *uap = v;
    152   1.1      fvdl 	int error, fl;
    153  1.12   mycroft 	struct sys_open_args boa;
    154   1.1      fvdl 	caddr_t sg;
    155   1.1      fvdl 
    156   1.5  christos 	sg = stackgap_init(p->p_emul);
    157   1.1      fvdl 
    158   1.2      fvdl 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    159   1.1      fvdl 
    160   1.2      fvdl 	if (fl & O_CREAT)
    161   1.5  christos 		LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    162   1.2      fvdl 	else
    163   1.5  christos 		LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    164   1.1      fvdl 
    165   1.1      fvdl 	SCARG(&boa, path) = SCARG(uap, path);
    166   1.1      fvdl 	SCARG(&boa, flags) = fl;
    167   1.1      fvdl 	SCARG(&boa, mode) = SCARG(uap, mode);
    168   1.2      fvdl 
    169  1.12   mycroft 	if ((error = sys_open(p, &boa, retval)))
    170   1.1      fvdl 		return error;
    171   1.1      fvdl 
    172   1.1      fvdl 	/*
    173   1.1      fvdl 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
    174   1.1      fvdl 	 * If we are a session leader, and we don't have a controlling
    175   1.1      fvdl 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
    176   1.1      fvdl 	 * this the controlling terminal.
    177   1.1      fvdl 	 */
    178   1.1      fvdl         if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
    179   1.1      fvdl                 struct filedesc *fdp = p->p_fd;
    180   1.1      fvdl                 struct file     *fp = fdp->fd_ofiles[*retval];
    181   1.1      fvdl 
    182   1.1      fvdl                 /* ignore any error, just give it a try */
    183   1.1      fvdl                 if (fp->f_type == DTYPE_VNODE)
    184   1.1      fvdl                         (fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, (caddr_t) 0, p);
    185   1.1      fvdl         }
    186   1.1      fvdl 	return 0;
    187   1.1      fvdl }
    188   1.1      fvdl 
    189   1.1      fvdl /*
    190   1.2      fvdl  * This appears to be part of a Linux attempt to switch to 64 bits file sizes.
    191   1.2      fvdl  */
    192   1.2      fvdl int
    193  1.12   mycroft linux_sys_llseek(p, v, retval)
    194   1.2      fvdl 	struct proc *p;
    195  1.11   thorpej 	void *v;
    196  1.11   thorpej 	register_t *retval;
    197  1.11   thorpej {
    198  1.12   mycroft 	struct linux_sys_llseek_args /* {
    199   1.2      fvdl 		syscallarg(int) fd;
    200   1.2      fvdl 		syscallarg(uint32_t) ohigh;
    201   1.2      fvdl 		syscallarg(uint32_t) olow;
    202   1.2      fvdl 		syscallarg(caddr_t) res;
    203   1.2      fvdl 		syscallarg(int) whence;
    204  1.11   thorpej 	} */ *uap = v;
    205  1.12   mycroft 	struct sys_lseek_args bla;
    206   1.2      fvdl 	int error;
    207   1.2      fvdl 	off_t off;
    208   1.2      fvdl 
    209   1.2      fvdl 	off = SCARG(uap, olow) | (((off_t) SCARG(uap, ohigh)) << 32);
    210   1.2      fvdl 
    211   1.2      fvdl 	SCARG(&bla, fd) = SCARG(uap, fd);
    212   1.2      fvdl 	SCARG(&bla, offset) = off;
    213   1.2      fvdl 	SCARG(&bla, whence) = SCARG(uap, whence);
    214   1.2      fvdl 
    215  1.12   mycroft 	if ((error = sys_lseek(p, &bla, retval)))
    216   1.2      fvdl 		return error;
    217   1.2      fvdl 
    218   1.2      fvdl 	if ((error = copyout(retval, SCARG(uap, res), sizeof (off_t))))
    219   1.2      fvdl 		return error;
    220   1.2      fvdl 
    221   1.2      fvdl 	retval[0] = 0;
    222   1.2      fvdl 	return 0;
    223   1.2      fvdl }
    224   1.2      fvdl 
    225   1.2      fvdl /*
    226   1.1      fvdl  * The next two functions take care of converting the flock
    227   1.1      fvdl  * structure back and forth between Linux and NetBSD format.
    228   1.1      fvdl  * The only difference in the structures is the order of
    229   1.1      fvdl  * the fields, and the 'whence' value.
    230   1.1      fvdl  */
    231   1.1      fvdl static void
    232   1.1      fvdl bsd_to_linux_flock(bfp, lfp)
    233   1.1      fvdl 	struct flock *bfp;
    234   1.1      fvdl 	struct linux_flock *lfp;
    235   1.1      fvdl {
    236  1.12   mycroft 
    237   1.1      fvdl 	lfp->l_start = bfp->l_start;
    238   1.1      fvdl 	lfp->l_len = bfp->l_len;
    239   1.1      fvdl 	lfp->l_pid = bfp->l_pid;
    240   1.3   mycroft 	lfp->l_whence = bfp->l_whence;
    241   1.3   mycroft 	switch (bfp->l_type) {
    242   1.1      fvdl 	case F_RDLCK:
    243   1.3   mycroft 		lfp->l_type = LINUX_F_RDLCK;
    244   1.1      fvdl 		break;
    245   1.1      fvdl 	case F_UNLCK:
    246   1.3   mycroft 		lfp->l_type = LINUX_F_UNLCK;
    247   1.1      fvdl 		break;
    248   1.1      fvdl 	case F_WRLCK:
    249   1.3   mycroft 		lfp->l_type = LINUX_F_WRLCK;
    250   1.1      fvdl 		break;
    251   1.1      fvdl 	}
    252   1.1      fvdl }
    253   1.1      fvdl 
    254   1.1      fvdl static void
    255   1.1      fvdl linux_to_bsd_flock(lfp, bfp)
    256   1.1      fvdl 	struct linux_flock *lfp;
    257   1.1      fvdl 	struct flock *bfp;
    258   1.1      fvdl {
    259  1.12   mycroft 
    260   1.1      fvdl 	bfp->l_start = lfp->l_start;
    261   1.1      fvdl 	bfp->l_len = lfp->l_len;
    262   1.1      fvdl 	bfp->l_pid = lfp->l_pid;
    263   1.3   mycroft 	bfp->l_whence = lfp->l_whence;
    264   1.3   mycroft 	switch (lfp->l_type) {
    265   1.1      fvdl 	case LINUX_F_RDLCK:
    266   1.3   mycroft 		bfp->l_type = F_RDLCK;
    267   1.1      fvdl 		break;
    268   1.1      fvdl 	case LINUX_F_UNLCK:
    269   1.3   mycroft 		bfp->l_type = F_UNLCK;
    270   1.1      fvdl 		break;
    271   1.1      fvdl 	case LINUX_F_WRLCK:
    272   1.3   mycroft 		bfp->l_type = F_WRLCK;
    273   1.1      fvdl 		break;
    274   1.1      fvdl 	}
    275   1.1      fvdl }
    276   1.1      fvdl 
    277   1.1      fvdl /*
    278   1.1      fvdl  * Most actions in the fcntl() call are straightforward; simply
    279   1.1      fvdl  * pass control to the NetBSD system call. A few commands need
    280   1.1      fvdl  * conversions after the actual system call has done its work,
    281   1.1      fvdl  * because the flag values and lock structure are different.
    282   1.1      fvdl  */
    283   1.1      fvdl int
    284  1.12   mycroft linux_sys_fcntl(p, v, retval)
    285   1.1      fvdl 	struct proc *p;
    286  1.11   thorpej 	void *v;
    287  1.11   thorpej 	register_t *retval;
    288  1.11   thorpej {
    289  1.12   mycroft 	struct linux_sys_fcntl_args /* {
    290   1.1      fvdl 		syscallarg(int) fd;
    291   1.1      fvdl 		syscallarg(int) cmd;
    292   1.1      fvdl 		syscallarg(void *) arg;
    293  1.11   thorpej 	} */ *uap = v;
    294   1.6      fvdl 	int fd, cmd, error, val;
    295   1.1      fvdl 	caddr_t arg, sg;
    296   1.1      fvdl 	struct linux_flock lfl;
    297   1.1      fvdl 	struct flock *bfp, bfl;
    298  1.12   mycroft 	struct sys_fcntl_args fca;
    299  1.13      fvdl 	struct filedesc *fdp;
    300  1.13      fvdl 	struct file *fp;
    301  1.13      fvdl 	struct vnode *vp;
    302  1.13      fvdl 	struct vattr va;
    303  1.13      fvdl 	long pgid;
    304  1.13      fvdl 	struct pgrp *pgrp;
    305  1.13      fvdl 	struct tty *tp, *(*d_tty) __P((dev_t));
    306   1.1      fvdl 
    307   1.1      fvdl 	fd = SCARG(uap, fd);
    308   1.1      fvdl 	cmd = SCARG(uap, cmd);
    309   1.1      fvdl 	arg = (caddr_t) SCARG(uap, arg);
    310   1.1      fvdl 
    311   1.1      fvdl 	switch (cmd) {
    312   1.1      fvdl 	case LINUX_F_DUPFD:
    313   1.1      fvdl 		cmd = F_DUPFD;
    314   1.1      fvdl 		break;
    315   1.1      fvdl 	case LINUX_F_GETFD:
    316   1.1      fvdl 		cmd = F_GETFD;
    317   1.1      fvdl 		break;
    318   1.1      fvdl 	case LINUX_F_SETFD:
    319   1.1      fvdl 		cmd = F_SETFD;
    320   1.1      fvdl 		break;
    321   1.1      fvdl 	case LINUX_F_GETFL:
    322   1.1      fvdl 		SCARG(&fca, fd) = fd;
    323   1.1      fvdl 		SCARG(&fca, cmd) = F_GETFL;
    324   1.1      fvdl 		SCARG(&fca, arg) = arg;
    325  1.12   mycroft 		if ((error = sys_fcntl(p, &fca, retval)))
    326   1.1      fvdl 			return error;
    327   1.1      fvdl 		retval[0] = bsd_to_linux_ioflags(retval[0]);
    328   1.1      fvdl 		return 0;
    329   1.1      fvdl 	case LINUX_F_SETFL:
    330   1.6      fvdl 		val = linux_to_bsd_ioflags((int)SCARG(uap, arg));
    331   1.1      fvdl 		SCARG(&fca, fd) = fd;
    332   1.1      fvdl 		SCARG(&fca, cmd) = F_SETFL;
    333   1.6      fvdl 		SCARG(&fca, arg) = (caddr_t) val;
    334  1.12   mycroft 		return sys_fcntl(p, &fca, retval);
    335   1.1      fvdl 	case LINUX_F_GETLK:
    336   1.5  christos 		sg = stackgap_init(p->p_emul);
    337   1.1      fvdl 		bfp = (struct flock *) stackgap_alloc(&sg, sizeof *bfp);
    338   1.1      fvdl 		SCARG(&fca, fd) = fd;
    339   1.1      fvdl 		SCARG(&fca, cmd) = F_GETLK;
    340   1.1      fvdl 		SCARG(&fca, arg) = bfp;
    341  1.12   mycroft 		if ((error = sys_fcntl(p, &fca, retval)))
    342   1.1      fvdl 			return error;
    343   1.1      fvdl 		if ((error = copyin(bfp, &bfl, sizeof bfl)))
    344   1.1      fvdl 			return error;
    345   1.1      fvdl 		bsd_to_linux_flock(&bfl, &lfl);
    346   1.1      fvdl 		return copyout(&lfl, arg, sizeof lfl);
    347   1.1      fvdl 		break;
    348   1.1      fvdl 	case LINUX_F_SETLK:
    349   1.1      fvdl 	case LINUX_F_SETLKW:
    350   1.1      fvdl 		cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
    351   1.1      fvdl 		if ((error = copyin(arg, &lfl, sizeof lfl)))
    352   1.1      fvdl 			return error;
    353   1.1      fvdl 		linux_to_bsd_flock(&lfl, &bfl);
    354   1.5  christos 		sg = stackgap_init(p->p_emul);
    355   1.1      fvdl 		bfp = (struct flock *) stackgap_alloc(&sg, sizeof *bfp);
    356   1.1      fvdl 		if ((error = copyout(&bfl, bfp, sizeof bfl)))
    357   1.1      fvdl 			return error;
    358   1.1      fvdl 		SCARG(&fca, fd) = fd;
    359   1.1      fvdl 		SCARG(&fca, cmd) = cmd;
    360   1.1      fvdl 		SCARG(&fca, arg) = bfp;
    361  1.12   mycroft 		return sys_fcntl(p, &fca, retval);
    362   1.1      fvdl 		break;
    363   1.1      fvdl 	case LINUX_F_SETOWN:
    364  1.13      fvdl 	case LINUX_F_GETOWN:
    365  1.13      fvdl 		/*
    366  1.13      fvdl 		 * We need to route around the normal fcntl() for these calls,
    367  1.13      fvdl 		 * since it uses TIOC{G,S}PGRP, which is too restrictive for
    368  1.13      fvdl 		 * Linux F_{G,S}ETOWN semantics. For sockets, this problem
    369  1.13      fvdl 		 * does not exist.
    370  1.13      fvdl 		 */
    371  1.13      fvdl 		fdp = p->p_fd;
    372  1.13      fvdl 		if ((u_int)fd >= fdp->fd_nfiles ||
    373  1.13      fvdl 		    (fp = fdp->fd_ofiles[fd]) == NULL)
    374  1.13      fvdl 			return EBADF;
    375  1.13      fvdl 		if (fp->f_type == DTYPE_SOCKET) {
    376  1.13      fvdl 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
    377  1.13      fvdl 			break;
    378  1.13      fvdl 		}
    379  1.13      fvdl 		vp = (struct vnode *)fp->f_data;
    380  1.13      fvdl 		if (vp->v_type != VCHR)
    381  1.13      fvdl 			return EINVAL;
    382  1.13      fvdl 		if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    383  1.13      fvdl 			return error;
    384  1.13      fvdl 		d_tty = cdevsw[major(va.va_rdev)].d_tty;
    385  1.13      fvdl 		if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
    386  1.13      fvdl 			return EINVAL;
    387  1.13      fvdl 		if (cmd == LINUX_F_GETOWN) {
    388  1.13      fvdl 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
    389  1.13      fvdl 			return 0;
    390  1.13      fvdl 		}
    391  1.13      fvdl 		if ((long)arg <= 0) {
    392  1.13      fvdl 			pgid = -(long)arg;
    393  1.13      fvdl 		} else {
    394  1.13      fvdl 			struct proc *p1 = pfind((long)arg);
    395  1.13      fvdl 			if (p1 == 0)
    396  1.13      fvdl 				return (ESRCH);
    397  1.13      fvdl 			pgid = (long)p1->p_pgrp->pg_id;
    398  1.13      fvdl 		}
    399  1.13      fvdl 		pgrp = pgfind(pgid);
    400  1.13      fvdl 		if (pgrp == NULL || pgrp->pg_session != p->p_session)
    401  1.13      fvdl 			return EPERM;
    402  1.13      fvdl 		tp->t_pgrp = pgrp;
    403  1.13      fvdl 		return 0;
    404   1.1      fvdl 	default:
    405   1.1      fvdl 		return EOPNOTSUPP;
    406   1.1      fvdl 	}
    407   1.1      fvdl 
    408   1.1      fvdl 	SCARG(&fca, fd) = fd;
    409   1.1      fvdl 	SCARG(&fca, cmd) = cmd;
    410   1.1      fvdl 	SCARG(&fca, arg) = arg;
    411  1.12   mycroft 
    412  1.12   mycroft 	return sys_fcntl(p, &fca, retval);
    413   1.1      fvdl }
    414   1.1      fvdl 
    415   1.1      fvdl /*
    416   1.1      fvdl  * Convert a NetBSD stat structure to a Linux stat structure.
    417   1.1      fvdl  * Only the order of the fields and the padding in the structure
    418   1.9      fvdl  * is different. linux_fakedev is a machine-dependent function
    419   1.9      fvdl  * which optionally converts device driver major/minor numbers
    420   1.9      fvdl  * (XXX horrible, but what can you do against code that compares
    421   1.9      fvdl  * things against constant major device numbers? sigh)
    422   1.1      fvdl  */
    423   1.1      fvdl static void
    424   1.1      fvdl bsd_to_linux_stat(bsp, lsp)
    425   1.1      fvdl 	struct stat *bsp;
    426   1.1      fvdl 	struct linux_stat *lsp;
    427   1.1      fvdl {
    428  1.12   mycroft 
    429   1.1      fvdl 	lsp->lst_dev     = bsp->st_dev;
    430   1.1      fvdl 	lsp->lst_ino     = bsp->st_ino;
    431   1.1      fvdl 	lsp->lst_mode    = bsp->st_mode;
    432   1.1      fvdl 	lsp->lst_nlink   = bsp->st_nlink;
    433   1.1      fvdl 	lsp->lst_uid     = bsp->st_uid;
    434   1.1      fvdl 	lsp->lst_gid     = bsp->st_gid;
    435   1.9      fvdl 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev);
    436   1.1      fvdl 	lsp->lst_size    = bsp->st_size;
    437   1.1      fvdl 	lsp->lst_blksize = bsp->st_blksize;
    438   1.1      fvdl 	lsp->lst_blocks  = bsp->st_blocks;
    439   1.1      fvdl 	lsp->lst_atime   = bsp->st_atime;
    440   1.1      fvdl 	lsp->lst_mtime   = bsp->st_mtime;
    441   1.1      fvdl 	lsp->lst_ctime   = bsp->st_ctime;
    442   1.1      fvdl }
    443   1.1      fvdl 
    444   1.1      fvdl /*
    445   1.1      fvdl  * The stat functions below are plain sailing. stat and lstat are handled
    446   1.1      fvdl  * by one function to avoid code duplication.
    447   1.1      fvdl  */
    448   1.1      fvdl int
    449  1.12   mycroft linux_sys_fstat(p, v, retval)
    450   1.1      fvdl 	struct proc *p;
    451  1.11   thorpej 	void *v;
    452  1.11   thorpej 	register_t *retval;
    453  1.11   thorpej {
    454  1.12   mycroft 	struct linux_sys_fstat_args /* {
    455   1.1      fvdl 		syscallarg(int) fd;
    456   1.1      fvdl 		syscallarg(linux_stat *) sp;
    457  1.11   thorpej 	} */ *uap = v;
    458  1.12   mycroft 	struct sys_fstat_args fsa;
    459   1.1      fvdl 	struct linux_stat tmplst;
    460   1.1      fvdl 	struct stat *st,tmpst;
    461   1.1      fvdl 	caddr_t sg;
    462   1.1      fvdl 	int error;
    463   1.1      fvdl 
    464   1.5  christos 	sg = stackgap_init(p->p_emul);
    465   1.1      fvdl 
    466   1.1      fvdl 	st = stackgap_alloc(&sg, sizeof (struct stat));
    467   1.1      fvdl 
    468   1.1      fvdl 	SCARG(&fsa, fd) = SCARG(uap, fd);
    469   1.1      fvdl 	SCARG(&fsa, sb) = st;
    470   1.1      fvdl 
    471  1.12   mycroft 	if ((error = sys_fstat(p, &fsa, retval)))
    472   1.1      fvdl 		return error;
    473   1.1      fvdl 
    474   1.1      fvdl 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    475   1.1      fvdl 		return error;
    476   1.1      fvdl 
    477   1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    478   1.1      fvdl 
    479   1.1      fvdl 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    480   1.1      fvdl 		return error;
    481   1.1      fvdl 
    482   1.1      fvdl 	return 0;
    483   1.1      fvdl }
    484   1.1      fvdl 
    485   1.1      fvdl static int
    486   1.1      fvdl linux_stat1(p, uap, retval, dolstat)
    487   1.1      fvdl 	struct proc *p;
    488  1.12   mycroft 	struct linux_sys_stat_args *uap;
    489   1.1      fvdl 	register_t *retval;
    490   1.1      fvdl 	int dolstat;
    491   1.1      fvdl {
    492  1.12   mycroft 	struct sys_stat_args sa;
    493   1.1      fvdl 	struct linux_stat tmplst;
    494   1.1      fvdl 	struct stat *st, tmpst;
    495   1.1      fvdl 	caddr_t sg;
    496   1.1      fvdl 	int error;
    497   1.1      fvdl 
    498   1.5  christos 	sg = stackgap_init(p->p_emul);
    499   1.1      fvdl 
    500   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    501   1.1      fvdl 
    502   1.1      fvdl 	st = stackgap_alloc(&sg, sizeof (struct stat));
    503   1.1      fvdl 	SCARG(&sa, ub) = st;
    504   1.1      fvdl 	SCARG(&sa, path) = SCARG(uap, path);
    505   1.1      fvdl 
    506  1.12   mycroft 	if ((error = (dolstat ? sys_lstat(p, &sa, retval) :
    507  1.12   mycroft 				sys_stat(p, &sa, retval))))
    508   1.1      fvdl 		return error;
    509   1.1      fvdl 
    510   1.1      fvdl 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    511   1.1      fvdl 		return error;
    512   1.1      fvdl 
    513   1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    514   1.1      fvdl 
    515   1.1      fvdl 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    516   1.1      fvdl 		return error;
    517   1.1      fvdl 
    518   1.1      fvdl 	return 0;
    519   1.1      fvdl }
    520   1.1      fvdl 
    521   1.1      fvdl int
    522  1.12   mycroft linux_sys_stat(p, v, retval)
    523   1.1      fvdl 	struct proc *p;
    524  1.11   thorpej 	void *v;
    525  1.11   thorpej 	register_t *retval;
    526  1.11   thorpej {
    527  1.12   mycroft 	struct linux_sys_stat_args /* {
    528   1.1      fvdl 		syscallarg(char *) path;
    529   1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    530  1.11   thorpej 	} */ *uap = v;
    531  1.11   thorpej 
    532   1.1      fvdl 	return linux_stat1(p, uap, retval, 0);
    533   1.1      fvdl }
    534   1.1      fvdl 
    535   1.1      fvdl int
    536  1.12   mycroft linux_sys_lstat(p, v, retval)
    537   1.1      fvdl 	struct proc *p;
    538  1.11   thorpej 	void *v;
    539  1.11   thorpej 	register_t *retval;
    540  1.11   thorpej {
    541  1.12   mycroft 	struct linux_sys_lstat_args /* {
    542   1.1      fvdl 		syscallarg(char *) path;
    543   1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    544  1.11   thorpej 	} */ *uap = v;
    545  1.11   thorpej 
    546   1.1      fvdl 	return linux_stat1(p, uap, retval, 1);
    547   1.1      fvdl }
    548   1.1      fvdl 
    549   1.1      fvdl /*
    550  1.10      fvdl  * The following syscalls are mostly here because of the alternate path check.
    551   1.1      fvdl  */
    552   1.1      fvdl int
    553  1.12   mycroft linux_sys_access(p, v, retval)
    554   1.1      fvdl 	struct proc *p;
    555  1.11   thorpej 	void *v;
    556  1.11   thorpej 	register_t *retval;
    557  1.11   thorpej {
    558  1.12   mycroft 	struct linux_sys_access_args /* {
    559   1.1      fvdl 		syscallarg(char *) path;
    560   1.1      fvdl 		syscallarg(int) flags;
    561  1.11   thorpej 	} */ *uap = v;
    562   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    563   1.1      fvdl 
    564   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    565   1.1      fvdl 
    566  1.12   mycroft 	return sys_access(p, uap, retval);
    567   1.2      fvdl }
    568   1.2      fvdl 
    569   1.2      fvdl int
    570  1.12   mycroft linux_sys_unlink(p, v, retval)
    571   1.2      fvdl 	struct proc *p;
    572  1.11   thorpej 	void *v;
    573   1.2      fvdl 	register_t *retval;
    574   1.2      fvdl 
    575   1.2      fvdl {
    576  1.12   mycroft 	struct linux_sys_unlink_args /* {
    577  1.11   thorpej 		syscallarg(char *) path;
    578  1.11   thorpej 	} */ *uap = v;
    579   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    580   1.2      fvdl 
    581   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    582   1.2      fvdl 
    583  1.12   mycroft 	return sys_unlink(p, uap, retval);
    584   1.2      fvdl }
    585   1.2      fvdl 
    586  1.10      fvdl int
    587  1.12   mycroft linux_sys_chdir(p, v, retval)
    588   1.2      fvdl 	struct proc *p;
    589  1.11   thorpej 	void *v;
    590  1.11   thorpej 	register_t *retval;
    591  1.11   thorpej {
    592  1.12   mycroft 	struct linux_sys_chdir_args /* {
    593   1.2      fvdl 		syscallarg(char *) path;
    594  1.11   thorpej 	} */ *uap = v;
    595   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    596   1.2      fvdl 
    597   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    598   1.2      fvdl 
    599  1.12   mycroft 	return sys_chdir(p, uap, retval);
    600   1.2      fvdl }
    601   1.2      fvdl 
    602   1.2      fvdl int
    603  1.12   mycroft linux_sys_mknod(p, v, retval)
    604   1.2      fvdl 	struct proc *p;
    605  1.11   thorpej 	void *v;
    606  1.11   thorpej 	register_t *retval;
    607  1.11   thorpej {
    608  1.12   mycroft 	struct linux_sys_mknod_args /* {
    609   1.2      fvdl 		syscallarg(char *) path;
    610   1.2      fvdl 		syscallarg(int) mode;
    611   1.2      fvdl 		syscallarg(int) dev;
    612  1.11   thorpej 	} */ *uap = v;
    613   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    614  1.12   mycroft 	struct sys_mkfifo_args bma;
    615   1.2      fvdl 
    616   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    617   1.2      fvdl 
    618  1.10      fvdl 	/*
    619  1.10      fvdl 	 * BSD handles FIFOs seperately
    620  1.10      fvdl 	 */
    621  1.10      fvdl 	if (SCARG(uap, mode) & S_IFIFO) {
    622  1.10      fvdl 		SCARG(&bma, path) = SCARG(uap, path);
    623  1.10      fvdl 		SCARG(&bma, mode) = SCARG(uap, mode);
    624  1.12   mycroft 		return sys_mkfifo(p, uap, retval);
    625  1.10      fvdl 	} else
    626  1.12   mycroft 		return sys_mknod(p, uap, retval);
    627   1.2      fvdl }
    628   1.2      fvdl 
    629   1.2      fvdl int
    630  1.12   mycroft linux_sys_chmod(p, v, retval)
    631   1.2      fvdl 	struct proc *p;
    632  1.11   thorpej 	void *v;
    633  1.11   thorpej 	register_t *retval;
    634  1.11   thorpej {
    635  1.12   mycroft 	struct linux_sys_chmod_args /* {
    636   1.2      fvdl 		syscallarg(char *) path;
    637   1.2      fvdl 		syscallarg(int) mode;
    638  1.11   thorpej 	} */ *uap = v;
    639   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    640   1.2      fvdl 
    641   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    642   1.2      fvdl 
    643  1.12   mycroft 	return sys_chmod(p, uap, retval);
    644   1.2      fvdl }
    645   1.2      fvdl 
    646   1.2      fvdl int
    647  1.12   mycroft linux_sys_chown(p, v, retval)
    648   1.2      fvdl 	struct proc *p;
    649  1.11   thorpej 	void *v;
    650  1.11   thorpej 	register_t *retval;
    651  1.11   thorpej {
    652  1.12   mycroft 	struct linux_sys_chown_args /* {
    653   1.2      fvdl 		syscallarg(char *) path;
    654   1.2      fvdl 		syscallarg(int) uid;
    655   1.2      fvdl 		syscallarg(int) gid;
    656  1.11   thorpej 	} */ *uap = v;
    657  1.12   mycroft 	struct sys_chown_args bca;
    658   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    659   1.2      fvdl 
    660   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    661   1.2      fvdl 
    662  1.10      fvdl 	SCARG(&bca, path) = SCARG(uap, path);
    663  1.10      fvdl 	SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    664  1.10      fvdl 		(uid_t)-1 : SCARG(uap, uid);
    665  1.10      fvdl 	SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    666  1.10      fvdl 		(gid_t)-1 : SCARG(uap, gid);
    667  1.10      fvdl 
    668  1.12   mycroft 	return sys_chown(p, &bca, retval);
    669  1.10      fvdl }
    670  1.10      fvdl 
    671  1.10      fvdl int
    672  1.12   mycroft linux_sys_fchown(p, v, retval)
    673  1.10      fvdl 	struct proc *p;
    674  1.11   thorpej 	void *v;
    675  1.11   thorpej 	register_t *retval;
    676  1.11   thorpej {
    677  1.12   mycroft 	struct linux_sys_fchown_args /* {
    678  1.10      fvdl 		syscallarg(int) fd;
    679  1.10      fvdl 		syscallarg(int) uid;
    680  1.10      fvdl 		syscallarg(int) gid;
    681  1.11   thorpej 	} */ *uap = v;
    682  1.12   mycroft 	struct sys_fchown_args bfa;
    683  1.10      fvdl 
    684  1.10      fvdl 	SCARG(&bfa, fd) = SCARG(uap, fd);
    685  1.10      fvdl 	SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    686  1.10      fvdl 		(uid_t)-1 : SCARG(uap, uid);
    687  1.10      fvdl 	SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    688  1.10      fvdl 		(gid_t)-1 : SCARG(uap, gid);
    689  1.10      fvdl 
    690  1.12   mycroft 	return sys_fchown(p, &bfa, retval);
    691   1.2      fvdl }
    692   1.2      fvdl 
    693   1.2      fvdl int
    694  1.12   mycroft linux_sys_rename(p, v, retval)
    695   1.2      fvdl 	struct proc *p;
    696  1.11   thorpej 	void *v;
    697  1.11   thorpej 	register_t *retval;
    698  1.11   thorpej {
    699  1.12   mycroft 	struct linux_sys_rename_args /* {
    700   1.2      fvdl 		syscallarg(char *) from;
    701   1.2      fvdl 		syscallarg(char *) to;
    702  1.11   thorpej 	} */ *uap = v;
    703   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    704   1.2      fvdl 
    705   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, from));
    706   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
    707   1.2      fvdl 
    708  1.12   mycroft 	return sys_rename(p, uap, retval);
    709   1.2      fvdl }
    710   1.2      fvdl 
    711   1.2      fvdl int
    712  1.12   mycroft linux_sys_mkdir(p, v, retval)
    713   1.2      fvdl 	struct proc *p;
    714  1.11   thorpej 	void *v;
    715  1.11   thorpej 	register_t *retval;
    716  1.11   thorpej {
    717  1.12   mycroft 	struct linux_sys_mkdir_args /* {
    718   1.2      fvdl 		syscallarg(char *) path;
    719   1.7      fvdl 		syscallarg(int) mode;
    720  1.11   thorpej 	} */ *uap = v;
    721   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    722   1.2      fvdl 
    723   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    724  1.12   mycroft 
    725  1.12   mycroft 	return sys_mkdir(p, uap, retval);
    726   1.2      fvdl }
    727   1.2      fvdl 
    728   1.2      fvdl int
    729  1.12   mycroft linux_sys_rmdir(p, v, retval)
    730   1.2      fvdl 	struct proc *p;
    731  1.11   thorpej 	void *v;
    732  1.11   thorpej 	register_t *retval;
    733  1.11   thorpej {
    734  1.12   mycroft 	struct linux_sys_rmdir_args /* {
    735   1.2      fvdl 		syscallarg(char *) path;
    736  1.11   thorpej 	} */ *uap = v;
    737   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    738   1.2      fvdl 
    739   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    740  1.12   mycroft 
    741  1.12   mycroft 	return sys_rmdir(p, uap, retval);
    742   1.2      fvdl }
    743   1.2      fvdl 
    744   1.2      fvdl int
    745  1.12   mycroft linux_sys_symlink(p, v, retval)
    746   1.2      fvdl 	struct proc *p;
    747  1.11   thorpej 	void *v;
    748  1.11   thorpej 	register_t *retval;
    749  1.11   thorpej {
    750  1.12   mycroft 	struct linux_sys_symlink_args /* {
    751   1.2      fvdl 		syscallarg(char *) path;
    752   1.2      fvdl 		syscallarg(char *) to;
    753  1.11   thorpej 	} */ *uap = v;
    754   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    755   1.2      fvdl 
    756   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    757   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
    758   1.2      fvdl 
    759  1.12   mycroft 	return sys_symlink(p, uap, retval);
    760   1.2      fvdl }
    761   1.2      fvdl 
    762   1.2      fvdl int
    763  1.12   mycroft linux_sys_readlink(p, v, retval)
    764   1.2      fvdl 	struct proc *p;
    765  1.11   thorpej 	void *v;
    766  1.11   thorpej 	register_t *retval;
    767  1.11   thorpej {
    768  1.12   mycroft 	struct linux_sys_readlink_args /* {
    769   1.2      fvdl 		syscallarg(char *) name;
    770   1.2      fvdl 		syscallarg(char *) buf;
    771   1.2      fvdl 		syscallarg(int) count;
    772  1.11   thorpej 	} */ *uap = v;
    773   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    774   1.2      fvdl 
    775   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, name));
    776  1.12   mycroft 
    777  1.12   mycroft 	return sys_readlink(p, uap, retval);
    778   1.2      fvdl }
    779   1.2      fvdl 
    780   1.2      fvdl int
    781  1.12   mycroft linux_sys_truncate(p, v, retval)
    782   1.2      fvdl 	struct proc *p;
    783  1.11   thorpej 	void *v;
    784  1.11   thorpej 	register_t *retval;
    785  1.11   thorpej {
    786  1.12   mycroft 	struct linux_sys_truncate_args /* {
    787   1.2      fvdl 		syscallarg(char *) path;
    788   1.2      fvdl 		syscallarg(long) length;
    789  1.11   thorpej 	} */ *uap = v;
    790   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    791   1.2      fvdl 
    792   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    793  1.12   mycroft 
    794  1.12   mycroft 	return compat_43_sys_truncate(p, uap, retval);
    795   1.1      fvdl }
    796