1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25
26
27
28#ifdef CONFIG_SOLARIS
29#include <sys/statvfs.h>
30
31
32extern int madvise(caddr_t, size_t, int);
33#endif
34
35#include "qemu-common.h"
36#include "qemu/cutils.h"
37#include "qemu/sockets.h"
38#include "qemu/error-report.h"
39#include "monitor/monitor.h"
40
41static bool fips_enabled = false;
42
43static const char *hw_version = QEMU_HW_VERSION;
44
45int socket_set_cork(int fd, int v)
46{
47#if defined(SOL_TCP) && defined(TCP_CORK)
48 return qemu_setsockopt(fd, SOL_TCP, TCP_CORK, &v, sizeof(v));
49#else
50 return 0;
51#endif
52}
53
54int socket_set_nodelay(int fd)
55{
56 int v = 1;
57 return qemu_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
58}
59
60int qemu_madvise(void *addr, size_t len, int advice)
61{
62 if (advice == QEMU_MADV_INVALID) {
63 errno = EINVAL;
64 return -1;
65 }
66#if defined(CONFIG_MADVISE)
67 return madvise(addr, len, advice);
68#elif defined(CONFIG_POSIX_MADVISE)
69 return posix_madvise(addr, len, advice);
70#else
71 errno = EINVAL;
72 return -1;
73#endif
74}
75
76#ifndef _WIN32
77
78
79
80static int qemu_dup_flags(int fd, int flags)
81{
82 int ret;
83 int serrno;
84 int dup_flags;
85
86 ret = qemu_dup(fd);
87 if (ret == -1) {
88 goto fail;
89 }
90
91 dup_flags = fcntl(ret, F_GETFL);
92 if (dup_flags == -1) {
93 goto fail;
94 }
95
96 if ((flags & O_SYNC) != (dup_flags & O_SYNC)) {
97 errno = EINVAL;
98 goto fail;
99 }
100
101
102 if (fcntl(ret, F_SETFL, flags) == -1) {
103 goto fail;
104 }
105
106
107 if (flags & O_TRUNC ||
108 ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) {
109 if (ftruncate(ret, 0) == -1) {
110 goto fail;
111 }
112 }
113
114 return ret;
115
116fail:
117 serrno = errno;
118 if (ret != -1) {
119 close(ret);
120 }
121 errno = serrno;
122 return -1;
123}
124
125int qemu_dup(int fd)
126{
127 int ret;
128#ifdef F_DUPFD_CLOEXEC
129 ret = fcntl(fd, F_DUPFD_CLOEXEC, 0);
130#else
131 ret = dup(fd);
132 if (ret != -1) {
133 qemu_set_cloexec(ret);
134 }
135#endif
136 return ret;
137}
138
139static int qemu_parse_fdset(const char *param)
140{
141 return qemu_parse_fd(param);
142}
143#endif
144
145
146
147
148int qemu_open(const char *name, int flags, ...)
149{
150 int ret;
151 int mode = 0;
152
153#ifndef _WIN32
154 const char *fdset_id_str;
155
156
157 if (strstart(name, "/dev/fdset/", &fdset_id_str)) {
158 int64_t fdset_id;
159 int fd, dupfd;
160
161 fdset_id = qemu_parse_fdset(fdset_id_str);
162 if (fdset_id == -1) {
163 errno = EINVAL;
164 return -1;
165 }
166
167 fd = monitor_fdset_get_fd(fdset_id, flags);
168 if (fd == -1) {
169 return -1;
170 }
171
172 dupfd = qemu_dup_flags(fd, flags);
173 if (dupfd == -1) {
174 return -1;
175 }
176
177 ret = monitor_fdset_dup_fd_add(fdset_id, dupfd);
178 if (ret == -1) {
179 close(dupfd);
180 errno = EINVAL;
181 return -1;
182 }
183
184 return dupfd;
185 }
186#endif
187
188 if (flags & O_CREAT) {
189 va_list ap;
190
191 va_start(ap, flags);
192 mode = va_arg(ap, int);
193 va_end(ap);
194 }
195
196#ifdef O_CLOEXEC
197 ret = open(name, flags | O_CLOEXEC, mode);
198#else
199 ret = open(name, flags, mode);
200 if (ret >= 0) {
201 qemu_set_cloexec(ret);
202 }
203#endif
204
205#ifdef O_DIRECT
206 if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {
207 error_report("file system may not support O_DIRECT");
208 errno = EINVAL;
209 }
210#endif
211
212 return ret;
213}
214
215int qemu_close(int fd)
216{
217 int64_t fdset_id;
218
219
220 fdset_id = monitor_fdset_dup_fd_find(fd);
221 if (fdset_id != -1) {
222 int ret;
223
224 ret = close(fd);
225 if (ret == 0) {
226 monitor_fdset_dup_fd_remove(fd);
227 }
228
229 return ret;
230 }
231
232 return close(fd);
233}
234
235
236
237
238
239
240
241
242
243
244
245
246ssize_t qemu_write_full(int fd, const void *buf, size_t count)
247{
248 ssize_t ret = 0;
249 ssize_t total = 0;
250
251 while (count) {
252 ret = write(fd, buf, count);
253 if (ret < 0) {
254 if (errno == EINTR)
255 continue;
256 break;
257 }
258
259 count -= ret;
260 buf += ret;
261 total += ret;
262 }
263
264 return total;
265}
266
267
268
269
270int qemu_socket(int domain, int type, int protocol)
271{
272 int ret;
273
274#ifdef SOCK_CLOEXEC
275 ret = socket(domain, type | SOCK_CLOEXEC, protocol);
276 if (ret != -1 || errno != EINVAL) {
277 return ret;
278 }
279#endif
280 ret = socket(domain, type, protocol);
281 if (ret >= 0) {
282 qemu_set_cloexec(ret);
283 }
284
285 return ret;
286}
287
288
289
290
291int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
292{
293 int ret;
294
295#ifdef CONFIG_ACCEPT4
296 ret = accept4(s, addr, addrlen, SOCK_CLOEXEC);
297 if (ret != -1 || errno != ENOSYS) {
298 return ret;
299 }
300#endif
301 ret = accept(s, addr, addrlen);
302 if (ret >= 0) {
303 qemu_set_cloexec(ret);
304 }
305
306 return ret;
307}
308
309void qemu_set_hw_version(const char *version)
310{
311 hw_version = version;
312}
313
314const char *qemu_hw_version(void)
315{
316 return hw_version;
317}
318
319void fips_set_state(bool requested)
320{
321#ifdef __linux__
322 if (requested) {
323 FILE *fds = fopen("/proc/sys/crypto/fips_enabled", "r");
324 if (fds != NULL) {
325 fips_enabled = (fgetc(fds) == '1');
326 fclose(fds);
327 }
328 }
329#else
330 fips_enabled = false;
331#endif
332
333#ifdef _FIPS_DEBUG
334 fprintf(stderr, "FIPS mode %s (requested %s)\n",
335 (fips_enabled ? "enabled" : "disabled"),
336 (requested ? "enabled" : "disabled"));
337#endif
338}
339
340bool fips_get_state(void)
341{
342 return fips_enabled;
343}
344
345#ifdef _WIN32
346static void socket_cleanup(void)
347{
348 WSACleanup();
349}
350#endif
351
352int socket_init(void)
353{
354#ifdef _WIN32
355 WSADATA Data;
356 int ret, err;
357
358 ret = WSAStartup(MAKEWORD(2, 2), &Data);
359 if (ret != 0) {
360 err = WSAGetLastError();
361 fprintf(stderr, "WSAStartup: %d\n", err);
362 return -1;
363 }
364 atexit(socket_cleanup);
365#endif
366 return 0;
367}
368
369#if !GLIB_CHECK_VERSION(2, 31, 0)
370
371
372
373
374
375
376static void __attribute__((constructor)) thread_init(void)
377{
378 if (!g_thread_supported()) {
379 g_thread_init(NULL);
380 }
381}
382#endif
383
384#ifndef CONFIG_IOVEC
385
386static ssize_t
387readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
388{
389 unsigned i = 0;
390 ssize_t ret = 0;
391 while (i < iov_cnt) {
392 ssize_t r = do_write
393 ? write(fd, iov[i].iov_base, iov[i].iov_len)
394 : read(fd, iov[i].iov_base, iov[i].iov_len);
395 if (r > 0) {
396 ret += r;
397 } else if (!r) {
398 break;
399 } else if (errno == EINTR) {
400 continue;
401 } else {
402
403
404 if (ret == 0) {
405 ret = -1;
406 }
407 break;
408 }
409 i++;
410 }
411 return ret;
412}
413
414ssize_t
415readv(int fd, const struct iovec *iov, int iov_cnt)
416{
417 return readv_writev(fd, iov, iov_cnt, false);
418}
419
420ssize_t
421writev(int fd, const struct iovec *iov, int iov_cnt)
422{
423 return readv_writev(fd, iov, iov_cnt, true);
424}
425#endif
426