1 1.1 mrg /* $NetBSD: netbsd32_fd.c,v 1.1 2018/12/24 21:27:05 mrg Exp $ */ 2 1.1 mrg 3 1.1 mrg /* 4 1.1 mrg * Copyright (c) 1998, 2001, 2008, 2018 Matthew R. Green 5 1.1 mrg * All rights reserved. 6 1.1 mrg * 7 1.1 mrg * Redistribution and use in source and binary forms, with or without 8 1.1 mrg * modification, are permitted provided that the following conditions 9 1.1 mrg * are met: 10 1.1 mrg * 1. Redistributions of source code must retain the above copyright 11 1.1 mrg * notice, this list of conditions and the following disclaimer. 12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 mrg * notice, this list of conditions and the following disclaimer in the 14 1.1 mrg * documentation and/or other materials provided with the distribution. 15 1.1 mrg * 16 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 mrg * SUCH DAMAGE. 27 1.1 mrg * 28 1.1 mrg * from: NetBSD: netbsd32_netbsd.c,v 1.221 2018/12/24 20:44:39 mrg Exp 29 1.1 mrg */ 30 1.1 mrg 31 1.1 mrg #include <sys/cdefs.h> 32 1.1 mrg __KERNEL_RCSID(0, "$NetBSD: netbsd32_fd.c,v 1.1 2018/12/24 21:27:05 mrg Exp $"); 33 1.1 mrg 34 1.1 mrg #include <sys/param.h> 35 1.1 mrg #include <sys/systm.h> 36 1.1 mrg #include <sys/filedesc.h> 37 1.1 mrg #include <sys/namei.h> 38 1.1 mrg #include <sys/vnode.h> 39 1.1 mrg #include <sys/vfs_syscalls.h> 40 1.1 mrg #include <sys/kauth.h> 41 1.1 mrg 42 1.1 mrg #include <compat/netbsd32/netbsd32.h> 43 1.1 mrg #include <compat/netbsd32/netbsd32_syscall.h> 44 1.1 mrg #include <compat/netbsd32/netbsd32_syscallargs.h> 45 1.1 mrg #include <compat/netbsd32/netbsd32_conv.h> 46 1.1 mrg 47 1.1 mrg 48 1.1 mrg int 49 1.1 mrg netbsd32___getfh30(struct lwp *l, const struct netbsd32___getfh30_args *uap, register_t *retval) 50 1.1 mrg { 51 1.1 mrg /* { 52 1.1 mrg syscallarg(const netbsd32_charp) fname; 53 1.1 mrg syscallarg(netbsd32_fhandlep_t) fhp; 54 1.1 mrg syscallarg(netbsd32_size_tp) fh_size; 55 1.1 mrg } */ 56 1.1 mrg struct vnode *vp; 57 1.1 mrg fhandle_t *fh; 58 1.1 mrg int error; 59 1.1 mrg struct pathbuf *pb; 60 1.1 mrg struct nameidata nd; 61 1.1 mrg netbsd32_size_t usz32, sz32; 62 1.1 mrg size_t sz; 63 1.1 mrg 64 1.1 mrg /* 65 1.1 mrg * Must be super user 66 1.1 mrg */ 67 1.1 mrg error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE, 68 1.1 mrg 0, NULL, NULL, NULL); 69 1.1 mrg if (error) 70 1.1 mrg return error; 71 1.1 mrg 72 1.1 mrg error = pathbuf_copyin(SCARG_P32(uap, fname), &pb); 73 1.1 mrg if (error) { 74 1.1 mrg return error; 75 1.1 mrg } 76 1.1 mrg 77 1.1 mrg NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb); 78 1.1 mrg error = namei(&nd); 79 1.1 mrg if (error) { 80 1.1 mrg pathbuf_destroy(pb); 81 1.1 mrg return error; 82 1.1 mrg } 83 1.1 mrg vp = nd.ni_vp; 84 1.1 mrg pathbuf_destroy(pb); 85 1.1 mrg 86 1.1 mrg error = vfs_composefh_alloc(vp, &fh); 87 1.1 mrg vput(vp); 88 1.1 mrg if (error != 0) { 89 1.1 mrg return error; 90 1.1 mrg } 91 1.1 mrg error = copyin(SCARG_P32(uap, fh_size), &usz32, sizeof(usz32)); 92 1.1 mrg if (error != 0) { 93 1.1 mrg goto out; 94 1.1 mrg } 95 1.1 mrg sz = FHANDLE_SIZE(fh); 96 1.1 mrg sz32 = sz; 97 1.1 mrg 98 1.1 mrg error = copyout(&sz32, SCARG_P32(uap, fh_size), sizeof(sz32)); 99 1.1 mrg if (error != 0) { 100 1.1 mrg goto out; 101 1.1 mrg } 102 1.1 mrg if (usz32 >= sz32) { 103 1.1 mrg error = copyout(fh, SCARG_P32(uap, fhp), sz); 104 1.1 mrg } else { 105 1.1 mrg error = E2BIG; 106 1.1 mrg } 107 1.1 mrg out: 108 1.1 mrg vfs_composefh_free(fh); 109 1.1 mrg return error; 110 1.1 mrg } 111 1.1 mrg 112 1.1 mrg int 113 1.1 mrg netbsd32_pipe2(struct lwp *l, const struct netbsd32_pipe2_args *uap, 114 1.1 mrg register_t *retval) 115 1.1 mrg { 116 1.1 mrg /* { 117 1.1 mrg syscallarg(netbsd32_intp) fildes; 118 1.1 mrg syscallarg(int) flags; 119 1.1 mrg } */ 120 1.1 mrg int fd[2], error; 121 1.1 mrg 122 1.1 mrg error = pipe1(l, fd, SCARG(uap, flags)); 123 1.1 mrg if (error != 0) 124 1.1 mrg return error; 125 1.1 mrg 126 1.1 mrg error = copyout(fd, SCARG_P32(uap, fildes), sizeof(fd)); 127 1.1 mrg if (error != 0) 128 1.1 mrg return error; 129 1.1 mrg 130 1.1 mrg retval[0] = 0; 131 1.1 mrg return 0; 132 1.1 mrg } 133