Don't leak file descriptors
[qemu.git] / osdep.c
1 /*
2 * QEMU low level functions
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #ifdef CONFIG_SOLARIS
32 #include <sys/types.h>
33 #include <sys/statvfs.h>
34 #endif
35
36 /* Needed early for CONFIG_BSD etc. */
37 #include "config-host.h"
38
39 #ifdef _WIN32
40 #include <windows.h>
41 #elif defined(CONFIG_BSD)
42 #include <stdlib.h>
43 #else
44 #include <malloc.h>
45 #endif
46
47 #include "qemu-common.h"
48 #include "sysemu.h"
49 #include "qemu_socket.h"
50
51 #if !defined(_POSIX_C_SOURCE) || defined(_WIN32) || defined(__sun__)
52 static void *oom_check(void *ptr)
53 {
54 if (ptr == NULL) {
55 abort();
56 }
57 return ptr;
58 }
59 #endif
60
61 #if defined(_WIN32)
62 void *qemu_memalign(size_t alignment, size_t size)
63 {
64 if (!size) {
65 abort();
66 }
67 return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
68 }
69
70 void *qemu_vmalloc(size_t size)
71 {
72 /* FIXME: this is not exactly optimal solution since VirtualAlloc
73 has 64Kb granularity, but at least it guarantees us that the
74 memory is page aligned. */
75 if (!size) {
76 abort();
77 }
78 return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
79 }
80
81 void qemu_vfree(void *ptr)
82 {
83 VirtualFree(ptr, 0, MEM_RELEASE);
84 }
85
86 #else
87
88 void *qemu_memalign(size_t alignment, size_t size)
89 {
90 #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
91 int ret;
92 void *ptr;
93 ret = posix_memalign(&ptr, alignment, size);
94 if (ret != 0)
95 abort();
96 return ptr;
97 #elif defined(CONFIG_BSD)
98 return oom_check(valloc(size));
99 #else
100 return oom_check(memalign(alignment, size));
101 #endif
102 }
103
104 /* alloc shared memory pages */
105 void *qemu_vmalloc(size_t size)
106 {
107 return qemu_memalign(getpagesize(), size);
108 }
109
110 void qemu_vfree(void *ptr)
111 {
112 free(ptr);
113 }
114
115 #endif
116
117 int qemu_create_pidfile(const char *filename)
118 {
119 char buffer[128];
120 int len;
121 #ifndef _WIN32
122 int fd;
123
124 fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
125 if (fd == -1)
126 return -1;
127
128 if (lockf(fd, F_TLOCK, 0) == -1)
129 return -1;
130
131 len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
132 if (write(fd, buffer, len) != len)
133 return -1;
134 #else
135 HANDLE file;
136 DWORD flags;
137 OVERLAPPED overlap;
138 BOOL ret;
139
140 /* Open for writing with no sharing. */
141 file = CreateFile(filename, GENERIC_WRITE, 0, NULL,
142 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
143
144 if (file == INVALID_HANDLE_VALUE)
145 return -1;
146
147 flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
148 overlap.hEvent = 0;
149 /* Lock 1 byte. */
150 ret = LockFileEx(file, flags, 0, 0, 1, &overlap);
151 if (ret == 0)
152 return -1;
153
154 /* Write PID to file. */
155 len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
156 ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
157 &overlap, NULL);
158 if (ret == 0)
159 return -1;
160 #endif
161 return 0;
162 }
163
164 #ifdef _WIN32
165
166 /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
167 #define _W32_FT_OFFSET (116444736000000000ULL)
168
169 int qemu_gettimeofday(qemu_timeval *tp)
170 {
171 union {
172 unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
173 FILETIME ft;
174 } _now;
175
176 if(tp)
177 {
178 GetSystemTimeAsFileTime (&_now.ft);
179 tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL );
180 tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL);
181 }
182 /* Always return 0 as per Open Group Base Specifications Issue 6.
183 Do not set errno on error. */
184 return 0;
185 }
186 #endif /* _WIN32 */
187
188
189 #ifdef _WIN32
190 void socket_set_nonblock(int fd)
191 {
192 unsigned long opt = 1;
193 ioctlsocket(fd, FIONBIO, &opt);
194 }
195
196 int inet_aton(const char *cp, struct in_addr *ia)
197 {
198 uint32_t addr = inet_addr(cp);
199 if (addr == 0xffffffff)
200 return 0;
201 ia->s_addr = addr;
202 return 1;
203 }
204
205 void qemu_set_cloexec(int fd)
206 {
207 }
208
209 #else
210
211 void socket_set_nonblock(int fd)
212 {
213 int f;
214 f = fcntl(fd, F_GETFL);
215 fcntl(fd, F_SETFL, f | O_NONBLOCK);
216 }
217
218 void qemu_set_cloexec(int fd)
219 {
220 int f;
221 f = fcntl(fd, F_GETFD);
222 fcntl(fd, F_SETFD, f | FD_CLOEXEC);
223 }
224
225 #endif
226
227 /*
228 * Opens a file with FD_CLOEXEC set
229 */
230 int qemu_open(const char *name, int flags, ...)
231 {
232 int ret;
233 int mode = 0;
234
235 if (flags & O_CREAT) {
236 va_list ap;
237
238 va_start(ap, flags);
239 mode = va_arg(ap, int);
240 va_end(ap);
241 }
242
243 #ifdef O_CLOEXEC
244 ret = open(name, flags | O_CLOEXEC, mode);
245 #else
246 ret = open(name, flags, mode);
247 if (ret >= 0) {
248 qemu_set_cloexec(ret);
249 }
250 #endif
251
252 return ret;
253 }
254
255 #ifndef _WIN32
256 /*
257 * Creates a pipe with FD_CLOEXEC set on both file descriptors
258 */
259 int qemu_pipe(int pipefd[2])
260 {
261 int ret;
262
263 #ifdef CONFIG_PIPE2
264 ret = pipe2(pipefd, O_CLOEXEC);
265 #else
266 ret = pipe(pipefd);
267 if (ret == 0) {
268 qemu_set_cloexec(pipefd[0]);
269 qemu_set_cloexec(pipefd[1]);
270 }
271 #endif
272
273 return ret;
274 }
275 #endif
276
277 /*
278 * Opens a socket with FD_CLOEXEC set
279 */
280 int qemu_socket(int domain, int type, int protocol)
281 {
282 int ret;
283
284 #ifdef SOCK_CLOEXEC
285 ret = socket(domain, type | SOCK_CLOEXEC, protocol);
286 #else
287 ret = socket(domain, type, protocol);
288 if (ret >= 0) {
289 qemu_set_cloexec(ret);
290 }
291 #endif
292
293 return ret;
294 }
295
296 /*
297 * Accept a connection and set FD_CLOEXEC
298 */
299 int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
300 {
301 int ret;
302
303 #ifdef CONFIG_ACCEPT4
304 ret = accept4(s, addr, addrlen, SOCK_CLOEXEC);
305 #else
306 ret = accept(s, addr, addrlen);
307 if (ret >= 0) {
308 qemu_set_cloexec(ret);
309 }
310 #endif
311
312 return ret;
313 }