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