sync with latest
[sdk/emulator/qemu.git] / oslib-posix.c
1 /*
2  * os-posix-lib.c
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2010 Red Hat, Inc.
6  *
7  * QEMU library functions on POSIX which are shared between QEMU and
8  * the QEMU tools.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  */
28
29 /* The following block of code temporarily renames the daemon() function so the
30    compiler does not see the warning associated with it in stdlib.h on OSX */
31 #ifdef __APPLE__
32 #define daemon qemu_fake_daemon_function
33 #include <stdlib.h>
34 #undef daemon
35 extern int daemon(int, int);
36 #endif
37
38 #if defined(__linux__) && defined(__x86_64__)
39    /* Use 2 MiB alignment so transparent hugepages can be used by KVM.
40       Valgrind does not support alignments larger than 1 MiB,
41       therefore we need special code which handles running on Valgrind. */
42 #  define QEMU_VMALLOC_ALIGN (512 * 4096)
43 #  define CONFIG_VALGRIND
44 #elif defined(__linux__) && defined(__s390x__)
45    /* Use 1 MiB (segment size) alignment so gmap can be used by KVM. */
46 #  define QEMU_VMALLOC_ALIGN (256 * 4096)
47 #else
48 #  define QEMU_VMALLOC_ALIGN getpagesize()
49 #endif
50
51 #include "config-host.h"
52 #include "sysemu.h"
53 #include "trace.h"
54 #include "qemu_socket.h"
55
56
57 #ifdef CONFIG_MARU
58 #include "../tizen/src/skin/maruskin_client.h"
59 #endif
60
61 #if defined(CONFIG_VALGRIND)
62 static int running_on_valgrind = -1;
63 #else
64 #  define running_on_valgrind 0
65 #endif
66 #ifdef CONFIG_LINUX
67 #include <sys/syscall.h>
68 #endif
69 #ifdef CONFIG_EVENTFD
70 #include <sys/eventfd.h>
71 #endif
72
73 int qemu_get_thread_id(void)
74 {
75 #if defined(__linux__)
76     return syscall(SYS_gettid);
77 #else
78     return getpid();
79 #endif
80 }
81
82 int qemu_daemon(int nochdir, int noclose)
83 {
84     return daemon(nochdir, noclose);
85 }
86
87 void *qemu_oom_check(void *ptr)
88 {
89     if (ptr == NULL) {
90         fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
91
92 #ifdef CONFIG_MARU
93         char _msg[] = "Failed to allocate memory in qemu.";
94         char cmd[JAVA_MAX_COMMAND_LENGTH] = { 0, };
95
96         int len = strlen(JAVA_EXEFILE_PATH) + strlen(JAVA_EXEOPTION) + strlen(JAR_SKINFILE) +
97             strlen(JAVA_SIMPLEMODE_OPTION) + strlen(_msg) + 7;
98         if (len > JAVA_MAX_COMMAND_LENGTH) {
99             len = JAVA_MAX_COMMAND_LENGTH;
100         }
101
102         snprintf(cmd, len, "%s %s %s %s=\"%s\"",
103             JAVA_EXEFILE_PATH, JAVA_EXEOPTION, JAR_SKINFILE, JAVA_SIMPLEMODE_OPTION, _msg);
104         if(system(cmd) == -1) {
105             // TODO: Handle error... 
106         }
107 #endif
108
109         abort();
110     }
111     return ptr;
112 }
113
114 void *qemu_memalign(size_t alignment, size_t size)
115 {
116     void *ptr;
117 #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
118     int ret;
119     ret = posix_memalign(&ptr, alignment, size);
120     if (ret != 0) {
121         fprintf(stderr, "Failed to allocate %zu B: %s\n",
122                 size, strerror(ret));
123         abort();
124     }
125 #elif defined(CONFIG_BSD)
126     ptr = qemu_oom_check(valloc(size));
127 #else
128     ptr = qemu_oom_check(memalign(alignment, size));
129 #endif
130     trace_qemu_memalign(alignment, size, ptr);
131     return ptr;
132 }
133
134 /* conflicts with qemu_vmalloc in bsd-user/mmap.c */
135 #if !defined(CONFIG_BSD_USER)
136 /* alloc shared memory pages */
137 void *qemu_vmalloc(size_t size)
138 {
139     void *ptr;
140     size_t align = QEMU_VMALLOC_ALIGN;
141
142 #if defined(CONFIG_VALGRIND)
143     if (running_on_valgrind < 0) {
144         /* First call, test whether we are running on Valgrind.
145            This is a substitute for RUNNING_ON_VALGRIND from valgrind.h. */
146         const char *ld = getenv("LD_PRELOAD");
147         running_on_valgrind = (ld != NULL && strstr(ld, "vgpreload"));
148     }
149 #endif
150
151     if (size < align || running_on_valgrind) {
152         align = getpagesize();
153     }
154     ptr = qemu_memalign(align, size);
155     trace_qemu_vmalloc(size, ptr);
156     return ptr;
157 }
158 #endif
159
160 void qemu_vfree(void *ptr)
161 {
162     trace_qemu_vfree(ptr);
163     free(ptr);
164 }
165
166 void socket_set_block(int fd)
167 {
168     int f;
169     f = fcntl(fd, F_GETFL);
170     fcntl(fd, F_SETFL, f & ~O_NONBLOCK);
171 }
172
173 void socket_set_nonblock(int fd)
174 {
175     int f;
176     f = fcntl(fd, F_GETFL);
177     fcntl(fd, F_SETFL, f | O_NONBLOCK);
178 }
179
180 void qemu_set_cloexec(int fd)
181 {
182     int f;
183     f = fcntl(fd, F_GETFD);
184     fcntl(fd, F_SETFD, f | FD_CLOEXEC);
185 }
186
187 /*
188  * Creates a pipe with FD_CLOEXEC set on both file descriptors
189  */
190 int qemu_pipe(int pipefd[2])
191 {
192     int ret;
193
194 #ifdef CONFIG_PIPE2
195     ret = pipe2(pipefd, O_CLOEXEC);
196     if (ret != -1 || errno != ENOSYS) {
197         return ret;
198     }
199 #endif
200     ret = pipe(pipefd);
201     if (ret == 0) {
202         qemu_set_cloexec(pipefd[0]);
203         qemu_set_cloexec(pipefd[1]);
204     }
205
206     return ret;
207 }
208
209 /*
210  * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set.
211  */
212 int qemu_eventfd(int fds[2])
213 {
214 #ifdef CONFIG_EVENTFD
215     int ret;
216
217     ret = eventfd(0, 0);
218     if (ret >= 0) {
219         fds[0] = ret;
220         fds[1] = dup(ret);
221         if (fds[1] == -1) {
222             close(ret);
223             return -1;
224         }
225         qemu_set_cloexec(ret);
226         qemu_set_cloexec(fds[1]);
227         return 0;
228     }
229     if (errno != ENOSYS) {
230         return -1;
231     }
232 #endif
233
234     return qemu_pipe(fds);
235 }
236
237 int qemu_utimens(const char *path, const struct timespec *times)
238 {
239     struct timeval tv[2], tv_now;
240     struct stat st;
241     int i;
242 #ifdef CONFIG_UTIMENSAT
243     int ret;
244
245     ret = utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW);
246     if (ret != -1 || errno != ENOSYS) {
247         return ret;
248     }
249 #endif
250     /* Fallback: use utimes() instead of utimensat() */
251
252     /* happy if special cases */
253     if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) {
254         return 0;
255     }
256     if (times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW) {
257         return utimes(path, NULL);
258     }
259
260     /* prepare for hard cases */
261     if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) {
262         gettimeofday(&tv_now, NULL);
263     }
264     if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) {
265         stat(path, &st);
266     }
267
268     for (i = 0; i < 2; i++) {
269         if (times[i].tv_nsec == UTIME_NOW) {
270             tv[i].tv_sec = tv_now.tv_sec;
271             tv[i].tv_usec = tv_now.tv_usec;
272         } else if (times[i].tv_nsec == UTIME_OMIT) {
273             tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
274             tv[i].tv_usec = 0;
275         } else {
276             tv[i].tv_sec = times[i].tv_sec;
277             tv[i].tv_usec = times[i].tv_nsec / 1000;
278         }
279     }
280
281     return utimes(path, &tv[0]);
282 }