diff options
| author | cvs2svn <cvs2svn@FreeBSD.org> | 2000-08-28 15:07:59 +0000 |
|---|---|---|
| committer | cvs2svn <cvs2svn@FreeBSD.org> | 2000-08-28 15:07:59 +0000 |
| commit | 895c13aefa8bdc12f6db744772fcd40b081751a3 (patch) | |
| tree | 0271f4b0d035ec2f4a7d204df4d65f1c81869165 /sys/compat/linux | |
| parent | fb2c932b9b390709e2977a804b83ae87da327e2a (diff) | |
Notes
Diffstat (limited to 'sys/compat/linux')
| -rw-r--r-- | sys/compat/linux/linux_misc.c | 1120 | ||||
| -rw-r--r-- | sys/compat/linux/linux_socket.c | 904 |
2 files changed, 2024 insertions, 0 deletions
diff --git a/sys/compat/linux/linux_misc.c b/sys/compat/linux/linux_misc.c new file mode 100644 index 000000000000..8b326582b11f --- /dev/null +++ b/sys/compat/linux/linux_misc.c @@ -0,0 +1,1120 @@ +/*- + * Copyright (c) 1994-1995 Søren Schmidt + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software withough specific prior written permission + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include "opt_compat.h" + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/sysproto.h> +#include <sys/kernel.h> +#include <sys/mman.h> +#include <sys/proc.h> +#include <sys/fcntl.h> +#include <sys/imgact_aout.h> +#include <sys/mount.h> +#include <sys/namei.h> +#include <sys/resourcevar.h> +#include <sys/stat.h> +#include <sys/sysctl.h> +#include <sys/unistd.h> +#include <sys/vnode.h> +#include <sys/wait.h> +#include <sys/time.h> +#include <sys/signalvar.h> + +#include <vm/vm.h> +#include <vm/pmap.h> +#include <vm/vm_kern.h> +#include <vm/vm_map.h> +#include <vm/vm_extern.h> + +#include <machine/frame.h> +#include <machine/limits.h> +#include <machine/psl.h> +#include <machine/sysarch.h> +#include <machine/segments.h> + +#include <posix4/sched.h> + +#include <machine/../linux/linux.h> +#include <machine/../linux/linux_proto.h> +#include <compat/linux/linux_mib.h> +#include <compat/linux/linux_util.h> + +#define BSD_TO_LINUX_SIGNAL(sig) \ + (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig) + +struct linux_rlimit { + unsigned long rlim_cur; + unsigned long rlim_max; +}; + +static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = +{ RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK, + RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE, + RLIMIT_MEMLOCK, -1 +}; + +int +linux_alarm(struct proc *p, struct linux_alarm_args *args) +{ + struct itimerval it, old_it; + struct timeval tv; + int s; + +#ifdef DEBUG + printf("Linux-emul(%ld): alarm(%u)\n", (long)p->p_pid, args->secs); +#endif + if (args->secs > 100000000) + return EINVAL; + it.it_value.tv_sec = (long)args->secs; + it.it_value.tv_usec = 0; + it.it_interval.tv_sec = 0; + it.it_interval.tv_usec = 0; + s = splsoftclock(); + old_it = p->p_realtimer; + getmicrouptime(&tv); + if (timevalisset(&old_it.it_value)) + untimeout(realitexpire, (caddr_t)p, p->p_ithandle); + if (it.it_value.tv_sec != 0) { + p->p_ithandle = timeout(realitexpire, (caddr_t)p, tvtohz(&it.it_value)); + timevaladd(&it.it_value, &tv); + } + p->p_realtimer = it; + splx(s); + if (timevalcmp(&old_it.it_value, &tv, >)) { + timevalsub(&old_it.it_value, &tv); + if (old_it.it_value.tv_usec != 0) + old_it.it_value.tv_sec++; + p->p_retval[0] = old_it.it_value.tv_sec; + } + return 0; +} + +int +linux_brk(struct proc *p, struct linux_brk_args *args) +{ +#if 0 + struct vmspace *vm = p->p_vmspace; + vm_offset_t new, old; + int error; + + if ((vm_offset_t)args->dsend < (vm_offset_t)vm->vm_daddr) + return EINVAL; + if (((caddr_t)args->dsend - (caddr_t)vm->vm_daddr) + > p->p_rlimit[RLIMIT_DATA].rlim_cur) + return ENOMEM; + + old = round_page((vm_offset_t)vm->vm_daddr) + ctob(vm->vm_dsize); + new = round_page((vm_offset_t)args->dsend); + p->p_retval[0] = old; + if ((new-old) > 0) { + if (swap_pager_full) + return ENOMEM; + error = vm_map_find(&vm->vm_map, NULL, 0, &old, (new-old), FALSE, + VM_PROT_ALL, VM_PROT_ALL, 0); + if (error) + return error; + vm->vm_dsize += btoc((new-old)); + p->p_retval[0] = (int)(vm->vm_daddr + ctob(vm->vm_dsize)); + } + return 0; +#else + struct vmspace *vm = p->p_vmspace; + vm_offset_t new, old; + struct obreak_args /* { + char * nsize; + } */ tmp; + +#ifdef DEBUG + printf("Linux-emul(%ld): brk(%p)\n", (long)p->p_pid, (void *)args->dsend); +#endif + old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize); + new = (vm_offset_t)args->dsend; + tmp.nsize = (char *) new; + if (((caddr_t)new > vm->vm_daddr) && !obreak(p, &tmp)) + p->p_retval[0] = (int)new; + else + p->p_retval[0] = (int)old; + + return 0; +#endif +} + +int +linux_uselib(struct proc *p, struct linux_uselib_args *args) +{ + struct nameidata ni; + struct vnode *vp; + struct exec *a_out; + struct vattr attr; + vm_offset_t vmaddr; + unsigned long file_offset; + vm_offset_t buffer; + unsigned long bss_size; + int error; + caddr_t sg; + int locked; + + sg = stackgap_init(); + CHECKALTEXIST(p, &sg, args->library); + +#ifdef DEBUG + printf("Linux-emul(%ld): uselib(%s)\n", (long)p->p_pid, args->library); +#endif + + a_out = NULL; + locked = 0; + vp = NULL; + + NDINIT(&ni, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, args->library, p); + error = namei(&ni); + if (error) + goto cleanup; + + vp = ni.ni_vp; + /* + * XXX This looks like a bogus check - a LOCKLEAF namei should not succeed + * without returning a vnode. + */ + if (vp == NULL) { + error = ENOEXEC; /* ?? */ + goto cleanup; + } + NDFREE(&ni, NDF_ONLY_PNBUF); + + /* + * From here on down, we have a locked vnode that must be unlocked. + */ + locked++; + + /* + * Writable? + */ + if (vp->v_writecount) { + error = ETXTBSY; + goto cleanup; + } + + /* + * Executable? + */ + error = VOP_GETATTR(vp, &attr, p->p_ucred, p); + if (error) + goto cleanup; + + if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || + ((attr.va_mode & 0111) == 0) || + (attr.va_type != VREG)) { + error = ENOEXEC; + goto cleanup; + } + + /* + * Sensible size? + */ + if (attr.va_size == 0) { + error = ENOEXEC; + goto cleanup; + } + + /* + * Can we access it? + */ + error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); + if (error) + goto cleanup; + + error = VOP_OPEN(vp, FREAD, p->p_ucred, p); + if (error) + goto cleanup; + + /* + * Lock no longer needed + */ + VOP_UNLOCK(vp, 0, p); + locked = 0; + + /* + * Pull in executable header into kernel_map + */ + error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE, + VM_PROT_READ, VM_PROT_READ, 0, (caddr_t)vp, 0); + if (error) + goto cleanup; + + /* + * Is it a Linux binary ? + */ + if (((a_out->a_magic >> 16) & 0xff) != 0x64) { + error = ENOEXEC; + goto cleanup; + } + + /* While we are here, we should REALLY do some more checks */ + + /* + * Set file/virtual offset based on a.out variant. + */ + switch ((int)(a_out->a_magic & 0xffff)) { + case 0413: /* ZMAGIC */ + file_offset = 1024; + break; + case 0314: /* QMAGIC */ + file_offset = 0; + break; + default: + error = ENOEXEC; + goto cleanup; + } + + bss_size = round_page(a_out->a_bss); + + /* + * Check various fields in header for validity/bounds. + */ + if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) { + error = ENOEXEC; + goto cleanup; + } + + /* text + data can't exceed file size */ + if (a_out->a_data + a_out->a_text > attr.va_size) { + error = EFAULT; + goto cleanup; + } + + /* + * text/data/bss must not exceed limits + * XXX: this is not complete. it should check current usage PLUS + * the resources needed by this library. + */ + if (a_out->a_text > MAXTSIZ || + a_out->a_data + bss_size > p->p_rlimit[RLIMIT_DATA].rlim_cur) { + error = ENOMEM; + goto cleanup; + } + + /* + * prevent more writers + */ + vp->v_flag |= VTEXT; + + /* + * Check if file_offset page aligned,. + * Currently we cannot handle misalinged file offsets, + * and so we read in the entire image (what a waste). + */ + if (file_offset & PAGE_MASK) { +#ifdef DEBUG +printf("uselib: Non page aligned binary %lu\n", file_offset); +#endif + /* + * Map text+data read/write/execute + */ + + /* a_entry is the load address and is page aligned */ + vmaddr = trunc_page(a_out->a_entry); + + /* get anon user mapping, read+write+execute */ + error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &vmaddr, + a_out->a_text + a_out->a_data, FALSE, + VM_PROT_ALL, VM_PROT_ALL, 0); + if (error) + goto cleanup; + + /* map file into kernel_map */ + error = vm_mmap(kernel_map, &buffer, + round_page(a_out->a_text + a_out->a_data + file_offset), + VM_PROT_READ, VM_PROT_READ, 0, + (caddr_t)vp, trunc_page(file_offset)); + if (error) + goto cleanup; + + /* copy from kernel VM space to user space */ + error = copyout((caddr_t)(void *)(uintptr_t)(buffer + file_offset), + (caddr_t)vmaddr, a_out->a_text + a_out->a_data); + + /* release temporary kernel space */ + vm_map_remove(kernel_map, buffer, + buffer + round_page(a_out->a_text + a_out->a_data + file_offset)); + + if (error) + goto cleanup; + } + else { +#ifdef DEBUG +printf("uselib: Page aligned binary %lu\n", file_offset); +#endif + /* + * for QMAGIC, a_entry is 20 bytes beyond the load address + * to skip the executable header + */ + vmaddr = trunc_page(a_out->a_entry); + + /* + * Map it all into the process's space as a single copy-on-write + * "data" segment. + */ + error = vm_mmap(&p->p_vmspace->vm_map, &vmaddr, + a_out->a_text + a_out->a_data, + VM_PROT_ALL, VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, + (caddr_t)vp, file_offset); + if (error) + goto cleanup; + } +#ifdef DEBUG +printf("mem=%08x = %08x %08x\n", vmaddr, ((int*)vmaddr)[0], ((int*)vmaddr)[1]); +#endif + if (bss_size != 0) { + /* + * Calculate BSS start address + */ + vmaddr = trunc_page(a_out->a_entry) + a_out->a_text + a_out->a_data; + + /* + * allocate some 'anon' space + */ + error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &vmaddr, + bss_size, FALSE, + VM_PROT_ALL, VM_PROT_ALL, 0); + if (error) + goto cleanup; + } + +cleanup: + /* + * Unlock vnode if needed + */ + if (locked) + VOP_UNLOCK(vp, 0, p); + + /* + * Release the kernel mapping. + */ + if (a_out) + vm_map_remove(kernel_map, (vm_offset_t)a_out, (vm_offset_t)a_out + PAGE_SIZE); + + return error; +} + +int +linux_newselect(struct proc *p, struct linux_newselect_args *args) +{ + struct select_args bsa; + struct timeval tv0, tv1, utv, *tvp; + caddr_t sg; + int error; + +#ifdef DEBUG + printf("Linux-emul(%ld): newselect(%d, %p, %p, %p, %p)\n", + (long)p->p_pid, args->nfds, (void *)args->readfds, + (void *)args->writefds, (void *)args->exceptfds, + (void *)args->timeout); +#endif + error = 0; + bsa.nd = args->nfds; + bsa.in = args->readfds; + bsa.ou = args->writefds; + bsa.ex = args->exceptfds; + bsa.tv = args->timeout; + + /* + * Store current time for computation of the amount of + * time left. + */ + if (args->timeout) { + if ((error = copyin(args->timeout, &utv, sizeof(utv)))) + goto select_out; +#ifdef DEBUG + printf("Linux-emul(%ld): incoming timeout (%ld/%ld)\n", + (long)p->p_pid, utv.tv_sec, utv.tv_usec); +#endif + if (itimerfix(&utv)) { + /* + * The timeval was invalid. Convert it to something + * valid that will act as it does under Linux. + */ + sg = stackgap_init(); + tvp = stackgap_alloc(&sg, sizeof(utv)); + utv.tv_sec += utv.tv_usec / 1000000; + utv.tv_usec %= 1000000; + if (utv.tv_usec < 0) { + utv.tv_sec -= 1; + utv.tv_usec += 1000000; + } + if (utv.tv_sec < 0) + timevalclear(&utv); + if ((error = copyout(&utv, tvp, sizeof(utv)))) + goto select_out; + bsa.tv = tvp; + } + microtime(&tv0); + } + + error = select(p, &bsa); +#ifdef DEBUG + printf("Linux-emul(%ld): real select returns %d\n", (long)p->p_pid, error); +#endif + + if (error) { + /* + * See fs/select.c in the Linux kernel. Without this, + * Maelstrom doesn't work. + */ + if (error == ERESTART) + error = EINTR; + goto select_out; + } + + if (args->timeout) { + if (p->p_retval[0]) { + /* + * Compute how much time was left of the timeout, + * by subtracting the current time and the time + * before we started the call, and subtracting + * that result from the user-supplied value. + */ + microtime(&tv1); + timevalsub(&tv1, &tv0); + timevalsub(&utv, &tv1); + if (utv.tv_sec < 0) + timevalclear(&utv); + } else + timevalclear(&utv); +#ifdef DEBUG + printf("Linux-emul(%ld): outgoing timeout (%ld/%ld)\n", + (long)p->p_pid, utv.tv_sec, utv.tv_usec); +#endif + if ((error = copyout(&utv, args->timeout, sizeof(utv)))) + goto select_out; + } + +select_out: +#ifdef DEBUG + printf("Linux-emul(%ld): newselect_out -> %d\n", (long)p->p_pid, error); +#endif + return error; +} + +int +linux_getpgid(struct proc *p, struct linux_getpgid_args *args) +{ + struct proc *curp; + +#ifdef DEBUG + printf("Linux-emul(%ld): getpgid(%d)\n", (long)p->p_pid, args->pid); +#endif + if (args->pid != p->p_pid) { + if (!(curp = pfind(args->pid))) + return ESRCH; + } + else + curp = p; + p->p_retval[0] = curp->p_pgid; + return 0; +} + +int +linux_mremap(struct proc *p, struct linux_mremap_args *args) +{ + struct munmap_args /* { + void *addr; + size_t len; + } */ bsd_args; + int error = 0; + +#ifdef DEBUG + printf("Linux-emul(%ld): mremap(%p, %08x, %08x, %08x)\n", + (long)p->p_pid, (void *)args->addr, args->old_len, args->new_len, + args->flags); +#endif + args->new_len = round_page(args->new_len); + args->old_len = round_page(args->old_len); + + if (args->new_len > args->old_len) { + p->p_retval[0] = 0; + return ENOMEM; + } + + if (args->new_len < args->old_len) { + bsd_args.addr = args->addr + args->new_len; + bsd_args.len = args->old_len - args->new_len; + error = munmap(p, &bsd_args); + } + + p->p_retval[0] = error ? 0 : (int)args->addr; + return error; +} + +int +linux_msync(struct proc *p, struct linux_msync_args *args) +{ + struct msync_args bsd_args; + + bsd_args.addr = args->addr; + bsd_args.len = args->len; + bsd_args.flags = 0; /* XXX ignore */ + + return msync(p, &bsd_args); +} + +int +linux_time(struct proc *p, struct linux_time_args *args) +{ + struct timeval tv; + linux_time_t tm; + int error; + +#ifdef DEBUG + printf("Linux-emul(%ld): time(*)\n", (long)p->p_pid); +#endif + microtime(&tv); + tm = tv.tv_sec; + if (args->tm && (error = copyout(&tm, args->tm, sizeof(linux_time_t)))) + return error; + p->p_retval[0] = tm; + return 0; +} + +struct linux_times_argv { + long tms_utime; + long tms_stime; + long tms_cutime; + long tms_cstime; +}; + +#define CLK_TCK 100 /* Linux uses 100 */ +#define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) + +int +linux_times(struct proc *p, struct linux_times_args *args) +{ + struct timeval tv; + struct linux_times_argv tms; + struct rusage ru; + int error; + +#ifdef DEBUG + printf("Linux-emul(%ld): times(*)\n", (long)p->p_pid); +#endif + calcru(p, &ru.ru_utime, &ru.ru_stime, NULL); + + tms.tms_utime = CONVTCK(ru.ru_utime); + tms.tms_stime = CONVTCK(ru.ru_stime); + + tms.tms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime); + tms.tms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime); + + if ((error = copyout((caddr_t)&tms, (caddr_t)args->buf, + sizeof(struct linux_times_argv)))) + return error; + + microuptime(&tv); + p->p_retval[0] = (int)CONVTCK(tv); + return 0; +} + +int +linux_newuname(struct proc *p, struct linux_newuname_args *args) +{ + struct linux_new_utsname utsname; + char *osrelease, *osname; + +#ifdef DEBUG + printf("Linux-emul(%ld): newuname(*)\n", (long)p->p_pid); +#endif + + osname = linux_get_osname(p); + osrelease = linux_get_osrelease(p); + + bzero(&utsname, sizeof(struct linux_new_utsname)); + strncpy(utsname.sysname, osname, LINUX_MAX_UTSNAME-1); + strncpy(utsname.nodename, hostname, LINUX_MAX_UTSNAME-1); + strncpy(utsname.release, osrelease, LINUX_MAX_UTSNAME-1); + strncpy(utsname.version, version, LINUX_MAX_UTSNAME-1); + strncpy(utsname.machine, machine, LINUX_MAX_UTSNAME-1); + strncpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME-1); + + return (copyout((caddr_t)&utsname, (caddr_t)args->buf, + sizeof(struct linux_new_utsname))); +} + +struct linux_utimbuf { + linux_time_t l_actime; + linux_time_t l_modtime; +}; + +int +linux_utime(struct proc *p, struct linux_utime_args *args) +{ + struct utimes_args /* { + char *path; + struct timeval *tptr; + } */ bsdutimes; + struct timeval tv[2], *tvp; + struct linux_utimbuf lut; + int error; + caddr_t sg; + + sg = stackgap_init(); + CHECKALTEXIST(p, &sg, args->fname); + +#ifdef DEBUG + printf("Linux-emul(%ld): utime(%s, *)\n", (long)p->p_pid, args->fname); +#endif + if (args->times) { + if ((error = copyin(args->times, &lut, sizeof lut))) + return error; + tv[0].tv_sec = lut.l_actime; + tv[0].tv_usec = 0; + tv[1].tv_sec = lut.l_modtime; + tv[1].tv_usec = 0; + /* so that utimes can copyin */ + tvp = (struct timeval *)stackgap_alloc(&sg, sizeof(tv)); + if (tvp == NULL) + return (ENAMETOOLONG); + if ((error = copyout(tv, tvp, sizeof(tv)))) + return error; + bsdutimes.tptr = tvp; + } else + bsdutimes.tptr = NULL; + + bsdutimes.path = args->fname; + return utimes(p, &bsdutimes); +} + +#define __WCLONE 0x80000000 + +int +linux_waitpid(struct proc *p, struct linux_waitpid_args *args) +{ + struct wait_args /* { + int pid; + int *status; + int options; + struct rusage *rusage; + } */ tmp; + int error, tmpstat; + +#ifdef DEBUG + printf("Linux-emul(%ld): waitpid(%d, %p, %d)\n", + (long)p->p_pid, args->pid, (void *)args->status, args->options); +#endif + tmp.pid = args->pid; + tmp.status = args->status; + tmp.options = (args->options & (WNOHANG | WUNTRACED)); + /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ + if (args->options & __WCLONE) + tmp.options |= WLINUXCLONE; + tmp.rusage = NULL; + + if ((error = wait4(p, &tmp)) != 0) + return error; + + if (args->status) { + if ((error = copyin(args->status, &tmpstat, sizeof(int))) != 0) + return error; + tmpstat &= 0xffff; + if (WIFSIGNALED(tmpstat)) + tmpstat = (tmpstat & 0xffffff80) | + BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); + else if (WIFSTOPPED(tmpstat)) + tmpstat = (tmpstat & 0xffff00ff) | + (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); + return copyout(&tmpstat, args->status, sizeof(int)); + } else + return 0; +} + +int +linux_wait4(struct proc *p, struct linux_wait4_args *args) +{ + struct wait_args /* { + int pid; + int *status; + int options; + struct rusage *rusage; + } */ tmp; + int error, tmpstat; + +#ifdef DEBUG + printf("Linux-emul(%ld): wait4(%d, %p, %d, %p)\n", + (long)p->p_pid, args->pid, (void *)args->status, args->options, + (void *)args->rusage); +#endif + tmp.pid = args->pid; + tmp.status = args->status; + tmp.options = (args->options & (WNOHANG | WUNTRACED)); + /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ + if (args->options & __WCLONE) + tmp.options |= WLINUXCLONE; + tmp.rusage = args->rusage; + + if ((error = wait4(p, &tmp)) != 0) + return error; + + SIGDELSET(p->p_siglist, SIGCHLD); + + if (args->status) { + if ((error = copyin(args->status, &tmpstat, sizeof(int))) != 0) + return error; + tmpstat &= 0xffff; + if (WIFSIGNALED(tmpstat)) + tmpstat = (tmpstat & 0xffffff80) | + BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); + else if (WIFSTOPPED(tmpstat)) + tmpstat = (tmpstat & 0xffff00ff) | + (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); + return copyout(&tmpstat, args->status, sizeof(int)); + } else + return 0; +} + +int +linux_mknod(struct proc *p, struct linux_mknod_args *args) +{ + caddr_t sg; + struct mknod_args bsd_mknod; + struct mkfifo_args bsd_mkfifo; + + sg = stackgap_init(); + + CHECKALTCREAT(p, &sg, args->path); + +#ifdef DEBUG + printf("Linux-emul(%ld): mknod(%s, %d, %d)\n", + (long)p->p_pid, args->path, args->mode, args->dev); +#endif + + if (args->mode & S_IFIFO) { + bsd_mkfifo.path = args->path; + bsd_mkfifo.mode = args->mode; + return mkfifo(p, &bsd_mkfifo); + } else { + bsd_mknod.path = args->path; + bsd_mknod.mode = args->mode; + bsd_mknod.dev = args->dev; + return mknod(p, &bsd_mknod); + } +} + +/* + * UGH! This is just about the dumbest idea I've ever heard!! + */ +int +linux_personality(struct proc *p, struct linux_personality_args *args) +{ +#ifdef DEBUG + printf("Linux-emul(%ld): personality(%d)\n", + (long)p->p_pid, args->per); +#endif + if (args->per != 0) + return EINVAL; + + /* Yes Jim, it's still a Linux... */ + p->p_retval[0] = 0; + return 0; +} + +/* + * Wrappers for get/setitimer for debugging.. + */ +int +linux_setitimer(struct proc *p, struct linux_setitimer_args *args) +{ + struct setitimer_args bsa; + struct itimerval foo; + int error; + +#ifdef DEBUG + printf("Linux-emul(%ld): setitimer(%p, %p)\n", + (long)p->p_pid, (void *)args->itv, (void *)args->oitv); +#endif + bsa.which = args->which; + bsa.itv = args->itv; + bsa.oitv = args->oitv; + if (args->itv) { + if ((error = copyin((caddr_t)args->itv, (caddr_t)&foo, + sizeof(foo)))) + return error; +#ifdef DEBUG + printf("setitimer: value: sec: %ld, usec: %ld\n", + foo.it_value.tv_sec, foo.it_value.tv_usec); + printf("setitimer: interval: sec: %ld, usec: %ld\n", + foo.it_interval.tv_sec, foo.it_interval.tv_usec); +#endif + } + return setitimer(p, &bsa); +} + +int +linux_getitimer(struct proc *p, struct linux_getitimer_args *args) +{ + struct getitimer_args bsa; +#ifdef DEBUG + printf("Linux-emul(%ld): getitimer(%p)\n", + (long)p->p_pid, (void *)args->itv); +#endif + bsa.which = args->which; + bsa.itv = args->itv; + return getitimer(p, &bsa); +} + +int +linux_nice(struct proc *p, struct linux_nice_args *args) +{ + struct setpriority_args bsd_args; + + bsd_args.which = PRIO_PROCESS; + bsd_args.who = 0; /* current process */ + bsd_args.prio = args->inc; + return setpriority(p, &bsd_args); +} + +int +linux_setgroups(p, uap) + struct proc *p; + struct linux_setgroups_args *uap; +{ + struct pcred *pc; + linux_gid_t linux_gidset[NGROUPS]; + gid_t *bsd_gidset; + int ngrp, error; + + pc = p->p_cred; + ngrp = uap->gidsetsize; + + /* + * cr_groups[0] holds egid. Setting the whole set from + * the supplied set will cause egid to be changed too. + * Keep cr_groups[0] unchanged to prevent that. + */ + + if ((error = suser(p)) != 0) + return (error); + + if (ngrp >= NGROUPS) + return (EINVAL); + + pc->pc_ucred = crcopy(pc->pc_ucred); + if (ngrp > 0) { + error = copyin((caddr_t)uap->gidset, (caddr_t)linux_gidset, + ngrp * sizeof(linux_gid_t)); + if (error) + return (error); + + pc->pc_ucred->cr_ngroups = ngrp + 1; + + bsd_gidset = pc->pc_ucred->cr_groups; + ngrp--; + while (ngrp >= 0) { + bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; + ngrp--; + } + } + else + pc->pc_ucred->cr_ngroups = 1; + + setsugid(p); + return (0); +} + +int +linux_getgroups(p, uap) + struct proc *p; + struct linux_getgroups_args *uap; +{ + struct pcred *pc; + linux_gid_t linux_gidset[NGROUPS]; + gid_t *bsd_gidset; + int bsd_gidsetsz, ngrp, error; + + pc = p->p_cred; + bsd_gidset = pc->pc_ucred->cr_groups; + bsd_gidsetsz = pc->pc_ucred->cr_ngroups - 1; + + /* + * cr_groups[0] holds egid. Returning the whole set + * here will cause a duplicate. Exclude cr_groups[0] + * to prevent that. + */ + + if ((ngrp = uap->gidsetsize) == 0) { + p->p_retval[0] = bsd_gidsetsz; + return (0); + } + + if (ngrp < bsd_gidsetsz) + return (EINVAL); + + ngrp = 0; + while (ngrp < bsd_gidsetsz) { + linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; + ngrp++; + } + + if ((error = copyout((caddr_t)linux_gidset, (caddr_t)uap->gidset, + ngrp * sizeof(linux_gid_t)))) + return (error); + + p->p_retval[0] = ngrp; + return (0); +} + +int +linux_setrlimit(p, uap) + struct proc *p; + struct linux_setrlimit_args *uap; +{ + struct __setrlimit_args bsd; + struct linux_rlimit rlim; + int error; + caddr_t sg = stackgap_init(); + +#ifdef DEBUG + printf("Linux-emul(%ld): setrlimit(%d, %p)\n", (long)p->p_pid, + uap->resource, (void *)uap->rlim); +#endif + + if (uap->resource >= LINUX_RLIM_NLIMITS) + return (EINVAL); + + bsd.which = linux_to_bsd_resource[uap->resource]; + if (bsd.which == -1) + return (EINVAL); + + error = copyin(uap->rlim, &rlim, sizeof(rlim)); + if (error) + return (error); + + bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit)); + bsd.rlp->rlim_cur = (rlim_t)rlim.rlim_cur; + bsd.rlp->rlim_max = (rlim_t)rlim.rlim_max; + return (setrlimit(p, &bsd)); +} + +int +linux_getrlimit(p, uap) + struct proc *p; + struct linux_getrlimit_args *uap; +{ + struct __getrlimit_args bsd; + struct linux_rlimit rlim; + int error; + caddr_t sg = stackgap_init(); + +#ifdef DEBUG + printf("Linux-emul(%ld): getrlimit(%d, %p)\n", (long)p->p_pid, + uap->resource, (void *)uap->rlim); +#endif + + if (uap->resource >= LINUX_RLIM_NLIMITS) + return (EINVAL); + + bsd.which = linux_to_bsd_resource[uap->resource]; + if (bsd.which == -1) + return (EINVAL); + + bsd.rlp = stackgap_alloc(&sg, sizeof(struct rlimit)); + error = getrlimit(p, &bsd); + if (error) + return (error); + + rlim.rlim_cur = (unsigned long)bsd.rlp->rlim_cur; + if (rlim.rlim_cur == ULONG_MAX) + rlim.rlim_cur = LONG_MAX; + rlim.rlim_max = (unsigned long)bsd.rlp->rlim_max; + if (rlim.rlim_max == ULONG_MAX) + rlim.rlim_max = LONG_MAX; + return (copyout(&rlim, uap->rlim, sizeof(rlim))); +} + +int +linux_sched_setscheduler(p, uap) + struct proc *p; + struct linux_sched_setscheduler_args *uap; +{ + struct sched_setscheduler_args bsd; + +#ifdef DEBUG + printf("Linux-emul(%ld): sched_setscheduler(%d, %d, %p)\n", + (long)p->p_pid, uap->pid, uap->policy, (const void *)uap->param); +#endif + + switch (uap->policy) { + case LINUX_SCHED_OTHER: + bsd.policy = SCHED_OTHER; + break; + case LINUX_SCHED_FIFO: + bsd.policy = SCHED_FIFO; + break; + case LINUX_SCHED_RR: + bsd.policy = SCHED_RR; + break; + default: + return EINVAL; + } + + bsd.pid = uap->pid; + bsd.param = uap->param; + return sched_setscheduler(p, &bsd); +} + +int +linux_sched_getscheduler(p, uap) + struct proc *p; + struct linux_sched_getscheduler_args *uap; +{ + struct sched_getscheduler_args bsd; + int error; + +#ifdef DEBUG + printf("Linux-emul(%ld): sched_getscheduler(%d)\n", + (long)p->p_pid, uap->pid); +#endif + + bsd.pid = uap->pid; + error = sched_getscheduler(p, &bsd); + + switch (p->p_retval[0]) { + case SCHED_OTHER: + p->p_retval[0] = LINUX_SCHED_OTHER; + break; + case SCHED_FIFO: + p->p_retval[0] = LINUX_SCHED_FIFO; + break; + case SCHED_RR: + p->p_retval[0] = LINUX_SCHED_RR; + break; + } + + return error; +} diff --git a/sys/compat/linux/linux_socket.c b/sys/compat/linux/linux_socket.c new file mode 100644 index 000000000000..b7c9b7da8460 --- /dev/null +++ b/sys/compat/linux/linux_socket.c @@ -0,0 +1,904 @@ +/*- + * Copyright (c) 1995 Søren Schmidt + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software withough specific prior written permission + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* XXX we use functions that might not exist. */ +#include "opt_compat.h" + +#ifndef COMPAT_43 +#error "Unable to compile Linux-emulator due to missing COMPAT_43 option!" +#endif + +#include <sys/param.h> +#include <sys/proc.h> +#include <sys/systm.h> +#include <sys/sysproto.h> +#include <sys/fcntl.h> +#include <sys/socket.h> +#include <sys/uio.h> + +#include <netinet/in.h> +#include <netinet/in_systm.h> +#include <netinet/ip.h> + +#include <machine/../linux/linux.h> +#include <machine/../linux/linux_proto.h> +#include <compat/linux/linux_util.h> + +static int +linux_to_bsd_domain(int domain) +{ + + switch (domain) { + case LINUX_AF_UNSPEC: + return (AF_UNSPEC); + case LINUX_AF_UNIX: + return (AF_LOCAL); + case LINUX_AF_INET: + return (AF_INET); + case LINUX_AF_AX25: + return (AF_CCITT); + case LINUX_AF_IPX: + return (AF_IPX); + case LINUX_AF_APPLETALK: + return (AF_APPLETALK); + } + return (-1); +} + +static int +linux_to_bsd_sockopt_level(int level) +{ + + switch (level) { + case LINUX_SOL_SOCKET: + return (SOL_SOCKET); + } + return (level); +} + +static int +linux_to_bsd_ip_sockopt(int opt) +{ + + switch (opt) { + case LINUX_IP_TOS: + return (IP_TOS); + case LINUX_IP_TTL: + return (IP_TTL); + case LINUX_IP_OPTIONS: + return (IP_OPTIONS); + case LINUX_IP_MULTICAST_IF: + return (IP_MULTICAST_IF); + case LINUX_IP_MULTICAST_TTL: + return (IP_MULTICAST_TTL); + case LINUX_IP_MULTICAST_LOOP: + return (IP_MULTICAST_LOOP); + case LINUX_IP_ADD_MEMBERSHIP: + return (IP_ADD_MEMBERSHIP); + case LINUX_IP_DROP_MEMBERSHIP: + return (IP_DROP_MEMBERSHIP); + case LINUX_IP_HDRINCL: + return (IP_HDRINCL); + } + return (-1); +} + +static int +linux_to_bsd_so_sockopt(int opt) +{ + + switch (opt) { + case LINUX_SO_DEBUG: + return (SO_DEBUG); + case LINUX_SO_REUSEADDR: + return (SO_REUSEADDR); + case LINUX_SO_TYPE: + return (SO_TYPE); + case LINUX_SO_ERROR: + return (SO_ERROR); + case LINUX_SO_DONTROUTE: + return (SO_DONTROUTE); + case LINUX_SO_BROADCAST: + return (SO_BROADCAST); + case LINUX_SO_SNDBUF: + return (SO_SNDBUF); + case LINUX_SO_RCVBUF: + return (SO_RCVBUF); + case LINUX_SO_KEEPALIVE: + return (SO_KEEPALIVE); + case LINUX_SO_OOBINLINE: + return (SO_OOBINLINE); + case LINUX_SO_LINGER: + return (SO_LINGER); + } + return (-1); +} + +/* Return 0 if IP_HDRINCL is set for the given socket. */ +static int +linux_check_hdrincl(struct proc *p, int s) +{ + struct getsockopt_args /* { + int s; + int level; + int name; + caddr_t val; + int *avalsize; + } */ bsd_args; + int error; + caddr_t sg, val, valsize; + int size_val = sizeof val; + int optval; + + sg = stackgap_init(); + val = stackgap_alloc(&sg, sizeof(int)); + valsize = stackgap_alloc(&sg, sizeof(int)); + + if ((error = copyout(&size_val, valsize, sizeof(size_val)))) + return (error); + + bsd_args.s = s; + bsd_args.level = IPPROTO_IP; + bsd_args.name = IP_HDRINCL; + bsd_args.val = val; + bsd_args.avalsize = (int *)valsize; + if ((error = getsockopt(p, &bsd_args))) + return (error); + + if ((error = copyin(val, &optval, sizeof(optval)))) + return (error); + + return (optval == 0); +} + +/* + * Updated sendto() when IP_HDRINCL is set: + * tweak endian-dependent fields in the IP packet. + */ +static int +linux_sendto_hdrincl(struct proc *p, struct sendto_args *bsd_args) +{ +/* + * linux_ip_copysize defines how many bytes we should copy + * from the beginning of the IP packet before we customize it for BSD. + * It should include all the fields we modify (ip_len and ip_off) + * and be as small as possible to minimize copying overhead. + */ +#define linux_ip_copysize 8 + + caddr_t sg; + struct ip *packet; + struct msghdr *msg; + struct iovec *iov; + + int error; + struct sendmsg_args /* { + int s; + caddr_t msg; + int flags; + } */ sendmsg_args; + + /* Check the packet isn't too small before we mess with it */ + if (bsd_args->len < linux_ip_copysize) + return (EINVAL); + + /* + * Tweaking the user buffer in place would be bad manners. + * We create a corrected IP header with just the needed length, + * then use an iovec to glue it to the rest of the user packet + * when calling sendmsg(). + */ + sg = stackgap_init(); + packet = (struct ip *)stackgap_alloc(&sg, linux_ip_copysize); + msg = (struct msghdr *)stackgap_alloc(&sg, sizeof(*msg)); + iov = (struct iovec *)stackgap_alloc(&sg, sizeof(*iov)*2); + + /* Make a copy of the beginning of the packet to be sent */ + if ((error = copyin(bsd_args->buf, packet, linux_ip_copysize))) + return (error); + + /* Convert fields from Linux to BSD raw IP socket format */ + packet->ip_len = bsd_args->len; + packet->ip_off = ntohs(packet->ip_off); + + /* Prepare the msghdr and iovec structures describing the new packet */ + msg->msg_name = bsd_args->to; + msg->msg_namelen = bsd_args->tolen; + msg->msg_iov = iov; + msg->msg_iovlen = 2; + msg->msg_control = NULL; + msg->msg_controllen = 0; + msg->msg_flags = 0; + iov[0].iov_base = (char *)packet; + iov[0].iov_len = linux_ip_copysize; + iov[1].iov_base = (char *)(bsd_args->buf) + linux_ip_copysize; + iov[1].iov_len = bsd_args->len - linux_ip_copysize; + + sendmsg_args.s = bsd_args->s; + sendmsg_args.msg = (caddr_t)msg; + sendmsg_args.flags = bsd_args->flags; + return (sendmsg(p, &sendmsg_args)); +} + +struct linux_socket_args { + int domain; + int type; + int protocol; +}; + +static int +linux_socket(struct proc *p, struct linux_socket_args *args) +{ + struct linux_socket_args linux_args; + struct socket_args /* { + int domain; + int type; + int protocol; + } */ bsd_args; + int error; + int retval_socket; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.protocol = linux_args.protocol; + bsd_args.type = linux_args.type; + bsd_args.domain = linux_to_bsd_domain(linux_args.domain); + if (bsd_args.domain == -1) + return (EINVAL); + + retval_socket = socket(p, &bsd_args); + if (bsd_args.type == SOCK_RAW + && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0) + && bsd_args.domain == AF_INET + && retval_socket >= 0) { + /* It's a raw IP socket: set the IP_HDRINCL option. */ + struct setsockopt_args /* { + int s; + int level; + int name; + caddr_t val; + int valsize; + } */ bsd_setsockopt_args; + caddr_t sg; + int *hdrincl; + + sg = stackgap_init(); + hdrincl = (int *)stackgap_alloc(&sg, sizeof(*hdrincl)); + *hdrincl = 1; + bsd_setsockopt_args.s = p->p_retval[0]; + bsd_setsockopt_args.level = IPPROTO_IP; + bsd_setsockopt_args.name = IP_HDRINCL; + bsd_setsockopt_args.val = (caddr_t)hdrincl; + bsd_setsockopt_args.valsize = sizeof(*hdrincl); + /* We ignore any error returned by setsockopt() */ + setsockopt(p, &bsd_setsockopt_args); + /* Copy back the return value from socket() */ + p->p_retval[0] = bsd_setsockopt_args.s; + } + + return (retval_socket); +} + +struct linux_bind_args { + int s; + struct sockaddr *name; + int namelen; +}; + +static int +linux_bind(struct proc *p, struct linux_bind_args *args) +{ + struct linux_bind_args linux_args; + struct bind_args /* { + int s; + caddr_t name; + int namelen; + } */ bsd_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.s = linux_args.s; + bsd_args.name = (caddr_t)linux_args.name; + bsd_args.namelen = linux_args.namelen; + return (bind(p, &bsd_args)); +} + +struct linux_connect_args { + int s; + struct sockaddr * name; + int namelen; +}; + +static int +linux_connect(struct proc *p, struct linux_connect_args *args) +{ + struct linux_connect_args linux_args; + struct connect_args /* { + int s; + caddr_t name; + int namelen; + } */ bsd_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.s = linux_args.s; + bsd_args.name = (caddr_t)linux_args.name; + bsd_args.namelen = linux_args.namelen; + error = connect(p, &bsd_args); + if (error == EISCONN) { + /* + * Linux doesn't return EISCONN the first time it occurs, + * when on a non-blocking socket. Instead it returns the + * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD. + */ + struct fcntl_args /* { + int fd; + int cmd; + int arg; + } */ bsd_fcntl_args; + struct getsockopt_args /* { + int s; + int level; + int name; + caddr_t val; + int *avalsize; + } */ bsd_getsockopt_args; + void *status, *statusl; + int stat, statl = sizeof stat; + caddr_t sg; + + /* Check for non-blocking */ + bsd_fcntl_args.fd = linux_args.s; + bsd_fcntl_args.cmd = F_GETFL; + bsd_fcntl_args.arg = 0; + error = fcntl(p, &bsd_fcntl_args); + if (error == 0 && (p->p_retval[0] & O_NONBLOCK)) { + sg = stackgap_init(); + status = stackgap_alloc(&sg, sizeof stat); + statusl = stackgap_alloc(&sg, sizeof statusl); + + if ((error = copyout(&statl, statusl, sizeof statl))) + return (error); + + bsd_getsockopt_args.s = linux_args.s; + bsd_getsockopt_args.level = SOL_SOCKET; + bsd_getsockopt_args.name = SO_ERROR; + bsd_getsockopt_args.val = status; + bsd_getsockopt_args.avalsize = statusl; + + error = getsockopt(p, &bsd_getsockopt_args); + if (error) + return (error); + + if ((error = copyin(status, &stat, sizeof stat))) + return (error); + + p->p_retval[0] = stat; + return (0); + } + } + + return (error); +} + +struct linux_listen_args { + int s; + int backlog; +}; + +static int +linux_listen(struct proc *p, struct linux_listen_args *args) +{ + struct linux_listen_args linux_args; + struct listen_args /* { + int s; + int backlog; + } */ bsd_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.s = linux_args.s; + bsd_args.backlog = linux_args.backlog; + return (listen(p, &bsd_args)); +} + +struct linux_accept_args { + int s; + struct sockaddr *addr; + int *namelen; +}; + +static int +linux_accept(struct proc *p, struct linux_accept_args *args) +{ + struct linux_accept_args linux_args; + struct accept_args /* { + int s; + caddr_t name; + int *anamelen; + } */ bsd_args; + struct fcntl_args /* { + int fd; + int cmd; + long arg; + } */ f_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.s = linux_args.s; + bsd_args.name = (caddr_t)linux_args.addr; + bsd_args.anamelen = linux_args.namelen; + error = oaccept(p, &bsd_args); + if (error) + return (error); + + /* + * linux appears not to copy flags from the parent socket to the + * accepted one, so we must clear the flags in the new descriptor. + * Ignore any errors, because we already have an open fd. + */ + f_args.fd = p->p_retval[0]; + f_args.cmd = F_SETFL; + f_args.arg = 0; + (void)fcntl(p, &f_args); + p->p_retval[0] = f_args.fd; + return (0); +} + +struct linux_getsockname_args { + int s; + struct sockaddr *addr; + int *namelen; +}; + +static int +linux_getsockname(struct proc *p, struct linux_getsockname_args *args) +{ + struct linux_getsockname_args linux_args; + struct getsockname_args /* { + int fdes; + caddr_t asa; + int *alen; + } */ bsd_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.fdes = linux_args.s; + bsd_args.asa = (caddr_t) linux_args.addr; + bsd_args.alen = linux_args.namelen; + return (ogetsockname(p, &bsd_args)); +} + +struct linux_getpeername_args { + int s; + struct sockaddr *addr; + int *namelen; +}; + +static int +linux_getpeername(struct proc *p, struct linux_getpeername_args *args) +{ + struct linux_getpeername_args linux_args; + struct ogetpeername_args /* { + int fdes; + caddr_t asa; + int *alen; + } */ bsd_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.fdes = linux_args.s; + bsd_args.asa = (caddr_t) linux_args.addr; + bsd_args.alen = linux_args.namelen; + return (ogetpeername(p, &bsd_args)); +} + +struct linux_socketpair_args { + int domain; + int type; + int protocol; + int *rsv; +}; + +static int +linux_socketpair(struct proc *p, struct linux_socketpair_args *args) +{ + struct linux_socketpair_args linux_args; + struct socketpair_args /* { + int domain; + int type; + int protocol; + int *rsv; + } */ bsd_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.domain = linux_to_bsd_domain(linux_args.domain); + if (bsd_args.domain == -1) + return (EINVAL); + + bsd_args.type = linux_args.type; + bsd_args.protocol = linux_args.protocol; + bsd_args.rsv = linux_args.rsv; + return (socketpair(p, &bsd_args)); +} + +struct linux_send_args { + int s; + void *msg; + int len; + int flags; +}; + +static int +linux_send(struct proc *p, struct linux_send_args *args) +{ + struct linux_send_args linux_args; + struct osend_args /* { + int s; + caddr_t buf; + int len; + int flags; + } */ bsd_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.s = linux_args.s; + bsd_args.buf = linux_args.msg; + bsd_args.len = linux_args.len; + bsd_args.flags = linux_args.flags; + return (osend(p, &bsd_args)); +} + +struct linux_recv_args { + int s; + void *msg; + int len; + int flags; +}; + +static int +linux_recv(struct proc *p, struct linux_recv_args *args) +{ + struct linux_recv_args linux_args; + struct orecv_args /* { + int s; + caddr_t buf; + int len; + int flags; + } */ bsd_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.s = linux_args.s; + bsd_args.buf = linux_args.msg; + bsd_args.len = linux_args.len; + bsd_args.flags = linux_args.flags; + return (orecv(p, &bsd_args)); +} + +struct linux_sendto_args { + int s; + void *msg; + int len; + int flags; + caddr_t to; + int tolen; +}; + +static int +linux_sendto(struct proc *p, struct linux_sendto_args *args) +{ + struct linux_sendto_args linux_args; + struct sendto_args /* { + int s; + caddr_t buf; + size_t len; + int flags; + caddr_t to; + int tolen; + } */ bsd_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.s = linux_args.s; + bsd_args.buf = linux_args.msg; + bsd_args.len = linux_args.len; + bsd_args.flags = linux_args.flags; + bsd_args.to = linux_args.to; + bsd_args.tolen = linux_args.tolen; + + if (linux_check_hdrincl(p, linux_args.s) == 0) + /* IP_HDRINCL set, tweak the packet before sending */ + return (linux_sendto_hdrincl(p, &bsd_args)); + + return (sendto(p, &bsd_args)); +} + +struct linux_recvfrom_args { + int s; + void *buf; + int len; + int flags; + caddr_t from; + int *fromlen; +}; + +static int +linux_recvfrom(struct proc *p, struct linux_recvfrom_args *args) +{ + struct linux_recvfrom_args linux_args; + struct recvfrom_args /* { + int s; + caddr_t buf; + size_t len; + int flags; + caddr_t from; + int *fromlenaddr; + } */ bsd_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.s = linux_args.s; + bsd_args.buf = linux_args.buf; + bsd_args.len = linux_args.len; + bsd_args.flags = linux_args.flags; + bsd_args.from = linux_args.from; + bsd_args.fromlenaddr = linux_args.fromlen; + return (orecvfrom(p, &bsd_args)); +} + +struct linux_shutdown_args { + int s; + int how; +}; + +static int +linux_shutdown(struct proc *p, struct linux_shutdown_args *args) +{ + struct linux_shutdown_args linux_args; + struct shutdown_args /* { + int s; + int how; + } */ bsd_args; + int error; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.s = linux_args.s; + bsd_args.how = linux_args.how; + return (shutdown(p, &bsd_args)); +} + +struct linux_setsockopt_args { + int s; + int level; + int optname; + void *optval; + int optlen; +}; + +static int +linux_setsockopt(struct proc *p, struct linux_setsockopt_args *args) +{ + struct linux_setsockopt_args linux_args; + struct setsockopt_args /* { + int s; + int level; + int name; + caddr_t val; + int valsize; + } */ bsd_args; + int error, name; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.s = linux_args.s; + bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level); + switch (bsd_args.level) { + case SOL_SOCKET: + name = linux_to_bsd_so_sockopt(linux_args.optname); + break; + case IPPROTO_IP: + name = linux_to_bsd_ip_sockopt(linux_args.optname); + break; + case IPPROTO_TCP: + /* Linux TCP option values match BSD's */ + name = linux_args.optname; + break; + default: + name = -1; + break; + } + if (name == -1) + return (EINVAL); + + bsd_args.name = name; + bsd_args.val = linux_args.optval; + bsd_args.valsize = linux_args.optlen; + return (setsockopt(p, &bsd_args)); +} + +struct linux_getsockopt_args { + int s; + int level; + int optname; + void *optval; + int *optlen; +}; + +static int +linux_getsockopt(struct proc *p, struct linux_getsockopt_args *args) +{ + struct linux_getsockopt_args linux_args; + struct getsockopt_args /* { + int s; + int level; + int name; + caddr_t val; + int *avalsize; + } */ bsd_args; + int error, name; + + if ((error = copyin(args, &linux_args, sizeof(linux_args)))) + return (error); + + bsd_args.s = linux_args.s; + bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level); + switch (bsd_args.level) { + case SOL_SOCKET: + name = linux_to_bsd_so_sockopt(linux_args.optname); + break; + case IPPROTO_IP: + name = linux_to_bsd_ip_sockopt(linux_args.optname); + break; + case IPPROTO_TCP: + /* Linux TCP option values match BSD's */ + name = linux_args.optname; + break; + default: + name = -1; + break; + } + if (name == -1) + return (EINVAL); + + bsd_args.name = name; + bsd_args.val = linux_args.optval; + bsd_args.avalsize = linux_args.optlen; + return (getsockopt(p, &bsd_args)); +} + +int +linux_socketcall(struct proc *p, struct linux_socketcall_args *args) +{ + + switch (args->what) { + case LINUX_SOCKET: + return (linux_socket(p, args->args)); + case LINUX_BIND: + return (linux_bind(p, args->args)); + case LINUX_CONNECT: + return (linux_connect(p, args->args)); + case LINUX_LISTEN: + return (linux_listen(p, args->args)); + case LINUX_ACCEPT: + return (linux_accept(p, args->args)); + case LINUX_GETSOCKNAME: + return (linux_getsockname(p, args->args)); + case LINUX_GETPEERNAME: + return (linux_getpeername(p, args->args)); + case LINUX_SOCKETPAIR: + return (linux_socketpair(p, args->args)); + case LINUX_SEND: + return (linux_send(p, args->args)); + case LINUX_RECV: + return (linux_recv(p, args->args)); + case LINUX_SENDTO: + return (linux_sendto(p, args->args)); + case LINUX_RECVFROM: + return (linux_recvfrom(p, args->args)); + case LINUX_SHUTDOWN: + return (linux_shutdown(p, args->args)); + case LINUX_SETSOCKOPT: + return (linux_setsockopt(p, args->args)); + case LINUX_GETSOCKOPT: + return (linux_getsockopt(p, args->args)); + case LINUX_SENDMSG: + do { + int error; + int level; + caddr_t control; + struct { + int s; + const struct msghdr *msg; + int flags; + } *uap = args->args; + + error = copyin(&uap->msg->msg_control, &control, + sizeof(caddr_t)); + if (error) + return (error); + + if (control == NULL) + goto done; + + error = copyin(&((struct cmsghdr*)control)->cmsg_level, + &level, sizeof(int)); + if (error) + return (error); + + if (level == 1) { + /* + * Linux thinks that SOL_SOCKET is 1; we know + * that it's really 0xffff, of course. + */ + level = SOL_SOCKET; + error = copyout(&level, + &((struct cmsghdr *)control)->cmsg_level, + sizeof(int)); + if (error) + return (error); + } + done: + return (sendmsg(p, args->args)); + } while (0); + case LINUX_RECVMSG: + return (recvmsg(p, args->args)); + } + + uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what); + return (ENOSYS); +} |
