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