]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/bsd-file.h
bsd-user: Implement chdir and fchdir
[mirror_qemu.git] / bsd-user / bsd-file.h
CommitLineData
c5c84d16
WL
1/*
2 * file related system call shims and definitions
3 *
4 * Copyright (c) 2013 Stacey D. Son
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
9c092804
MA
20#ifndef BSD_FILE_H
21#define BSD_FILE_H
c5c84d16
WL
22
23#include "qemu/path.h"
24
77d3522b
WL
25#define LOCK_PATH(p, arg) \
26do { \
27 (p) = lock_user_string(arg); \
28 if ((p) == NULL) { \
29 return -TARGET_EFAULT; \
30 } \
31} while (0)
32
33#define UNLOCK_PATH(p, arg) unlock_user(p, arg, 0)
34
35
c5c84d16
WL
36extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,
37 int copy);
38extern void unlock_iovec(struct iovec *vec, abi_ulong target_addr, int count,
39 int copy);
40
77d3522b
WL
41int safe_open(const char *path, int flags, mode_t mode);
42int safe_openat(int fd, const char *path, int flags, mode_t mode);
43
80da1b00
WL
44ssize_t safe_read(int fd, void *buf, size_t nbytes);
45ssize_t safe_pread(int fd, void *buf, size_t nbytes, off_t offset);
46ssize_t safe_readv(int fd, const struct iovec *iov, int iovcnt);
47ssize_t safe_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
48
770d8aba
WL
49ssize_t safe_write(int fd, void *buf, size_t nbytes);
50ssize_t safe_pwrite(int fd, void *buf, size_t nbytes, off_t offset);
51ssize_t safe_writev(int fd, const struct iovec *iov, int iovcnt);
52ssize_t safe_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
53
80da1b00
WL
54/* read(2) */
55static abi_long do_bsd_read(abi_long arg1, abi_long arg2, abi_long arg3)
56{
57 abi_long ret;
58 void *p;
59
60 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
61 if (p == NULL) {
62 return -TARGET_EFAULT;
63 }
64 ret = get_errno(safe_read(arg1, p, arg3));
65 unlock_user(p, arg2, ret);
66
67 return ret;
68}
69
70/* pread(2) */
71static abi_long do_bsd_pread(void *cpu_env, abi_long arg1,
72 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
73{
74 abi_long ret;
75 void *p;
76
77 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
78 if (p == NULL) {
79 return -TARGET_EFAULT;
80 }
81 if (regpairs_aligned(cpu_env) != 0) {
82 arg4 = arg5;
83 arg5 = arg6;
84 }
85 ret = get_errno(safe_pread(arg1, p, arg3, target_arg64(arg4, arg5)));
86 unlock_user(p, arg2, ret);
87
88 return ret;
89}
90
91/* readv(2) */
92static abi_long do_bsd_readv(abi_long arg1, abi_long arg2, abi_long arg3)
93{
94 abi_long ret;
95 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
96
97 if (vec != NULL) {
98 ret = get_errno(safe_readv(arg1, vec, arg3));
99 unlock_iovec(vec, arg2, arg3, 1);
100 } else {
101 ret = -host_to_target_errno(errno);
102 }
103
104 return ret;
105}
106
107/* preadv(2) */
108static abi_long do_bsd_preadv(void *cpu_env, abi_long arg1,
109 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
110{
111 abi_long ret;
112 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 1);
113
114 if (vec != NULL) {
115 if (regpairs_aligned(cpu_env) != 0) {
116 arg4 = arg5;
117 arg5 = arg6;
118 }
119 ret = get_errno(safe_preadv(arg1, vec, arg3, target_arg64(arg4, arg5)));
120 unlock_iovec(vec, arg2, arg3, 0);
121 } else {
122 ret = -host_to_target_errno(errno);
123 }
124
125 return ret;
126}
127
770d8aba
WL
128/* write(2) */
129static abi_long do_bsd_write(abi_long arg1, abi_long arg2, abi_long arg3)
130{
131 abi_long nbytes, ret;
132 void *p;
133
134 /* nbytes < 0 implies that it was larger than SIZE_MAX. */
135 nbytes = arg3;
136 if (nbytes < 0) {
137 return -TARGET_EINVAL;
138 }
139 p = lock_user(VERIFY_READ, arg2, nbytes, 1);
140 if (p == NULL) {
141 return -TARGET_EFAULT;
142 }
143 ret = get_errno(safe_write(arg1, p, arg3));
144 unlock_user(p, arg2, 0);
145
146 return ret;
147}
148
149/* pwrite(2) */
150static abi_long do_bsd_pwrite(void *cpu_env, abi_long arg1,
151 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
152{
153 abi_long ret;
154 void *p;
155
156 p = lock_user(VERIFY_READ, arg2, arg3, 1);
157 if (p == NULL) {
158 return -TARGET_EFAULT;
159 }
160 if (regpairs_aligned(cpu_env) != 0) {
161 arg4 = arg5;
162 arg5 = arg6;
163 }
164 ret = get_errno(safe_pwrite(arg1, p, arg3, target_arg64(arg4, arg5)));
165 unlock_user(p, arg2, 0);
166
167 return ret;
168}
169
170/* writev(2) */
171static abi_long do_bsd_writev(abi_long arg1, abi_long arg2, abi_long arg3)
172{
173 abi_long ret;
174 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
175
176 if (vec != NULL) {
177 ret = get_errno(safe_writev(arg1, vec, arg3));
178 unlock_iovec(vec, arg2, arg3, 0);
179 } else {
180 ret = -host_to_target_errno(errno);
181 }
182
183 return ret;
184}
185
186/* pwritev(2) */
187static abi_long do_bsd_pwritev(void *cpu_env, abi_long arg1,
188 abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
189{
190 abi_long ret;
191 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
192
193 if (vec != NULL) {
194 if (regpairs_aligned(cpu_env) != 0) {
195 arg4 = arg5;
196 arg5 = arg6;
197 }
198 ret = get_errno(safe_pwritev(arg1, vec, arg3, target_arg64(arg4, arg5)));
199 unlock_iovec(vec, arg2, arg3, 0);
200 } else {
201 ret = -host_to_target_errno(errno);
202 }
203
204 return ret;
205}
206
77d3522b
WL
207/* open(2) */
208static abi_long do_bsd_open(abi_long arg1, abi_long arg2, abi_long arg3)
209{
210 abi_long ret;
211 void *p;
212
213 LOCK_PATH(p, arg1);
214 ret = get_errno(safe_open(path(p), target_to_host_bitmask(arg2,
215 fcntl_flags_tbl), arg3));
216 UNLOCK_PATH(p, arg1);
217
218 return ret;
219}
220
221/* openat(2) */
222static abi_long do_bsd_openat(abi_long arg1, abi_long arg2,
223 abi_long arg3, abi_long arg4)
224{
225 abi_long ret;
226 void *p;
227
228 LOCK_PATH(p, arg2);
229 ret = get_errno(safe_openat(arg1, path(p),
230 target_to_host_bitmask(arg3, fcntl_flags_tbl), arg4));
231 UNLOCK_PATH(p, arg2);
232
233 return ret;
234}
235
236/* close(2) */
237static inline abi_long do_bsd_close(abi_long arg1)
238{
239 return get_errno(close(arg1));
240}
241
a2ba6c7b
WL
242/* fdatasync(2) */
243static abi_long do_bsd_fdatasync(abi_long arg1)
244{
245 return get_errno(fdatasync(arg1));
246}
247
248/* fsync(2) */
249static abi_long do_bsd_fsync(abi_long arg1)
250{
251 return get_errno(fsync(arg1));
252}
253
254/* closefrom(2) */
255static abi_long do_bsd_closefrom(abi_long arg1)
256{
257 closefrom(arg1); /* returns void */
258 return get_errno(0);
259}
260
65c6c4c8
WL
261/* revoke(2) */
262static abi_long do_bsd_revoke(abi_long arg1)
263{
264 abi_long ret;
265 void *p;
266
267 LOCK_PATH(p, arg1);
268 ret = get_errno(revoke(p)); /* XXX path(p)? */
269 UNLOCK_PATH(p, arg1);
270
271 return ret;
272}
273
274/* access(2) */
275static abi_long do_bsd_access(abi_long arg1, abi_long arg2)
276{
277 abi_long ret;
278 void *p;
279
280 LOCK_PATH(p, arg1);
281 ret = get_errno(access(path(p), arg2));
282 UNLOCK_PATH(p, arg1);
283
284 return ret;
285}
286
287/* eaccess(2) */
288static abi_long do_bsd_eaccess(abi_long arg1, abi_long arg2)
289{
290 abi_long ret;
291 void *p;
292
293 LOCK_PATH(p, arg1);
294 ret = get_errno(eaccess(path(p), arg2));
295 UNLOCK_PATH(p, arg1);
296
297 return ret;
298}
299
300/* faccessat(2) */
301static abi_long do_bsd_faccessat(abi_long arg1, abi_long arg2,
302 abi_long arg3, abi_long arg4)
303{
304 abi_long ret;
305 void *p;
306
307 LOCK_PATH(p, arg2);
308 ret = get_errno(faccessat(arg1, p, arg3, arg4)); /* XXX path(p)? */
309 UNLOCK_PATH(p, arg2);
310
311 return ret;
312}
313
390f547e
WL
314/* chdir(2) */
315static abi_long do_bsd_chdir(abi_long arg1)
316{
317 abi_long ret;
318 void *p;
319
320 LOCK_PATH(p, arg1);
321 ret = get_errno(chdir(p)); /* XXX path(p)? */
322 UNLOCK_PATH(p, arg1);
323
324 return ret;
325}
326
327/* fchdir(2) */
328static abi_long do_bsd_fchdir(abi_long arg1)
329{
330 return get_errno(fchdir(arg1));
331}
332
9c092804 333#endif /* BSD_FILE_H */