Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.10
      1 /*	$NetBSD: linux_misc_notalpha.c,v 1.10 1995/08/13 17:51:40 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Frank van der Linden
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed for the NetBSD Project
     18  *      by Frank van der Linden
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Linux compatibility module. Try to deal with various Linux system calls.
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/namei.h>
     41 #include <sys/proc.h>
     42 #include <sys/dir.h>
     43 #include <sys/file.h>
     44 #include <sys/stat.h>
     45 #include <sys/filedesc.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/kernel.h>
     48 #include <sys/malloc.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/mman.h>
     51 #include <sys/mount.h>
     52 #include <sys/ptrace.h>
     53 #include <sys/resource.h>
     54 #include <sys/resourcevar.h>
     55 #include <sys/signal.h>
     56 #include <sys/signalvar.h>
     57 #include <sys/socket.h>
     58 #include <sys/time.h>
     59 #include <sys/times.h>
     60 #include <sys/vnode.h>
     61 #include <sys/uio.h>
     62 #include <sys/wait.h>
     63 #include <sys/utsname.h>
     64 #include <sys/unistd.h>
     65 
     66 #include <sys/syscallargs.h>
     67 
     68 #include <vm/vm.h>
     69 #include <vm/vm_param.h>
     70 
     71 #include <compat/linux/linux_types.h>
     72 #include <compat/linux/linux_fcntl.h>
     73 #include <compat/linux/linux_mmap.h>
     74 #include <compat/linux/linux_syscallargs.h>
     75 #include <compat/linux/linux_util.h>
     76 #include <compat/linux/linux_dirent.h>
     77 
     78 /*
     79  * The information on a terminated (or stopped) process needs
     80  * to be converted in order for Linux binaries to get a valid signal
     81  * number out of it.
     82  */
     83 static int
     84 bsd_to_linux_wstat(status)
     85 	int *status;
     86 {
     87 	if (WIFSIGNALED(*status))
     88 		*status = (*status & ~0177) |
     89 		    bsd_to_linux_sig(WTERMSIG(*status));
     90 	else if (WIFSTOPPED(*status))
     91 		*status = (*status & ~0xff00) |
     92 		    (bsd_to_linux_sig(WSTOPSIG(*status)) << 8);
     93 }
     94 
     95 /*
     96  * waitpid(2). Passed on to the NetBSD call, surrounded by code to
     97  * reserve some space for a NetBSD-style wait status, and converting
     98  * it to what Linux wants.
     99  */
    100 int
    101 linux_waitpid(p, uap, retval)
    102 	struct proc *p;
    103 	struct linux_waitpid_args /* {
    104 		syscallarg(int) pid;
    105 		syscallarg(int *) status;
    106 		syscallarg(int) options;
    107 	} */ *uap;
    108 	register_t *retval;
    109 {
    110 	struct wait4_args w4a;
    111 	int error, *status, tstat;
    112 	caddr_t sg;
    113 
    114 	sg = stackgap_init(p->p_emul);
    115 	status = (int *) stackgap_alloc(&sg, sizeof status);
    116 
    117 	SCARG(&w4a, pid) = SCARG(uap, pid);
    118 	SCARG(&w4a, status) = status;
    119 	SCARG(&w4a, options) = SCARG(uap, options);
    120 	SCARG(&w4a, rusage) = NULL;
    121 
    122 	if ((error = wait4(p, &w4a, retval)))
    123 		return error;
    124 
    125 	if ((error = copyin(status, &tstat, sizeof tstat)))
    126 		return error;
    127 
    128 	bsd_to_linux_wstat(&tstat);
    129 
    130 	return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    131 }
    132 
    133 /*
    134  * This is very much the same as waitpid()
    135  */
    136 int
    137 linux_wait4(p, uap, retval)
    138 	struct proc *p;
    139 	struct linux_wait4_args /* {
    140 		syscallarg(int) pid;
    141 		syscallarg(int *) status;
    142 		syscallarg(int) options;
    143 		syscallarg(struct rusage *) rusage;
    144 	} */ *uap;
    145 	register_t *retval;
    146 {
    147 	struct wait4_args w4a;
    148 	int error, *status, tstat;
    149 	caddr_t sg;
    150 
    151 	sg = stackgap_init(p->p_emul);
    152 	status = (int *) stackgap_alloc(&sg, sizeof status);
    153 
    154 	SCARG(&w4a, pid) = SCARG(uap, pid);
    155 	SCARG(&w4a, status) = status;
    156 	SCARG(&w4a, options) = SCARG(uap, options);
    157 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
    158 
    159 	if ((error = wait4(p, &w4a, retval)))
    160 		return error;
    161 
    162 	if ((error = copyin(status, &tstat, sizeof tstat)))
    163 		return error;
    164 
    165 	bsd_to_linux_wstat(&tstat);
    166 
    167 	return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    168 }
    169 
    170 /*
    171  * This is the old brk(2) call. I don't think anything in the Linux
    172  * world uses this anymore
    173  */
    174 int
    175 linux_break(p, uap, retval)
    176 	struct proc *p;
    177 	struct linux_brk_args /* {
    178 		syscallarg(char *) nsize;
    179 	} */ *uap;
    180 	register_t *retval;
    181 {
    182 	return ENOSYS;
    183 }
    184 
    185 /*
    186  * Linux brk(2). The check if the new address is >= the old one is
    187  * done in the kernel in Linux. NetBSD does it in the library.
    188  */
    189 int
    190 linux_brk(p, uap, retval)
    191 	struct proc *p;
    192 	struct linux_brk_args /* {
    193 		syscallarg(char *) nsize;
    194 	} */ *uap;
    195 	register_t *retval;
    196 {
    197 	char *nbrk = SCARG(uap, nsize);
    198 	struct obreak_args oba;
    199 	struct vmspace *vm = p->p_vmspace;
    200 	int error = 0;
    201 	caddr_t oldbrk, newbrk;
    202 
    203 	oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
    204 	/*
    205 	 * XXX inconsistent.. Linux always returns at least the old
    206 	 * brk value, but it will be page-aligned if this fails,
    207 	 * and possibly not page aligned if it succeeds (the user
    208 	 * supplied pointer is returned).
    209 	 */
    210 	SCARG(&oba, nsize) = nbrk;
    211 
    212 	if ((caddr_t) nbrk > vm->vm_daddr && obreak(p, &oba, retval) == 0)
    213 		retval[0] = (register_t) nbrk;
    214 	else
    215 		retval[0] = (register_t) oldbrk;
    216 
    217 	return 0;
    218 }
    219 
    220 /*
    221  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    222  * need to deal with it.
    223  */
    224 int
    225 linux_time(p, uap, retval)
    226 	struct proc *p;
    227 	struct linux_time_args /* {
    228 		linux_time_t *t;
    229 	} */ *uap;
    230 	register_t *retval;
    231 {
    232 	struct timeval atv;
    233 	linux_time_t tt;
    234 	int error;
    235 
    236 	microtime(&atv);
    237 
    238 	tt = atv.tv_sec;
    239 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    240 		return error;
    241 
    242 	retval[0] = tt;
    243 	return 0;
    244 }
    245 
    246 /*
    247  * Convert BSD statfs structure to Linux statfs structure.
    248  * The Linux structure has less fields, and it also wants
    249  * the length of a name in a dir entry in a field, which
    250  * we fake (probably the wrong way).
    251  */
    252 static void
    253 bsd_to_linux_statfs(bsp, lsp)
    254 	struct statfs *bsp;
    255 	struct linux_statfs *lsp;
    256 {
    257 	lsp->l_ftype = bsp->f_type;
    258 	lsp->l_fbsize = bsp->f_bsize;
    259 	lsp->l_fblocks = bsp->f_blocks;
    260 	lsp->l_fbfree = bsp->f_bfree;
    261 	lsp->l_fbavail = bsp->f_bavail;
    262 	lsp->l_ffiles = bsp->f_files;
    263 	lsp->l_fffree = bsp->f_ffree;
    264 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
    265 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
    266 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
    267 }
    268 
    269 /*
    270  * Implement the fs stat functions. Straightforward.
    271  */
    272 int
    273 linux_statfs(p, uap, retval)
    274 	struct proc *p;
    275 	struct linux_statfs_args /* {
    276 		syscallarg(char *) path;
    277 		syscallarg(struct linux_statfs *) sp;
    278 	} */ *uap;
    279 	register_t *retval;
    280 {
    281 	struct statfs btmp, *bsp;
    282 	struct linux_statfs ltmp;
    283 	struct statfs_args bsa;
    284 	caddr_t sg;
    285 	int error;
    286 
    287 	sg = stackgap_init(p->p_emul);
    288 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    289 
    290 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    291 
    292 	SCARG(&bsa, path) = SCARG(uap, path);
    293 	SCARG(&bsa, buf) = bsp;
    294 
    295 	if ((error = statfs(p, &bsa, retval)))
    296 		return error;
    297 
    298 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    299 		return error;
    300 
    301 	bsd_to_linux_statfs(&btmp, &ltmp);
    302 
    303 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    304 }
    305 
    306 int
    307 linux_fstatfs(p, uap, retval)
    308 	struct proc *p;
    309 	struct linux_fstatfs_args /* {
    310 		syscallarg(int) fd;
    311 		syscallarg(struct linux_statfs *) sp;
    312 	} */ *uap;
    313 	register_t *retval;
    314 {
    315 	struct statfs btmp, *bsp;
    316 	struct linux_statfs ltmp;
    317 	struct fstatfs_args bsa;
    318 	caddr_t sg;
    319 	int error;
    320 
    321 	sg = stackgap_init(p->p_emul);
    322 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    323 
    324 	SCARG(&bsa, fd) = SCARG(uap, fd);
    325 	SCARG(&bsa, buf) = bsp;
    326 
    327 	if ((error = statfs(p, &bsa, retval)))
    328 		return error;
    329 
    330 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    331 		return error;
    332 
    333 	bsd_to_linux_statfs(&btmp, &ltmp);
    334 
    335 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    336 }
    337 
    338 /*
    339  * uname(). Just copy the info from the various strings stored in the
    340  * kernel, and put it in the Linux utsname structure. That structure
    341  * is almost the same as the NetBSD one, only it has fields 65 characters
    342  * long, and an extra domainname field.
    343  */
    344 int
    345 linux_uname(p, uap, retval)
    346 	struct proc *p;
    347 	struct linux_uname_args /* {
    348 		syscallarg(struct linux_utsname *) up;
    349 	} */ *uap;
    350 	register_t *retval;
    351 {
    352 	extern char ostype[], osrelease[], version[], hostname[], domainname[];
    353 	extern char machine[];
    354 	struct linux_utsname tluts;
    355 	int len;
    356 	char *cp;
    357 
    358 	strncpy(tluts.l_sysname, ostype, sizeof (tluts.l_sysname));
    359 	strncpy(tluts.l_nodename, hostname, sizeof (tluts.l_nodename));
    360 	strncpy(tluts.l_release, osrelease, sizeof (tluts.l_release));
    361 	strncpy(tluts.l_machine, machine, sizeof (tluts.l_machine));
    362 	strncpy(tluts.l_domainname, domainname, sizeof (tluts.l_domainname));
    363 	strncpy(tluts.l_version, version, sizeof (tluts.l_version));
    364 
    365 	/* This part taken from the the uname() in libc */
    366 	len = sizeof (tluts.l_version);
    367 	for (cp = tluts.l_version; len--; ++cp)
    368 		if (*cp == '\n' || *cp == '\t')
    369 			if (len > 1)
    370 				*cp = ' ';
    371 			else
    372 				*cp = '\0';
    373 
    374 	return copyout(&tluts, SCARG(uap, up), sizeof tluts);
    375 }
    376 
    377 /*
    378  * Linux wants to pass everything to a syscall in registers. However,
    379  * mmap() has 6 of them. Oops: out of register error. They just pass
    380  * everything in a structure.
    381  */
    382 int
    383 linux_mmap(p, uap, retval)
    384 	struct proc *p;
    385 	struct linux_mmap_args /* {
    386 		syscallarg(struct linux_mmap *) lmp;
    387 	} */ *uap;
    388 	register_t *retval;
    389 {
    390 	struct linux_mmap lmap;
    391 	struct mmap_args cma;
    392 	int error, flags;
    393 
    394 	if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap)))
    395 		return error;
    396 
    397 	flags = 0;
    398 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED);
    399 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE);
    400 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED);
    401 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON);
    402 
    403 	SCARG(&cma,addr) = lmap.lm_addr;
    404 	SCARG(&cma,len) = lmap.lm_len;
    405  	SCARG(&cma,prot) = lmap.lm_prot;
    406 	SCARG(&cma,flags) = flags;
    407 	SCARG(&cma,fd) = lmap.lm_fd;
    408 	SCARG(&cma,pad) = 0;
    409 	SCARG(&cma,pos) = lmap.lm_pos;
    410 
    411 	return mmap(p, &cma, retval);
    412 }
    413 
    414 /*
    415  * Linux doesn't use the retval[1] value to determine whether
    416  * we are the child or parent.
    417  */
    418 int
    419 linux_fork(p, uap, retval)
    420 	struct proc *p;
    421 	void *uap;
    422 	register_t *retval;
    423 {
    424 	int error;
    425 
    426 	if ((error = fork(p, uap, retval)))
    427 		return error;
    428 
    429 	if (retval[1] == 1)
    430 		retval[0] = 0;
    431 
    432 	return 0;
    433 }
    434 
    435 /*
    436  * This code is partly stolen from src/lib/libc/compat-43/times.c
    437  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
    438  */
    439 
    440 #define CLK_TCK 100
    441 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
    442 
    443 int
    444 linux_times(p, uap, retval)
    445 	struct proc *p;
    446 	struct linux_times_args /* {
    447 		syscallarg(struct times *) tms;
    448 	} */ *uap;
    449 	register_t *retval;
    450 {
    451 	struct timeval t;
    452 	struct linux_tms ltms;
    453 	struct rusage ru;
    454 	int error, s;
    455 
    456 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
    457 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
    458 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
    459 
    460 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    461 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    462 
    463 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
    464 		return error;
    465 
    466 	s = splclock();
    467 	timersub(&time, &boottime, &t);
    468 	splx(s);
    469 
    470 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
    471 	return 0;
    472 }
    473 
    474 /*
    475  * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
    476  * Linux directly passes the pointer.
    477  */
    478 int
    479 linux_pipe(p, uap, retval)
    480 	struct proc *p;
    481 	struct linux_pipe_args /* {
    482 		syscallarg(int *) pfds;
    483 	} */ *uap;
    484 	register_t *retval;
    485 {
    486 	int error;
    487 
    488 	if ((error = pipe(p, 0, retval)))
    489 		return error;
    490 
    491 	/* Assumes register_t is an int */
    492 
    493 	if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int))))
    494 		return error;
    495 
    496 	retval[0] = 0;
    497 	return 0;
    498 }
    499 
    500 /*
    501  * Alarm. This is a libc call which used setitimer(2) in NetBSD.
    502  * Fiddle with the timers to make it work.
    503  */
    504 int
    505 linux_alarm(p, uap, retval)
    506 	struct proc *p;
    507 	struct linux_alarm_args /* {
    508 		syscallarg(unsigned int) secs;
    509 	} */ *uap;
    510 	register_t *retval;
    511 {
    512 	int error, s;
    513 	struct itimerval *itp, it;
    514 
    515 	itp = &p->p_realtimer;
    516 	s = splclock();
    517 	/*
    518 	 * Clear any pending timer alarms.
    519 	 */
    520 	untimeout(realitexpire, p);
    521 	timerclear(&itp->it_interval);
    522 	if (timerisset(&itp->it_value) &&
    523 	    timercmp(&itp->it_value, &time, >))
    524 		timersub(&itp->it_value, &time, &itp->it_value);
    525 	/*
    526 	 * Return how many seconds were left (rounded up)
    527 	 */
    528 	retval[0] = itp->it_value.tv_sec;
    529 	if (itp->it_value.tv_usec)
    530 		retval[0]++;
    531 
    532 	/*
    533 	 * alarm(0) just resets the timer.
    534 	 */
    535 	if (SCARG(uap, secs) == 0) {
    536 		timerclear(&itp->it_value);
    537 		splx(s);
    538 		return 0;
    539 	}
    540 
    541 	/*
    542 	 * Check the new alarm time for sanity, and set it.
    543 	 */
    544 	timerclear(&it.it_interval);
    545 	it.it_value.tv_sec = SCARG(uap, secs);
    546 	it.it_value.tv_usec = 0;
    547 	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
    548 		splx(s);
    549 		return (EINVAL);
    550 	}
    551 
    552 	if (timerisset(&it.it_value)) {
    553 		timeradd(&it.it_value, &time, &it.it_value);
    554 		timeout(realitexpire, p, hzto(&it.it_value));
    555 	}
    556 	p->p_realtimer = it;
    557 	splx(s);
    558 
    559 	return 0;
    560 }
    561 
    562 /*
    563  * utime(). Do conversion to things that utimes() understands,
    564  * and pass it on.
    565  */
    566 int
    567 linux_utime(p, uap, retval)
    568 	struct proc *p;
    569 	struct linux_utime_args /* {
    570 		syscallarg(char *) path;
    571 		syscallarg(struct linux_utimbuf *)times;
    572 	} */ *uap;
    573 	register_t *retval;
    574 {
    575 	caddr_t sg;
    576 	int error;
    577 	struct utimes_args ua;
    578 	struct timeval tv[2], *tvp;
    579 	struct linux_utimbuf lut;
    580 
    581 	sg = stackgap_init(p->p_emul);
    582 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    583 
    584 	SCARG(&ua, path) = SCARG(uap, path);
    585 
    586 	if (SCARG(uap, times) != NULL) {
    587 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    588 			return error;
    589 		tv[0].tv_usec = tv[1].tv_usec = 0;
    590 		tv[0].tv_sec = lut.l_actime;
    591 		tv[1].tv_sec = lut.l_modtime;
    592 		tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
    593 		if ((error = copyout(tv, tvp, sizeof tv)))
    594 			return error;
    595 		SCARG(&ua, tptr) = tvp;
    596 	}
    597 	else
    598 		SCARG(&ua, tptr) = NULL;
    599 
    600 	return utimes(p, uap, retval);
    601 }
    602 
    603 /*
    604  * Linux 'readdir' call. This code is mostly taken from the
    605  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    606  * an attempt has been made to keep it a little cleaner (failing
    607  * miserably, because of the cruft needed if count 1 is passed).
    608  *
    609  * Read in BSD-style entries, convert them, and copy them out.
    610  * Note that the Linux d_reclen is actually the name length,
    611  * and d_off is the reclen.
    612  *
    613  * Note that this doesn't handle union-mounted filesystems.
    614  */
    615 int
    616 linux_readdir(p, uap, retval)
    617 	struct proc *p;
    618 	struct linux_readdir_args /* {
    619 		syscallarg(int) fd;
    620 		syscallarg(struct linux_dirent *) dent;
    621 		syscallarg(unsigned int) count;
    622 	} */ *uap;
    623 	register_t *retval;
    624 {
    625 	register struct dirent *bdp;
    626 	struct vnode *vp;
    627 	caddr_t	inp, buf;	/* BSD-format */
    628 	int len, reclen;	/* BSD-format */
    629 	caddr_t outp;		/* Linux-format */
    630 	int resid, linuxreclen;	/* Linux-format */
    631 	struct file *fp;
    632 	struct uio auio;
    633 	struct iovec aiov;
    634 	struct linux_dirent idb;
    635 	off_t off;		/* true file offset */
    636 	linux_off_t soff;	/* Linux file offset */
    637 	int buflen, error, eofflag, nbytes, justone;
    638 	struct vattr va;
    639 
    640 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    641 		return (error);
    642 
    643 	if ((fp->f_flag & FREAD) == 0)
    644 		return (EBADF);
    645 
    646 	vp = (struct vnode *)fp->f_data;
    647 
    648 	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
    649 		return (EINVAL);
    650 
    651 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    652 		return error;
    653 
    654 	nbytes = SCARG(uap, count);
    655 	if (nbytes == 1) {	/* Need this for older Linux libs, apparently */
    656 		nbytes = sizeof (struct linux_dirent);
    657 		buflen = max(va.va_blocksize, nbytes);
    658 		justone = 1;
    659 	} else {
    660 		buflen = min(MAXBSIZE, nbytes);
    661 		justone = 0;
    662 	}
    663 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    664 	VOP_LOCK(vp);
    665 	off = fp->f_offset;
    666 again:
    667 	aiov.iov_base = buf;
    668 	aiov.iov_len = buflen;
    669 	auio.uio_iov = &aiov;
    670 	auio.uio_iovcnt = 1;
    671 	auio.uio_rw = UIO_READ;
    672 	auio.uio_segflg = UIO_SYSSPACE;
    673 	auio.uio_procp = p;
    674 	auio.uio_resid = buflen;
    675 	auio.uio_offset = off;
    676 	/*
    677          * First we read into the malloc'ed buffer, then
    678          * we massage it into user space, one record at a time.
    679          */
    680 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
    681 	if (error)
    682 		goto out;
    683 
    684 	inp = buf;
    685 	outp = (caddr_t) SCARG(uap, dent);
    686 	resid = nbytes;
    687 	if ((len = buflen - auio.uio_resid) == 0)
    688 		goto eof;
    689 
    690 	for (; len > 0; len -= reclen) {
    691 		bdp = (struct dirent *)inp;
    692 		reclen = bdp->d_reclen;
    693 		if (reclen & 3)
    694 			panic("linux_readdir");
    695 		off += reclen;	/* each entry points to next */
    696 		if (bdp->d_fileno == 0) {
    697 			inp += reclen;	/* it is a hole; squish it out */
    698 			continue;
    699 		}
    700 		linuxreclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    701 		if (reclen > len || resid < linuxreclen) {
    702 			/* entry too big for buffer, so just stop */
    703 			outp++;
    704 			break;
    705 		}
    706 		/*
    707 		 * Massage in place to make a Linux-shaped dirent (otherwise
    708 		 * we have to worry about touching user memory outside of
    709 		 * the copyout() call).
    710 		 */
    711 		idb.d_ino = (long)bdp->d_fileno;
    712 		idb.d_off = (linux_off_t)linuxreclen;
    713 		idb.d_reclen = (u_short)bdp->d_namlen;
    714 		strcpy(idb.d_name, bdp->d_name);
    715 		if ((error = copyout((caddr_t)&idb, outp, linuxreclen)))
    716 			goto out;
    717 		/* advance past this real entry */
    718 		inp += reclen;
    719 		/* advance output past Linux-shaped entry */
    720 		outp += linuxreclen;
    721 		resid -= linuxreclen;
    722 		if (justone)
    723 			break;
    724 	}
    725 
    726 	/* if we squished out the whole block, try again */
    727 	if (outp == (caddr_t) SCARG(uap, dent))
    728 		goto again;
    729 	fp->f_offset = off;	/* update the vnode offset */
    730 
    731 	if (justone)
    732 		nbytes = resid + linuxreclen;
    733 
    734 eof:
    735 	*retval = nbytes - resid;
    736 out:
    737 	VOP_UNLOCK(vp);
    738 	free(buf, M_TEMP);
    739 	return error;
    740 }
    741 
    742 /*
    743  * Out of register error once more.. Also, Linux copies the amount of
    744  * time left into the user-supplied timeval structure.
    745  */
    746 int
    747 linux_select(p, uap, retval)
    748 	struct proc *p;
    749 	struct linux_select_args /* {
    750 		syscallarg(struct linux_select *) lsp;
    751 	} */ *uap;
    752 	register_t *retval;
    753 {
    754 	struct linux_select ls;
    755 	struct select_args bsa;
    756 	struct timeval tv0, tv1, utv;
    757 	int error;
    758 
    759 	if ((error = copyin(SCARG(uap, lsp), (caddr_t)&ls, sizeof ls)))
    760 		return error;
    761 
    762 	SCARG(&bsa, nd) = ls.nfds;
    763 	SCARG(&bsa, in) = ls.readfds;
    764 	SCARG(&bsa, ou) = ls.writefds;
    765 	SCARG(&bsa, ex) = ls.exceptfds;
    766 	SCARG(&bsa, tv) = ls.timeout;
    767 
    768 	/*
    769 	 * Store current time for computation of the amount of
    770 	 * time left.
    771 	 */
    772 	if (ls.timeout)
    773 		microtime(&tv0);
    774 
    775 	error = select(p, &bsa, retval);
    776 	if (error) {
    777 		/*
    778 		 * See fs/select.c in the Linux kernel.  Without this,
    779 		 * Maelstrom doesn't work.
    780 		 */
    781 		if (error == ERESTART)
    782 			error = EINTR;
    783 		return error;
    784 	}
    785 
    786 	if (ls.timeout) {
    787 		if (!*retval) {
    788 			utv.tv_sec = 0;
    789 			utv.tv_usec = 0;
    790 		} else {
    791 			/*
    792 			 * Compute how many time was left of the timeout,
    793 			 * by subtracting the current time and the time
    794 			 * before we started the call, and subtracting
    795 			 * that result from the user-supplied value.
    796 			 */
    797 			microtime(&tv1);
    798 			if ((error = copyin((caddr_t)ls.timeout, (caddr_t)&utv,
    799 			    sizeof utv)))
    800 				return error;
    801 			timersub(&tv1, &tv0, &tv1);
    802 			timersub(&utv, &tv1, &utv);
    803 		}
    804 		if ((error = copyout((caddr_t)&utv, (caddr_t)ls.timeout,
    805 		    sizeof utv)))
    806 			return error;
    807 	}
    808 	return 0;
    809 }
    810 
    811 /*
    812  * Get the process group of a certain process. Look it up
    813  * and return the value.
    814  */
    815 int
    816 linux_getpgid(p, uap, retval)
    817 	struct proc *p;
    818 	struct linux_getpgid_args /* {
    819 		syscallarg(int) pid;
    820 	} */ *uap;
    821 	register_t *retval;
    822 {
    823 	struct proc *targp;
    824 
    825 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid)
    826 		if ((targp = pfind(SCARG(uap, pid))) == 0)
    827 			return ESRCH;
    828 	else
    829 		targp = p;
    830 
    831 	retval[0] = targp->p_pgid;
    832 	return 0;
    833 }
    834 
    835 /*
    836  * Set the 'personality' (emulation mode) for the current process. Only
    837  * accept the Linux personality here (0). This call is needed because
    838  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
    839  * ELF binaries run in Linux mode, not SVR4 mode.
    840  */
    841 int
    842 linux_personality(p, uap, retval)
    843 	struct proc *p;
    844 	struct linux_personality_args /* P
    845 		syscallarg(int) per;
    846 	} */ *uap;
    847 	register_t *retval;
    848 {
    849 	if (SCARG(uap, per) != 0)
    850 		return EINVAL;
    851 	retval[0] = 0;
    852 	return 0;
    853 }
    854