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