1 /*-
2 * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD: releng/11.2/sys/compat/cloudabi64/cloudabi64_fd.c 316574 2017-04-06 15:10:36Z ed $");
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/limits.h>
32 #include <sys/malloc.h>
33 #include <sys/syscallsubr.h>
34 #include <sys/systm.h>
35 #include <sys/uio.h>
36
37 #include <contrib/cloudabi/cloudabi64_types.h>
38
39 #include <compat/cloudabi64/cloudabi64_proto.h>
40 #include <compat/cloudabi64/cloudabi64_util.h>
41
42 /* Copies in 64-bit iovec structures from userspace. */
43 static int
44 cloudabi64_copyinuio(const cloudabi64_iovec_t *iovp, size_t iovcnt,
45 struct uio **uiop)
46 {
47 cloudabi64_iovec_t iovobj;
48 struct uio *uio;
49 struct iovec *iov;
50 size_t i;
51 int error;
52
53 /* Allocate uio and iovecs. */
54 if (iovcnt > UIO_MAXIOV)
55 return (EINVAL);
56 uio = malloc(sizeof(struct uio) + iovcnt * sizeof(struct iovec),
57 M_IOV, M_WAITOK);
58 iov = (struct iovec *)(uio + 1);
59
60 /* Initialize uio. */
61 uio->uio_iov = iov;
62 uio->uio_iovcnt = iovcnt;
63 uio->uio_segflg = UIO_USERSPACE;
64 uio->uio_offset = -1;
65 uio->uio_resid = 0;
66
67 /* Copy in iovecs. */
68 for (i = 0; i < iovcnt; i++) {
69 error = copyin(&iovp[i], &iovobj, sizeof(iovobj));
70 if (error != 0) {
71 free(uio, M_IOV);
72 return (error);
73 }
74 iov[i].iov_base = TO_PTR(iovobj.buf);
75 iov[i].iov_len = iovobj.buf_len;
76 if (iov[i].iov_len > INT64_MAX - uio->uio_resid) {
77 free(uio, M_IOV);
78 return (EINVAL);
79 }
80 uio->uio_resid += iov[i].iov_len;
81 }
82
83 *uiop = uio;
84 return (0);
85 }
86
87 int
88 cloudabi64_sys_fd_pread(struct thread *td,
89 struct cloudabi64_sys_fd_pread_args *uap)
90 {
91 struct uio *uio;
92 int error;
93
94 error = cloudabi64_copyinuio(uap->iovs, uap->iovs_len, &uio);
95 if (error != 0)
96 return (error);
97 error = kern_preadv(td, uap->fd, uio, uap->offset);
98 free(uio, M_IOV);
99 return (error);
100 }
101
102 int
103 cloudabi64_sys_fd_pwrite(struct thread *td,
104 struct cloudabi64_sys_fd_pwrite_args *uap)
105 {
106 struct uio *uio;
107 int error;
108
109 error = cloudabi64_copyinuio(TO_PTR(uap->iovs), uap->iovs_len, &uio);
110 if (error != 0)
111 return (error);
112 error = kern_pwritev(td, uap->fd, uio, uap->offset);
113 free(uio, M_IOV);
114 return (error);
115 }
116
117 int
118 cloudabi64_sys_fd_read(struct thread *td,
119 struct cloudabi64_sys_fd_read_args *uap)
120 {
121 struct uio *uio;
122 int error;
123
124 error = cloudabi64_copyinuio(uap->iovs, uap->iovs_len, &uio);
125 if (error != 0)
126 return (error);
127 error = kern_readv(td, uap->fd, uio);
128 free(uio, M_IOV);
129 return (error);
130 }
131
132 int
133 cloudabi64_sys_fd_write(struct thread *td,
134 struct cloudabi64_sys_fd_write_args *uap)
135 {
136 struct uio *uio;
137 int error;
138
139 error = cloudabi64_copyinuio(TO_PTR(uap->iovs), uap->iovs_len, &uio);
140 if (error != 0)
141 return (error);
142 error = kern_writev(td, uap->fd, uio);
143 free(uio, M_IOV);
144 return (error);
145 }
Cache object: 98329f6ee739979bd1f10c261a421104
|