linux_pipe.c revision 1.1
11.1Sfvdl/*	$NetBSD: linux_pipe.c,v 1.1 1995/02/28 23:25:07 fvdl Exp $	*/
21.1Sfvdl
31.1Sfvdl/*
41.1Sfvdl * Copyright (c) 1995 Frank van der Linden
51.1Sfvdl * All rights reserved.
61.1Sfvdl *
71.1Sfvdl * Redistribution and use in source and binary forms, with or without
81.1Sfvdl * modification, are permitted provided that the following conditions
91.1Sfvdl * are met:
101.1Sfvdl * 1. Redistributions of source code must retain the above copyright
111.1Sfvdl *    notice, this list of conditions and the following disclaimer.
121.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
131.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
141.1Sfvdl *    documentation and/or other materials provided with the distribution.
151.1Sfvdl * 3. All advertising materials mentioning features or use of this software
161.1Sfvdl *    must display the following acknowledgement:
171.1Sfvdl *      This product includes software developed for the NetBSD Project
181.1Sfvdl *      by Frank van der Linden
191.1Sfvdl * 4. The name of the author may not be used to endorse or promote products
201.1Sfvdl *    derived from this software without specific prior written permission
211.1Sfvdl *
221.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
231.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
241.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
251.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
261.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
271.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
281.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
291.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
301.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
311.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321.1Sfvdl */
331.1Sfvdl
341.1Sfvdl/*
351.1Sfvdl * Linux compatibility module. Try to deal with various Linux system calls.
361.1Sfvdl */
371.1Sfvdl
381.1Sfvdl#include <sys/param.h>
391.1Sfvdl#include <sys/systm.h>
401.1Sfvdl#include <sys/namei.h>
411.1Sfvdl#include <sys/proc.h>
421.1Sfvdl#include <sys/dir.h>
431.1Sfvdl#include <sys/file.h>
441.1Sfvdl#include <sys/stat.h>
451.1Sfvdl#include <sys/filedesc.h>
461.1Sfvdl#include <sys/ioctl.h>
471.1Sfvdl#include <sys/kernel.h>
481.1Sfvdl#include <sys/malloc.h>
491.1Sfvdl#include <sys/mbuf.h>
501.1Sfvdl#include <sys/mman.h>
511.1Sfvdl#include <sys/mount.h>
521.1Sfvdl#include <sys/ptrace.h>
531.1Sfvdl#include <sys/resource.h>
541.1Sfvdl#include <sys/resourcevar.h>
551.1Sfvdl#include <sys/signal.h>
561.1Sfvdl#include <sys/signalvar.h>
571.1Sfvdl#include <sys/socket.h>
581.1Sfvdl#include <sys/time.h>
591.1Sfvdl#include <sys/times.h>
601.1Sfvdl#include <sys/vnode.h>
611.1Sfvdl#include <sys/uio.h>
621.1Sfvdl#include <sys/wait.h>
631.1Sfvdl#include <sys/utsname.h>
641.1Sfvdl#include <sys/unistd.h>
651.1Sfvdl
661.1Sfvdl#include <sys/syscallargs.h>
671.1Sfvdl
681.1Sfvdl#include <vm/vm.h>
691.1Sfvdl#include <vm/vm_param.h>
701.1Sfvdl
711.1Sfvdl#include <compat/linux/linux_types.h>
721.1Sfvdl#include <compat/linux/linux_fcntl.h>
731.1Sfvdl#include <compat/linux/linux_mmap.h>
741.1Sfvdl#include <compat/linux/linux_syscallargs.h>
751.1Sfvdl#include <compat/linux/linux_util.h>
761.1Sfvdl#include <compat/linux/linux_dirent.h>
771.1Sfvdl
781.1Sfvdl/*
791.1Sfvdl * The information on a terminated (or stopped) process needs
801.1Sfvdl * to be converted in order for Linux binaries to get a valid signal
811.1Sfvdl * number out of it.
821.1Sfvdl */
831.1Sfvdlstatic int
841.1Sfvdlbsd_to_linux_wstat(status)
851.1Sfvdl	int *status;
861.1Sfvdl{
871.1Sfvdl	if (WIFSIGNALED(*status))
881.1Sfvdl		*status = (*status & ~0177) |
891.1Sfvdl		    bsd_to_linux_sig(WTERMSIG(*status));
901.1Sfvdl	else if (WIFSTOPPED(*status))
911.1Sfvdl		*status = (*status & ~0xff00) |
921.1Sfvdl		    (bsd_to_linux_sig(WSTOPSIG(*status)) << 8);
931.1Sfvdl}
941.1Sfvdl
951.1Sfvdl/*
961.1Sfvdl * waitpid(2). Passed on to the NetBSD call, surrounded by code to
971.1Sfvdl * reserve some space for a NetBSD-style wait status, and converting
981.1Sfvdl * it to what Linux wants.
991.1Sfvdl */
1001.1Sfvdlint
1011.1Sfvdllinux_waitpid(p, uap, retval)
1021.1Sfvdl	struct proc *p;
1031.1Sfvdl	struct linux_waitpid_args /* {
1041.1Sfvdl		syscallarg(int) pid;
1051.1Sfvdl		syscallarg(int *) status;
1061.1Sfvdl		syscallarg(int) options;
1071.1Sfvdl	} */ *uap;
1081.1Sfvdl	register_t *retval;
1091.1Sfvdl{
1101.1Sfvdl	struct wait4_args w4a;
1111.1Sfvdl	int error, *status, tstat;
1121.1Sfvdl	caddr_t sg;
1131.1Sfvdl
1141.1Sfvdl	sg = stackgap_init();
1151.1Sfvdl	status = (int *) stackgap_alloc(&sg, sizeof status);
1161.1Sfvdl
1171.1Sfvdl	SCARG(&w4a, pid) = SCARG(uap, pid);
1181.1Sfvdl	SCARG(&w4a, status) = status;
1191.1Sfvdl	SCARG(&w4a, options) = SCARG(uap, options);
1201.1Sfvdl	SCARG(&w4a, rusage) = NULL;
1211.1Sfvdl
1221.1Sfvdl	if ((error = wait4(p, &w4a, retval)))
1231.1Sfvdl		return error;
1241.1Sfvdl
1251.1Sfvdl	if ((error = copyin(status, &tstat, sizeof tstat)))
1261.1Sfvdl		return error;
1271.1Sfvdl
1281.1Sfvdl	bsd_to_linux_wstat(&tstat);
1291.1Sfvdl
1301.1Sfvdl	return copyout(&tstat, SCARG(uap, status), sizeof tstat);
1311.1Sfvdl}
1321.1Sfvdl
1331.1Sfvdl/*
1341.1Sfvdl * This is very much the same as waitpid()
1351.1Sfvdl */
1361.1Sfvdlint
1371.1Sfvdllinux_wait4(p, uap, retval)
1381.1Sfvdl	struct proc *p;
1391.1Sfvdl	struct linux_wait4_args /* {
1401.1Sfvdl		syscallarg(int) pid;
1411.1Sfvdl		syscallarg(int *) status;
1421.1Sfvdl		syscallarg(int) options;
1431.1Sfvdl		syscallarg(struct rusage *) rusage;
1441.1Sfvdl	} */ *uap;
1451.1Sfvdl	register_t *retval;
1461.1Sfvdl{
1471.1Sfvdl	struct wait4_args w4a;
1481.1Sfvdl	int error, *status, tstat;
1491.1Sfvdl	caddr_t sg;
1501.1Sfvdl
1511.1Sfvdl	sg = stackgap_init();
1521.1Sfvdl	status = (int *) stackgap_alloc(&sg, sizeof status);
1531.1Sfvdl
1541.1Sfvdl	SCARG(&w4a, pid) = SCARG(uap, pid);
1551.1Sfvdl	SCARG(&w4a, status) = status;
1561.1Sfvdl	SCARG(&w4a, options) = SCARG(uap, options);
1571.1Sfvdl	SCARG(&w4a, rusage) = SCARG(uap, rusage);
1581.1Sfvdl
1591.1Sfvdl	if ((error = wait4(p, &w4a, retval)))
1601.1Sfvdl		return error;
1611.1Sfvdl
1621.1Sfvdl	if ((error = copyin(status, &tstat, sizeof tstat)))
1631.1Sfvdl		return error;
1641.1Sfvdl
1651.1Sfvdl	bsd_to_linux_wstat(&tstat);
1661.1Sfvdl
1671.1Sfvdl	return copyout(&tstat, SCARG(uap, status), sizeof tstat);
1681.1Sfvdl}
1691.1Sfvdl
1701.1Sfvdl/*
1711.1Sfvdl * This is the old brk(2) call. I don't think anything in the Linux
1721.1Sfvdl * world uses this anymore
1731.1Sfvdl */
1741.1Sfvdlint
1751.1Sfvdllinux_break(p, uap, retval)
1761.1Sfvdl	struct proc *p;
1771.1Sfvdl	struct linux_brk_args /* {
1781.1Sfvdl		syscallarg(char *) nsize;
1791.1Sfvdl	} */ *uap;
1801.1Sfvdl	register_t *retval;
1811.1Sfvdl{
1821.1Sfvdl	return ENOSYS;
1831.1Sfvdl}
1841.1Sfvdl
1851.1Sfvdl/*
1861.1Sfvdl * Linux brk(2). The check if the new address is >= the old one is
1871.1Sfvdl * done in the kernel in Linux. NetBSD does it in the library.
1881.1Sfvdl */
1891.1Sfvdlint
1901.1Sfvdllinux_brk(p, uap, retval)
1911.1Sfvdl	struct proc *p;
1921.1Sfvdl	struct linux_brk_args /* {
1931.1Sfvdl		syscallarg(char *) nsize;
1941.1Sfvdl	} */ *uap;
1951.1Sfvdl	register_t *retval;
1961.1Sfvdl{
1971.1Sfvdl	char *nbrk = SCARG(uap, nsize);
1981.1Sfvdl	struct obreak_args oba;
1991.1Sfvdl	struct vmspace *vm = p->p_vmspace;
2001.1Sfvdl	int error = 0;
2011.1Sfvdl	caddr_t oldbrk, newbrk;
2021.1Sfvdl
2031.1Sfvdl	oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
2041.1Sfvdl	/*
2051.1Sfvdl	 * XXX inconsistent.. Linux always returns at least the old
2061.1Sfvdl	 * brk value, but it will be page-aligned if this fails,
2071.1Sfvdl	 * and possibly not page aligned if it succeeds (the user
2081.1Sfvdl	 * supplied pointer is returned).
2091.1Sfvdl	 */
2101.1Sfvdl	SCARG(&oba, nsize) = nbrk;
2111.1Sfvdl
2121.1Sfvdl	if ((caddr_t) nbrk > vm->vm_daddr && obreak(p, &oba, retval) == 0)
2131.1Sfvdl		retval[0] = (register_t) nbrk;
2141.1Sfvdl	else
2151.1Sfvdl		retval[0] = (register_t) oldbrk;
2161.1Sfvdl
2171.1Sfvdl	return 0;
2181.1Sfvdl}
2191.1Sfvdl
2201.1Sfvdl/*
2211.1Sfvdl * I wonder why Linux has gettimeofday() _and_ time().. Still, we
2221.1Sfvdl * need to deal with it.
2231.1Sfvdl */
2241.1Sfvdlint
2251.1Sfvdllinux_time(p, uap, retval)
2261.1Sfvdl	struct proc *p;
2271.1Sfvdl	struct linux_time_args /* {
2281.1Sfvdl		linux_time_t *t;
2291.1Sfvdl	} */ *uap;
2301.1Sfvdl	register_t *retval;
2311.1Sfvdl{
2321.1Sfvdl	struct timeval atv;
2331.1Sfvdl	linux_time_t tt;
2341.1Sfvdl	int error;
2351.1Sfvdl
2361.1Sfvdl	microtime(&atv);
2371.1Sfvdl
2381.1Sfvdl	tt = atv.tv_sec;
2391.1Sfvdl	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
2401.1Sfvdl		return error;
2411.1Sfvdl
2421.1Sfvdl	retval[0] = tt;
2431.1Sfvdl	return 0;
2441.1Sfvdl}
2451.1Sfvdl
2461.1Sfvdl/*
2471.1Sfvdl * The statfs and fstatfs called are not implemented yet. They're
2481.1Sfvdl * easy, but just not important for the binaries I wanted to get
2491.1Sfvdl * running.
2501.1Sfvdl */
2511.1Sfvdlint
2521.1Sfvdllinux_statfs(p, uap, retval)
2531.1Sfvdl	struct proc *p;
2541.1Sfvdl	struct linux_statfs_args /* {
2551.1Sfvdl		syscallarg(char *) path;
2561.1Sfvdl		syscallarg(struct linux_statfs *) sp;
2571.1Sfvdl	} */ *uap;
2581.1Sfvdl	register_t *retval;
2591.1Sfvdl{
2601.1Sfvdl	return ENOSYS;
2611.1Sfvdl}
2621.1Sfvdl
2631.1Sfvdlint
2641.1Sfvdllinux_fstatfs(p, uap, retval)
2651.1Sfvdl	struct proc *p;
2661.1Sfvdl	struct linux_fstatfs_args /* {
2671.1Sfvdl		syscallarg(char *) path;
2681.1Sfvdl		syscallarg(struct linux_statfs *) sp;
2691.1Sfvdl	} */ *uap;
2701.1Sfvdl	register_t *retval;
2711.1Sfvdl{
2721.1Sfvdl	return ENOSYS;
2731.1Sfvdl}
2741.1Sfvdl
2751.1Sfvdl/*
2761.1Sfvdl * uname(). Just copy the info from the various strings stored in the
2771.1Sfvdl * kernel, and put it in the Linux utsname structure. That structure
2781.1Sfvdl * is almost the same as the NetBSD one, only it has fields 65 characters
2791.1Sfvdl * long, and an extra domainname field.
2801.1Sfvdl */
2811.1Sfvdlint
2821.1Sfvdllinux_uname(p, uap, retval)
2831.1Sfvdl	struct proc *p;
2841.1Sfvdl	struct linux_uname_args /* {
2851.1Sfvdl		syscallarg(struct linux_utsname *) up;
2861.1Sfvdl	} */ *uap;
2871.1Sfvdl	register_t *retval;
2881.1Sfvdl{
2891.1Sfvdl	extern char ostype[], osrelease[], version[], hostname[], domainname[];
2901.1Sfvdl	extern char machine[];
2911.1Sfvdl	struct linux_utsname tluts;
2921.1Sfvdl	int len;
2931.1Sfvdl	char *cp;
2941.1Sfvdl
2951.1Sfvdl	strncpy(tluts.l_sysname, ostype, sizeof (tluts.l_sysname));
2961.1Sfvdl	strncpy(tluts.l_nodename, hostname, sizeof (tluts.l_nodename));
2971.1Sfvdl	strncpy(tluts.l_release, osrelease, sizeof (tluts.l_release));
2981.1Sfvdl	strncpy(tluts.l_machine, machine, sizeof (tluts.l_machine));
2991.1Sfvdl	strncpy(tluts.l_domainname, domainname, sizeof (tluts.l_domainname));
3001.1Sfvdl	strncpy(tluts.l_version, version, sizeof (tluts.l_version));
3011.1Sfvdl
3021.1Sfvdl	/* This part taken from the the uname() in libc */
3031.1Sfvdl	len = sizeof (tluts.l_version);
3041.1Sfvdl	for (cp = tluts.l_version; len--; ++cp)
3051.1Sfvdl		if (*cp == '\n' || *cp == '\t')
3061.1Sfvdl			if (len > 1)
3071.1Sfvdl				*cp = ' ';
3081.1Sfvdl			else
3091.1Sfvdl				*cp = '\0';
3101.1Sfvdl
3111.1Sfvdl	return copyout(&tluts, SCARG(uap, up), sizeof tluts);
3121.1Sfvdl}
3131.1Sfvdl
3141.1Sfvdl/*
3151.1Sfvdl * Linux wants to pass everything to a syscall in registers. However,
3161.1Sfvdl * mmap() has 6 of them. Oops: out of register error. They just pass
3171.1Sfvdl * everything in a structure.
3181.1Sfvdl */
3191.1Sfvdlint
3201.1Sfvdllinux_mmap(p, uap, retval)
3211.1Sfvdl	struct proc *p;
3221.1Sfvdl	struct linux_mmap_args /* {
3231.1Sfvdl		syscallarg(struct linux_mmap *) lmp;
3241.1Sfvdl	} */ *uap;
3251.1Sfvdl	register_t *retval;
3261.1Sfvdl{
3271.1Sfvdl	struct linux_mmap lmap;
3281.1Sfvdl	struct mmap_args cma;
3291.1Sfvdl	int error, flags;
3301.1Sfvdl
3311.1Sfvdl	if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap)))
3321.1Sfvdl		return error;
3331.1Sfvdl
3341.1Sfvdl	flags = 0;
3351.1Sfvdl	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED);
3361.1Sfvdl	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE);
3371.1Sfvdl	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED);
3381.1Sfvdl	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON);
3391.1Sfvdl
3401.1Sfvdl	SCARG(&cma,addr) = lmap.lm_addr;
3411.1Sfvdl	SCARG(&cma,len) = lmap.lm_len;
3421.1Sfvdl 	SCARG(&cma,prot) = lmap.lm_prot;
3431.1Sfvdl	SCARG(&cma,flags) = flags;
3441.1Sfvdl	SCARG(&cma,fd) = lmap.lm_fd;
3451.1Sfvdl	SCARG(&cma,pad) = 0;
3461.1Sfvdl	SCARG(&cma,pos) = lmap.lm_pos;
3471.1Sfvdl
3481.1Sfvdl	return mmap(p, &cma, retval);
3491.1Sfvdl}
3501.1Sfvdl
3511.1Sfvdl/*
3521.1Sfvdl * Linux doesn't use the retval[1] value to determine whether
3531.1Sfvdl * we are the child or parent.
3541.1Sfvdl */
3551.1Sfvdlint
3561.1Sfvdllinux_fork(p, uap, retval)
3571.1Sfvdl	struct proc *p;
3581.1Sfvdl	void *uap;
3591.1Sfvdl	register_t *retval;
3601.1Sfvdl{
3611.1Sfvdl	int error;
3621.1Sfvdl
3631.1Sfvdl	if ((error = fork(p, uap, retval)))
3641.1Sfvdl		return error;
3651.1Sfvdl
3661.1Sfvdl	if (retval[1] == 1)
3671.1Sfvdl		retval[0] = 0;
3681.1Sfvdl
3691.1Sfvdl	return 0;
3701.1Sfvdl}
3711.1Sfvdl
3721.1Sfvdl/*
3731.1Sfvdl * This code is partly stolen from src/lib/libc/compat-43/times.c
3741.1Sfvdl * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
3751.1Sfvdl */
3761.1Sfvdl
3771.1Sfvdl#define CLK_TCK 100
3781.1Sfvdl#define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
3791.1Sfvdl
3801.1Sfvdlint
3811.1Sfvdllinux_times(p, uap, retval)
3821.1Sfvdl	struct proc *p;
3831.1Sfvdl	struct linux_times_args /* {
3841.1Sfvdl		syscallarg(struct times *) tms;
3851.1Sfvdl	} */ *uap;
3861.1Sfvdl	register_t *retval;
3871.1Sfvdl{
3881.1Sfvdl	struct timeval t;
3891.1Sfvdl	struct linux_tms ltms;
3901.1Sfvdl	struct rusage ru;
3911.1Sfvdl	int error;
3921.1Sfvdl
3931.1Sfvdl	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
3941.1Sfvdl	ltms.ltms_utime = CONVTCK(ru.ru_utime);
3951.1Sfvdl	ltms.ltms_stime = CONVTCK(ru.ru_stime);
3961.1Sfvdl
3971.1Sfvdl	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
3981.1Sfvdl	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
3991.1Sfvdl
4001.1Sfvdl	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
4011.1Sfvdl		return error;
4021.1Sfvdl
4031.1Sfvdl	microtime(&t);
4041.1Sfvdl
4051.1Sfvdl	retval[0] = ((linux_clock_t)(CONVTCK(t)));
4061.1Sfvdl	return 0;
4071.1Sfvdl}
4081.1Sfvdl
4091.1Sfvdl/*
4101.1Sfvdl * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
4111.1Sfvdl * Linux directly passes the pointer.
4121.1Sfvdl */
4131.1Sfvdlint
4141.1Sfvdllinux_pipe(p, uap, retval)
4151.1Sfvdl	struct proc *p;
4161.1Sfvdl	struct linux_pipe_args /* {
4171.1Sfvdl		syscallarg(int *) pfds;
4181.1Sfvdl	} */ *uap;
4191.1Sfvdl	register_t *retval;
4201.1Sfvdl{
4211.1Sfvdl	int error;
4221.1Sfvdl
4231.1Sfvdl	if ((error = pipe(p, 0, retval)))
4241.1Sfvdl		return error;
4251.1Sfvdl
4261.1Sfvdl	/* Assumes register_t is an int */
4271.1Sfvdl
4281.1Sfvdl	if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int))))
4291.1Sfvdl		return error;
4301.1Sfvdl
4311.1Sfvdl	retval[0] = 0;
4321.1Sfvdl	return 0;
4331.1Sfvdl}
4341.1Sfvdl
4351.1Sfvdl/*
4361.1Sfvdl * Alarm. This is a libc call which used setitimer(2) in NetBSD.
4371.1Sfvdl * Fiddle with the timers to make it work.
4381.1Sfvdl */
4391.1Sfvdlint
4401.1Sfvdllinux_alarm(p, uap, retval)
4411.1Sfvdl	struct proc *p;
4421.1Sfvdl	struct linux_alarm_args /* {
4431.1Sfvdl		syscallarg(unsigned int) secs;
4441.1Sfvdl	} */ *uap;
4451.1Sfvdl	register_t *retval;
4461.1Sfvdl{
4471.1Sfvdl	int error, s;
4481.1Sfvdl	struct itimerval *itp, it;
4491.1Sfvdl
4501.1Sfvdl	itp = &p->p_realtimer;
4511.1Sfvdl	s = splclock();
4521.1Sfvdl	/*
4531.1Sfvdl	 * Clear any pending timer alarms.
4541.1Sfvdl	 */
4551.1Sfvdl	untimeout(realitexpire, p);
4561.1Sfvdl	timerclear(&itp->it_interval);
4571.1Sfvdl	if (timerisset(&itp->it_value) &&
4581.1Sfvdl	    timercmp(&itp->it_value, &time, >))
4591.1Sfvdl		__timersub(&itp->it_value, &time);
4601.1Sfvdl	/*
4611.1Sfvdl	 * Return how many seconds were left (rounded up)
4621.1Sfvdl	 */
4631.1Sfvdl	retval[0] = itp->it_value.tv_sec;
4641.1Sfvdl	if (itp->it_value.tv_usec)
4651.1Sfvdl		retval[0]++;
4661.1Sfvdl
4671.1Sfvdl	/*
4681.1Sfvdl	 * alarm(0) just resets the timer.
4691.1Sfvdl	 */
4701.1Sfvdl	if (SCARG(uap, secs) == 0) {
4711.1Sfvdl		timerclear(&itp->it_value);
4721.1Sfvdl		splx(s);
4731.1Sfvdl		return 0;
4741.1Sfvdl	}
4751.1Sfvdl
4761.1Sfvdl	/*
4771.1Sfvdl	 * Check the new alarm time for sanity, and set it.
4781.1Sfvdl	 */
4791.1Sfvdl	timerclear(&it.it_interval);
4801.1Sfvdl	it.it_value.tv_sec = SCARG(uap, secs);
4811.1Sfvdl	it.it_value.tv_usec = 0;
4821.1Sfvdl	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
4831.1Sfvdl		splx(s);
4841.1Sfvdl		return (EINVAL);
4851.1Sfvdl	}
4861.1Sfvdl
4871.1Sfvdl	if (timerisset(&it.it_value)) {
4881.1Sfvdl		__timeradd(&it.it_value, &time);
4891.1Sfvdl		timeout(realitexpire, p, hzto(&it.it_value));
4901.1Sfvdl	}
4911.1Sfvdl	p->p_realtimer = it;
4921.1Sfvdl	splx(s);
4931.1Sfvdl
4941.1Sfvdl	return 0;
4951.1Sfvdl}
4961.1Sfvdl
4971.1Sfvdl/*
4981.1Sfvdl * utime(). Do conversion to things that utimes() understands,
4991.1Sfvdl * and pass it on.
5001.1Sfvdl */
5011.1Sfvdlint
5021.1Sfvdllinux_utime(p, uap, retval)
5031.1Sfvdl	struct proc *p;
5041.1Sfvdl	struct linux_utime_args /* {
5051.1Sfvdl		syscallarg(char *) path;
5061.1Sfvdl		syscallarg(struct linux_utimbuf *)times;
5071.1Sfvdl	} */ *uap;
5081.1Sfvdl	register_t *retval;
5091.1Sfvdl{
5101.1Sfvdl	caddr_t sg;
5111.1Sfvdl	int error;
5121.1Sfvdl	struct utimes_args ua;
5131.1Sfvdl	struct timeval tv[2], *tvp;
5141.1Sfvdl	struct linux_utimbuf lut;
5151.1Sfvdl
5161.1Sfvdl	sg = stackgap_init();
5171.1Sfvdl	CHECK_ALT(p, &sg, SCARG(uap, path));
5181.1Sfvdl
5191.1Sfvdl	SCARG(&ua, path) = SCARG(uap, path);
5201.1Sfvdl
5211.1Sfvdl	if (SCARG(uap, times) != NULL) {
5221.1Sfvdl		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
5231.1Sfvdl			return error;
5241.1Sfvdl		tv[0].tv_usec = tv[1].tv_usec = 0;
5251.1Sfvdl		tv[0].tv_sec = lut.l_actime;
5261.1Sfvdl		tv[1].tv_sec = lut.l_modtime;
5271.1Sfvdl		tvp = (struct timeval *) stackgap_alloc(sizeof tv);
5281.1Sfvdl		if ((error = copyout(tv, tvp, sizeof tv)))
5291.1Sfvdl			return error;
5301.1Sfvdl		SCARG(&ua, tptr) = tvp;
5311.1Sfvdl	}
5321.1Sfvdl	else
5331.1Sfvdl		SCARG(&ua, tptr) = NULL;
5341.1Sfvdl
5351.1Sfvdl	return utimes(p, uap, retval);
5361.1Sfvdl}
5371.1Sfvdl
5381.1Sfvdl/*
5391.1Sfvdl * Linux 'readdir' call. This code is mostly taken from the
5401.1Sfvdl * SunOS getdents call (see compat/sunos/sunos_misc.c), though
5411.1Sfvdl * an attempt has been made to keep it a little cleaner (failing
5421.1Sfvdl * miserably, because of the cruft needed if count 1 is passed).
5431.1Sfvdl *
5441.1Sfvdl * Read in BSD-style entries, convert them, and copy them out.
5451.1Sfvdl * Note that the Linux d_reclen is actually the name length,
5461.1Sfvdl * and d_off is the reclen.
5471.1Sfvdl *
5481.1Sfvdl * Note that this doesn't handle union-mounted filesystems.
5491.1Sfvdl */
5501.1Sfvdlint
5511.1Sfvdllinux_readdir(p, uap, retval)
5521.1Sfvdl	struct proc *p;
5531.1Sfvdl	struct linux_readdir_args /* {
5541.1Sfvdl		syscallarg(int) fd;
5551.1Sfvdl		syscallarg(struct linux_dirent *) dent;
5561.1Sfvdl		syscallarg(unsigned int) count;
5571.1Sfvdl	} */ *uap;
5581.1Sfvdl	register_t *retval;
5591.1Sfvdl{
5601.1Sfvdl	register struct dirent *bdp;
5611.1Sfvdl	struct vnode *vp;
5621.1Sfvdl	caddr_t	inp, buf;	/* BSD-format */
5631.1Sfvdl	int len, reclen;	/* BSD-format */
5641.1Sfvdl	caddr_t outp;		/* Linux-format */
5651.1Sfvdl	int resid, linuxreclen;	/* Linux-format */
5661.1Sfvdl	struct file *fp;
5671.1Sfvdl	struct uio auio;
5681.1Sfvdl	struct iovec aiov;
5691.1Sfvdl	struct linux_dirent idb;
5701.1Sfvdl	off_t off;		/* true file offset */
5711.1Sfvdl	linux_off_t soff;	/* Linux file offset */
5721.1Sfvdl	int buflen, error, eofflag, nbytes, justone;
5731.1Sfvdl	struct vattr va;
5741.1Sfvdl
5751.1Sfvdl	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
5761.1Sfvdl		return (error);
5771.1Sfvdl
5781.1Sfvdl	if ((fp->f_flag & FREAD) == 0)
5791.1Sfvdl		return (EBADF);
5801.1Sfvdl
5811.1Sfvdl	vp = (struct vnode *) fp->f_data;
5821.1Sfvdl
5831.1Sfvdl	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
5841.1Sfvdl		return (EINVAL);
5851.1Sfvdl
5861.1Sfvdl	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
5871.1Sfvdl		return error;
5881.1Sfvdl
5891.1Sfvdl	nbytes = SCARG(uap, count);
5901.1Sfvdl	if (nbytes == 1) {	/* Need this for older Linux libs, apparently */
5911.1Sfvdl		nbytes = sizeof (struct linux_dirent);
5921.1Sfvdl		justone = 1;
5931.1Sfvdl	}
5941.1Sfvdl	else
5951.1Sfvdl		justone = 0;
5961.1Sfvdl
5971.1Sfvdl	buflen = max(va.va_blocksize, nbytes);
5981.1Sfvdl	buf = malloc(buflen, M_TEMP, M_WAITOK);
5991.1Sfvdl	VOP_LOCK(vp);
6001.1Sfvdl	off = fp->f_offset;
6011.1Sfvdlagain:
6021.1Sfvdl	aiov.iov_base = buf;
6031.1Sfvdl	aiov.iov_len = buflen;
6041.1Sfvdl	auio.uio_iov = &aiov;
6051.1Sfvdl	auio.uio_iovcnt = 1;
6061.1Sfvdl	auio.uio_rw = UIO_READ;
6071.1Sfvdl	auio.uio_segflg = UIO_SYSSPACE;
6081.1Sfvdl	auio.uio_procp = p;
6091.1Sfvdl	auio.uio_resid = buflen;
6101.1Sfvdl	auio.uio_offset = off;
6111.1Sfvdl	/*
6121.1Sfvdl         * First we read into the malloc'ed buffer, then
6131.1Sfvdl         * we massage it into user space, one record at a time.
6141.1Sfvdl         */
6151.1Sfvdl	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *) 0, 0);
6161.1Sfvdl	if (error)
6171.1Sfvdl		goto out;
6181.1Sfvdl
6191.1Sfvdl	inp = buf;
6201.1Sfvdl	outp = (caddr_t) SCARG(uap, dent);
6211.1Sfvdl	resid = nbytes;
6221.1Sfvdl	if ((len = buflen - auio.uio_resid) == 0)
6231.1Sfvdl		goto eof;
6241.1Sfvdl
6251.1Sfvdl	for (; len > 0; len -= reclen) {
6261.1Sfvdl		reclen = ((struct dirent *) inp)->d_reclen;
6271.1Sfvdl		if (reclen & 3)
6281.1Sfvdl			panic("linux_readdir");
6291.1Sfvdl		off += reclen;	/* each entry points to next */
6301.1Sfvdl		bdp = (struct dirent *) inp;
6311.1Sfvdl		if (bdp->d_fileno == 0) {
6321.1Sfvdl			inp += reclen;	/* it is a hole; squish it out */
6331.1Sfvdl			continue;
6341.1Sfvdl		}
6351.1Sfvdl		linuxreclen = LINUX_RECLEN(&idb, bdp->d_namlen);
6361.1Sfvdl		if (reclen > len || resid < linuxreclen) {
6371.1Sfvdl			/* entry too big for buffer, so just stop */
6381.1Sfvdl			outp++;
6391.1Sfvdl			break;
6401.1Sfvdl		}
6411.1Sfvdl		/*
6421.1Sfvdl		 * Massage in place to make a Linux-shaped dirent (otherwise
6431.1Sfvdl		 * we have to worry about touching user memory outside of
6441.1Sfvdl		 * the copyout() call).
6451.1Sfvdl		 */
6461.1Sfvdl		idb.l_dino = (long) bdp->d_fileno;
6471.1Sfvdl		idb.l_doff = (linux_off_t) linuxreclen;
6481.1Sfvdl		idb.l_dreclen = (u_short) bdp->d_namlen;	/* sigh */
6491.1Sfvdl		strcpy(idb.l_dname, bdp->d_name);
6501.1Sfvdl		if ((error = copyout((caddr_t)&idb, outp, linuxreclen)))
6511.1Sfvdl			goto out;
6521.1Sfvdl		/* advance past this real entry */
6531.1Sfvdl		inp += reclen;
6541.1Sfvdl		/* advance output past Linux-shaped entry */
6551.1Sfvdl		outp += linuxreclen;
6561.1Sfvdl		resid -= linuxreclen;
6571.1Sfvdl		if (justone)
6581.1Sfvdl			break;
6591.1Sfvdl	}
6601.1Sfvdl
6611.1Sfvdl	/* if we squished out the whole block, try again */
6621.1Sfvdl	if (outp == (caddr_t) SCARG(uap, dent))
6631.1Sfvdl		goto again;
6641.1Sfvdl	fp->f_offset = off;	/* update the vnode offset */
6651.1Sfvdl
6661.1Sfvdl	if (justone)
6671.1Sfvdl		nbytes = resid + linuxreclen;
6681.1Sfvdl
6691.1Sfvdleof:
6701.1Sfvdl	*retval = nbytes - resid;
6711.1Sfvdlout:
6721.1Sfvdl	VOP_UNLOCK(vp);
6731.1Sfvdl	free(buf, M_TEMP);
6741.1Sfvdl	return error;
6751.1Sfvdl}
6761.1Sfvdl
6771.1Sfvdl/*
6781.1Sfvdl * Out of register error once more.. Apart from that, no difference.
6791.1Sfvdl */
6801.1Sfvdlint
6811.1Sfvdllinux_select(p, uap, retval)
6821.1Sfvdl	struct proc *p;
6831.1Sfvdl	struct linux_select_args /* {
6841.1Sfvdl		syscallarg(struct linux_select *) lsp;
6851.1Sfvdl	} */ *uap;
6861.1Sfvdl	register_t *retval;
6871.1Sfvdl{
6881.1Sfvdl	struct linux_select ls;
6891.1Sfvdl	struct select_args bsa;
6901.1Sfvdl	int error;
6911.1Sfvdl
6921.1Sfvdl	if ((error = copyin(SCARG(uap, lsp), (caddr_t) &ls, sizeof ls)))
6931.1Sfvdl		return error;
6941.1Sfvdl
6951.1Sfvdl	SCARG(&bsa, nd) = ls.nfds;
6961.1Sfvdl	SCARG(&bsa, in) = ls.readfds;
6971.1Sfvdl	SCARG(&bsa, ou) = ls.writefds;
6981.1Sfvdl	SCARG(&bsa, ex) = ls.exceptfds;
6991.1Sfvdl	SCARG(&bsa, tv) = ls.timeout;
7001.1Sfvdl
7011.1Sfvdl	return select(p, &bsa, retval);
7021.1Sfvdl}
7031.1Sfvdl
7041.1Sfvdl/*
7051.1Sfvdl * Get the process group of a certain process. Look it up
7061.1Sfvdl * and return the value.
7071.1Sfvdl */
7081.1Sfvdlint
7091.1Sfvdllinux_getpgid(p, uap, retval)
7101.1Sfvdl	struct proc *p;
7111.1Sfvdl	struct linux_getpgid_args /* {
7121.1Sfvdl		syscallarg(int) pid;
7131.1Sfvdl	} */ *uap;
7141.1Sfvdl	register_t *retval;
7151.1Sfvdl{
7161.1Sfvdl	struct proc *targp;
7171.1Sfvdl
7181.1Sfvdl	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid)
7191.1Sfvdl		if ((targp = pfind(SCARG(uap, pid))) == 0)
7201.1Sfvdl			return ESRCH;
7211.1Sfvdl	else
7221.1Sfvdl		targp = p;
7231.1Sfvdl
7241.1Sfvdl	retval[0] = targp->p_pgid;
7251.1Sfvdl	return 0;
7261.1Sfvdl}
727