]> git.proxmox.com Git - qemu.git/blame - osdep.c
block: Convert close calls to qemu_close
[qemu.git] / osdep.c
CommitLineData
ea88812f
FB
1/*
2 * QEMU low level functions
5fafdf24 3 *
ea88812f 4 * Copyright (c) 2003 Fabrice Bellard
5fafdf24 5 *
ea88812f
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include <stdlib.h>
25#include <stdio.h>
26#include <stdarg.h>
0f66998f 27#include <stdbool.h>
ea88812f 28#include <string.h>
ea88812f
FB
29#include <errno.h>
30#include <unistd.h>
aa26bb2d 31#include <fcntl.h>
f582af58
PB
32
33/* Needed early for CONFIG_BSD etc. */
34#include "config-host.h"
35
e78815a5
AF
36#if defined(CONFIG_MADVISE) || defined(CONFIG_POSIX_MADVISE)
37#include <sys/mman.h>
38#endif
39
dfe5fff3 40#ifdef CONFIG_SOLARIS
605686cd
TS
41#include <sys/types.h>
42#include <sys/statvfs.h>
e78815a5
AF
43/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
44 discussion about Solaris header problems */
45extern int madvise(caddr_t, size_t, int);
605686cd 46#endif
ea88812f 47
511d2b14 48#include "qemu-common.h"
cd245a19 49#include "trace.h"
03ff3ca3
AL
50#include "qemu_socket.h"
51
0f66998f
PM
52static bool fips_enabled = false;
53
93bfef4c
CV
54static const char *qemu_version = QEMU_VERSION;
55
128aa589
PB
56int socket_set_cork(int fd, int v)
57{
58#if defined(SOL_TCP) && defined(TCP_CORK)
59 return setsockopt(fd, SOL_TCP, TCP_CORK, &v, sizeof(v));
60#else
61 return 0;
62#endif
63}
64
e78815a5
AF
65int qemu_madvise(void *addr, size_t len, int advice)
66{
67 if (advice == QEMU_MADV_INVALID) {
68 errno = EINVAL;
69 return -1;
70 }
71#if defined(CONFIG_MADVISE)
72 return madvise(addr, len, advice);
73#elif defined(CONFIG_POSIX_MADVISE)
74 return posix_madvise(addr, len, advice);
75#else
76 errno = EINVAL;
77 return -1;
78#endif
79}
80
03ff3ca3 81
40ff6d7e
KW
82/*
83 * Opens a file with FD_CLOEXEC set
84 */
85int qemu_open(const char *name, int flags, ...)
86{
87 int ret;
88 int mode = 0;
89
90 if (flags & O_CREAT) {
91 va_list ap;
92
93 va_start(ap, flags);
94 mode = va_arg(ap, int);
95 va_end(ap);
96 }
97
98#ifdef O_CLOEXEC
99 ret = open(name, flags | O_CLOEXEC, mode);
100#else
101 ret = open(name, flags, mode);
102 if (ret >= 0) {
103 qemu_set_cloexec(ret);
104 }
03ff3ca3 105#endif
40ff6d7e
KW
106
107 return ret;
108}
109
2e1e79da
CB
110int qemu_close(int fd)
111{
112 return close(fd);
113}
114
7b5f699d
KS
115/*
116 * A variant of write(2) which handles partial write.
117 *
118 * Return the number of bytes transferred.
119 * Set errno if fewer than `count' bytes are written.
1298cb68
JQ
120 *
121 * This function don't work with non-blocking fd's.
122 * Any of the possibilities with non-bloking fd's is bad:
123 * - return a short write (then name is wrong)
124 * - busy wait adding (errno == EAGAIN) to the loop
7b5f699d
KS
125 */
126ssize_t qemu_write_full(int fd, const void *buf, size_t count)
127{
128 ssize_t ret = 0;
129 ssize_t total = 0;
130
131 while (count) {
132 ret = write(fd, buf, count);
133 if (ret < 0) {
134 if (errno == EINTR)
135 continue;
136 break;
137 }
138
139 count -= ret;
140 buf += ret;
141 total += ret;
142 }
143
144 return total;
145}
146
40ff6d7e
KW
147/*
148 * Opens a socket with FD_CLOEXEC set
149 */
150int qemu_socket(int domain, int type, int protocol)
151{
152 int ret;
153
154#ifdef SOCK_CLOEXEC
155 ret = socket(domain, type | SOCK_CLOEXEC, protocol);
3a03bfa5
AP
156 if (ret != -1 || errno != EINVAL) {
157 return ret;
158 }
159#endif
40ff6d7e
KW
160 ret = socket(domain, type, protocol);
161 if (ret >= 0) {
162 qemu_set_cloexec(ret);
163 }
40ff6d7e
KW
164
165 return ret;
166}
167
168/*
169 * Accept a connection and set FD_CLOEXEC
170 */
171int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
172{
173 int ret;
174
175#ifdef CONFIG_ACCEPT4
176 ret = accept4(s, addr, addrlen, SOCK_CLOEXEC);
347ed55c 177 if (ret != -1 || errno != ENOSYS) {
3a03bfa5
AP
178 return ret;
179 }
180#endif
40ff6d7e
KW
181 ret = accept(s, addr, addrlen);
182 if (ret >= 0) {
183 qemu_set_cloexec(ret);
184 }
40ff6d7e
KW
185
186 return ret;
187}
993295fe
PB
188
189/*
190 * A variant of send(2) which handles partial write.
191 *
192 * Return the number of bytes transferred, which is only
193 * smaller than `count' if there is an error.
194 *
195 * This function won't work with non-blocking fd's.
196 * Any of the possibilities with non-bloking fd's is bad:
197 * - return a short write (then name is wrong)
198 * - busy wait adding (errno == EAGAIN) to the loop
199 */
200ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
201{
202 ssize_t ret = 0;
203 ssize_t total = 0;
204
205 while (count) {
206 ret = send(fd, buf, count, flags);
207 if (ret < 0) {
208 if (errno == EINTR) {
209 continue;
210 }
211 break;
212 }
213
214 count -= ret;
215 buf += ret;
216 total += ret;
217 }
218
219 return total;
220}
221
222/*
223 * A variant of recv(2) which handles partial write.
224 *
225 * Return the number of bytes transferred, which is only
226 * smaller than `count' if there is an error.
227 *
228 * This function won't work with non-blocking fd's.
229 * Any of the possibilities with non-bloking fd's is bad:
230 * - return a short write (then name is wrong)
231 * - busy wait adding (errno == EAGAIN) to the loop
232 */
233ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
234{
235 ssize_t ret = 0;
236 ssize_t total = 0;
237
238 while (count) {
239 ret = qemu_recv(fd, buf, count, flags);
240 if (ret <= 0) {
241 if (ret < 0 && errno == EINTR) {
242 continue;
243 }
244 break;
245 }
246
247 count -= ret;
248 buf += ret;
249 total += ret;
250 }
251
252 return total;
253}
254
93bfef4c
CV
255void qemu_set_version(const char *version)
256{
257 qemu_version = version;
258}
259
260const char *qemu_get_version(void)
261{
262 return qemu_version;
263}
0f66998f
PM
264
265void fips_set_state(bool requested)
266{
267#ifdef __linux__
268 if (requested) {
269 FILE *fds = fopen("/proc/sys/crypto/fips_enabled", "r");
270 if (fds != NULL) {
271 fips_enabled = (fgetc(fds) == '1');
272 fclose(fds);
273 }
274 }
275#else
276 fips_enabled = false;
277#endif /* __linux__ */
278
279#ifdef _FIPS_DEBUG
280 fprintf(stderr, "FIPS mode %s (requested %s)\n",
281 (fips_enabled ? "enabled" : "disabled"),
282 (requested ? "enabled" : "disabled"));
283#endif
284}
285
286bool fips_get_state(void)
287{
288 return fips_enabled;
289}