root / util / oslib-posix.c @ 13401ba0
History | View | Annotate | Download (6.7 kB)
1 | c1b0b93b | Jes Sorensen | /*
|
---|---|---|---|
2 | c1b0b93b | Jes Sorensen | * os-posix-lib.c
|
3 | c1b0b93b | Jes Sorensen | *
|
4 | c1b0b93b | Jes Sorensen | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | c1b0b93b | Jes Sorensen | * Copyright (c) 2010 Red Hat, Inc.
|
6 | c1b0b93b | Jes Sorensen | *
|
7 | c1b0b93b | Jes Sorensen | * QEMU library functions on POSIX which are shared between QEMU and
|
8 | c1b0b93b | Jes Sorensen | * the QEMU tools.
|
9 | c1b0b93b | Jes Sorensen | *
|
10 | c1b0b93b | Jes Sorensen | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
11 | c1b0b93b | Jes Sorensen | * of this software and associated documentation files (the "Software"), to deal
|
12 | c1b0b93b | Jes Sorensen | * in the Software without restriction, including without limitation the rights
|
13 | c1b0b93b | Jes Sorensen | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
14 | c1b0b93b | Jes Sorensen | * copies of the Software, and to permit persons to whom the Software is
|
15 | c1b0b93b | Jes Sorensen | * furnished to do so, subject to the following conditions:
|
16 | c1b0b93b | Jes Sorensen | *
|
17 | c1b0b93b | Jes Sorensen | * The above copyright notice and this permission notice shall be included in
|
18 | c1b0b93b | Jes Sorensen | * all copies or substantial portions of the Software.
|
19 | c1b0b93b | Jes Sorensen | *
|
20 | c1b0b93b | Jes Sorensen | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
21 | c1b0b93b | Jes Sorensen | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
22 | c1b0b93b | Jes Sorensen | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
23 | c1b0b93b | Jes Sorensen | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
24 | c1b0b93b | Jes Sorensen | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
25 | c1b0b93b | Jes Sorensen | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
26 | c1b0b93b | Jes Sorensen | * THE SOFTWARE.
|
27 | c1b0b93b | Jes Sorensen | */
|
28 | c1b0b93b | Jes Sorensen | |
29 | f97742d0 | Alexandre Raymond | /* The following block of code temporarily renames the daemon() function so the
|
30 | f97742d0 | Alexandre Raymond | compiler does not see the warning associated with it in stdlib.h on OSX */
|
31 | f97742d0 | Alexandre Raymond | #ifdef __APPLE__
|
32 | f97742d0 | Alexandre Raymond | #define daemon qemu_fake_daemon_function
|
33 | f97742d0 | Alexandre Raymond | #include <stdlib.h> |
34 | f97742d0 | Alexandre Raymond | #undef daemon
|
35 | f97742d0 | Alexandre Raymond | extern int daemon(int, int); |
36 | f97742d0 | Alexandre Raymond | #endif
|
37 | f97742d0 | Alexandre Raymond | |
38 | 2e07b297 | Peter Maydell | #if defined(__linux__) && (defined(__x86_64__) || defined(__arm__))
|
39 | c2a8238a | Stefan Weil | /* Use 2 MiB alignment so transparent hugepages can be used by KVM.
|
40 | c2a8238a | Stefan Weil | Valgrind does not support alignments larger than 1 MiB,
|
41 | c2a8238a | Stefan Weil | therefore we need special code which handles running on Valgrind. */
|
42 | 36b58628 | Avi Kivity | # define QEMU_VMALLOC_ALIGN (512 * 4096) |
43 | fdec9918 | Christian Borntraeger | #elif defined(__linux__) && defined(__s390x__)
|
44 | fdec9918 | Christian Borntraeger | /* Use 1 MiB (segment size) alignment so gmap can be used by KVM. */
|
45 | fdec9918 | Christian Borntraeger | # define QEMU_VMALLOC_ALIGN (256 * 4096) |
46 | 36b58628 | Avi Kivity | #else
|
47 | 36b58628 | Avi Kivity | # define QEMU_VMALLOC_ALIGN getpagesize()
|
48 | 36b58628 | Avi Kivity | #endif
|
49 | 36b58628 | Avi Kivity | |
50 | 13401ba0 | Stefan Hajnoczi | #include <termios.h> |
51 | 13401ba0 | Stefan Hajnoczi | #include <unistd.h> |
52 | 13401ba0 | Stefan Hajnoczi | |
53 | e2ea3515 | Laszlo Ersek | #include <glib/gprintf.h> |
54 | e2ea3515 | Laszlo Ersek | |
55 | c1b0b93b | Jes Sorensen | #include "config-host.h" |
56 | 9c17d615 | Paolo Bonzini | #include "sysemu/sysemu.h" |
57 | c1b0b93b | Jes Sorensen | #include "trace.h" |
58 | 1de7afc9 | Paolo Bonzini | #include "qemu/sockets.h" |
59 | 7dda5dc8 | Paolo Bonzini | #include <sys/mman.h> |
60 | c1b0b93b | Jes Sorensen | |
61 | cbcfa041 | Paolo Bonzini | #ifdef CONFIG_LINUX
|
62 | cbcfa041 | Paolo Bonzini | #include <sys/syscall.h> |
63 | cbcfa041 | Paolo Bonzini | #endif
|
64 | cbcfa041 | Paolo Bonzini | |
65 | cbcfa041 | Paolo Bonzini | int qemu_get_thread_id(void) |
66 | cbcfa041 | Paolo Bonzini | { |
67 | cbcfa041 | Paolo Bonzini | #if defined(__linux__)
|
68 | cbcfa041 | Paolo Bonzini | return syscall(SYS_gettid);
|
69 | cbcfa041 | Paolo Bonzini | #else
|
70 | cbcfa041 | Paolo Bonzini | return getpid();
|
71 | cbcfa041 | Paolo Bonzini | #endif
|
72 | cbcfa041 | Paolo Bonzini | } |
73 | f97742d0 | Alexandre Raymond | |
74 | f97742d0 | Alexandre Raymond | int qemu_daemon(int nochdir, int noclose) |
75 | f97742d0 | Alexandre Raymond | { |
76 | f97742d0 | Alexandre Raymond | return daemon(nochdir, noclose);
|
77 | f97742d0 | Alexandre Raymond | } |
78 | f97742d0 | Alexandre Raymond | |
79 | b152aa84 | Jes Sorensen | void *qemu_oom_check(void *ptr) |
80 | c1b0b93b | Jes Sorensen | { |
81 | c1b0b93b | Jes Sorensen | if (ptr == NULL) { |
82 | c1b0b93b | Jes Sorensen | fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
|
83 | c1b0b93b | Jes Sorensen | abort(); |
84 | c1b0b93b | Jes Sorensen | } |
85 | c1b0b93b | Jes Sorensen | return ptr;
|
86 | c1b0b93b | Jes Sorensen | } |
87 | c1b0b93b | Jes Sorensen | |
88 | c1b0b93b | Jes Sorensen | void *qemu_memalign(size_t alignment, size_t size)
|
89 | c1b0b93b | Jes Sorensen | { |
90 | c1b0b93b | Jes Sorensen | void *ptr;
|
91 | c1b0b93b | Jes Sorensen | #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
|
92 | c1b0b93b | Jes Sorensen | int ret;
|
93 | c1b0b93b | Jes Sorensen | ret = posix_memalign(&ptr, alignment, size); |
94 | c1b0b93b | Jes Sorensen | if (ret != 0) { |
95 | c1b0b93b | Jes Sorensen | fprintf(stderr, "Failed to allocate %zu B: %s\n",
|
96 | c1b0b93b | Jes Sorensen | size, strerror(ret)); |
97 | c1b0b93b | Jes Sorensen | abort(); |
98 | c1b0b93b | Jes Sorensen | } |
99 | c1b0b93b | Jes Sorensen | #elif defined(CONFIG_BSD)
|
100 | b152aa84 | Jes Sorensen | ptr = qemu_oom_check(valloc(size)); |
101 | c1b0b93b | Jes Sorensen | #else
|
102 | b152aa84 | Jes Sorensen | ptr = qemu_oom_check(memalign(alignment, size)); |
103 | c1b0b93b | Jes Sorensen | #endif
|
104 | c1b0b93b | Jes Sorensen | trace_qemu_memalign(alignment, size, ptr); |
105 | c1b0b93b | Jes Sorensen | return ptr;
|
106 | c1b0b93b | Jes Sorensen | } |
107 | c1b0b93b | Jes Sorensen | |
108 | c1b0b93b | Jes Sorensen | /* alloc shared memory pages */
|
109 | 6eebf958 | Paolo Bonzini | void *qemu_anon_ram_alloc(size_t size)
|
110 | c1b0b93b | Jes Sorensen | { |
111 | 36b58628 | Avi Kivity | size_t align = QEMU_VMALLOC_ALIGN; |
112 | 7dda5dc8 | Paolo Bonzini | size_t total = size + align - getpagesize(); |
113 | 7dda5dc8 | Paolo Bonzini | void *ptr = mmap(0, total, PROT_READ | PROT_WRITE, |
114 | 7dda5dc8 | Paolo Bonzini | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
115 | 7dda5dc8 | Paolo Bonzini | size_t offset = QEMU_ALIGN_UP((uintptr_t)ptr, align) - (uintptr_t)ptr; |
116 | 36b58628 | Avi Kivity | |
117 | 7dda5dc8 | Paolo Bonzini | if (ptr == MAP_FAILED) {
|
118 | 39228250 | Markus Armbruster | return NULL; |
119 | c2a8238a | Stefan Weil | } |
120 | c2a8238a | Stefan Weil | |
121 | 7dda5dc8 | Paolo Bonzini | ptr += offset; |
122 | 7dda5dc8 | Paolo Bonzini | total -= offset; |
123 | 7dda5dc8 | Paolo Bonzini | |
124 | 7dda5dc8 | Paolo Bonzini | if (offset > 0) { |
125 | 7dda5dc8 | Paolo Bonzini | munmap(ptr - offset, offset); |
126 | 36b58628 | Avi Kivity | } |
127 | 7dda5dc8 | Paolo Bonzini | if (total > size) {
|
128 | 7dda5dc8 | Paolo Bonzini | munmap(ptr + size, total - size); |
129 | 7dda5dc8 | Paolo Bonzini | } |
130 | 7dda5dc8 | Paolo Bonzini | |
131 | 6eebf958 | Paolo Bonzini | trace_qemu_anon_ram_alloc(size, ptr); |
132 | c7f4111a | Jes Sorensen | return ptr;
|
133 | c1b0b93b | Jes Sorensen | } |
134 | c1b0b93b | Jes Sorensen | |
135 | c1b0b93b | Jes Sorensen | void qemu_vfree(void *ptr) |
136 | c1b0b93b | Jes Sorensen | { |
137 | c1b0b93b | Jes Sorensen | trace_qemu_vfree(ptr); |
138 | c1b0b93b | Jes Sorensen | free(ptr); |
139 | c1b0b93b | Jes Sorensen | } |
140 | 9549e764 | Jes Sorensen | |
141 | e7a09b92 | Paolo Bonzini | void qemu_anon_ram_free(void *ptr, size_t size) |
142 | e7a09b92 | Paolo Bonzini | { |
143 | e7a09b92 | Paolo Bonzini | trace_qemu_anon_ram_free(ptr, size); |
144 | e7a09b92 | Paolo Bonzini | if (ptr) {
|
145 | e7a09b92 | Paolo Bonzini | munmap(ptr, size); |
146 | e7a09b92 | Paolo Bonzini | } |
147 | e7a09b92 | Paolo Bonzini | } |
148 | e7a09b92 | Paolo Bonzini | |
149 | f9e8cacc | Stefan Hajnoczi | void qemu_set_block(int fd) |
150 | 154b9a0c | Paolo Bonzini | { |
151 | 154b9a0c | Paolo Bonzini | int f;
|
152 | 154b9a0c | Paolo Bonzini | f = fcntl(fd, F_GETFL); |
153 | 154b9a0c | Paolo Bonzini | fcntl(fd, F_SETFL, f & ~O_NONBLOCK); |
154 | 154b9a0c | Paolo Bonzini | } |
155 | 154b9a0c | Paolo Bonzini | |
156 | f9e8cacc | Stefan Hajnoczi | void qemu_set_nonblock(int fd) |
157 | 9549e764 | Jes Sorensen | { |
158 | 9549e764 | Jes Sorensen | int f;
|
159 | 9549e764 | Jes Sorensen | f = fcntl(fd, F_GETFL); |
160 | 9549e764 | Jes Sorensen | fcntl(fd, F_SETFL, f | O_NONBLOCK); |
161 | 9549e764 | Jes Sorensen | } |
162 | 9549e764 | Jes Sorensen | |
163 | 606600a1 | Sebastian Ottlik | int socket_set_fast_reuse(int fd) |
164 | 606600a1 | Sebastian Ottlik | { |
165 | 606600a1 | Sebastian Ottlik | int val = 1, ret; |
166 | 606600a1 | Sebastian Ottlik | |
167 | 606600a1 | Sebastian Ottlik | ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
168 | 606600a1 | Sebastian Ottlik | (const char *)&val, sizeof(val)); |
169 | 606600a1 | Sebastian Ottlik | |
170 | 606600a1 | Sebastian Ottlik | assert(ret == 0);
|
171 | 606600a1 | Sebastian Ottlik | |
172 | 606600a1 | Sebastian Ottlik | return ret;
|
173 | 606600a1 | Sebastian Ottlik | } |
174 | 606600a1 | Sebastian Ottlik | |
175 | 9549e764 | Jes Sorensen | void qemu_set_cloexec(int fd) |
176 | 9549e764 | Jes Sorensen | { |
177 | 9549e764 | Jes Sorensen | int f;
|
178 | 9549e764 | Jes Sorensen | f = fcntl(fd, F_GETFD); |
179 | 9549e764 | Jes Sorensen | fcntl(fd, F_SETFD, f | FD_CLOEXEC); |
180 | 9549e764 | Jes Sorensen | } |
181 | 70e72ce4 | Jes Sorensen | |
182 | 70e72ce4 | Jes Sorensen | /*
|
183 | 70e72ce4 | Jes Sorensen | * Creates a pipe with FD_CLOEXEC set on both file descriptors
|
184 | 70e72ce4 | Jes Sorensen | */
|
185 | 70e72ce4 | Jes Sorensen | int qemu_pipe(int pipefd[2]) |
186 | 70e72ce4 | Jes Sorensen | { |
187 | 70e72ce4 | Jes Sorensen | int ret;
|
188 | 70e72ce4 | Jes Sorensen | |
189 | 70e72ce4 | Jes Sorensen | #ifdef CONFIG_PIPE2
|
190 | 70e72ce4 | Jes Sorensen | ret = pipe2(pipefd, O_CLOEXEC); |
191 | 70e72ce4 | Jes Sorensen | if (ret != -1 || errno != ENOSYS) { |
192 | 70e72ce4 | Jes Sorensen | return ret;
|
193 | 70e72ce4 | Jes Sorensen | } |
194 | 70e72ce4 | Jes Sorensen | #endif
|
195 | 70e72ce4 | Jes Sorensen | ret = pipe(pipefd); |
196 | 70e72ce4 | Jes Sorensen | if (ret == 0) { |
197 | 70e72ce4 | Jes Sorensen | qemu_set_cloexec(pipefd[0]);
|
198 | 70e72ce4 | Jes Sorensen | qemu_set_cloexec(pipefd[1]);
|
199 | 70e72ce4 | Jes Sorensen | } |
200 | 70e72ce4 | Jes Sorensen | |
201 | 70e72ce4 | Jes Sorensen | return ret;
|
202 | 70e72ce4 | Jes Sorensen | } |
203 | 38671423 | Hidetoshi Seto | |
204 | ae0f940e | Paolo Bonzini | int qemu_utimens(const char *path, const struct timespec *times) |
205 | 38671423 | Hidetoshi Seto | { |
206 | 38671423 | Hidetoshi Seto | struct timeval tv[2], tv_now; |
207 | 38671423 | Hidetoshi Seto | struct stat st;
|
208 | 38671423 | Hidetoshi Seto | int i;
|
209 | 38671423 | Hidetoshi Seto | #ifdef CONFIG_UTIMENSAT
|
210 | 38671423 | Hidetoshi Seto | int ret;
|
211 | 38671423 | Hidetoshi Seto | |
212 | ae0f940e | Paolo Bonzini | ret = utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW); |
213 | 38671423 | Hidetoshi Seto | if (ret != -1 || errno != ENOSYS) { |
214 | 38671423 | Hidetoshi Seto | return ret;
|
215 | 38671423 | Hidetoshi Seto | } |
216 | 38671423 | Hidetoshi Seto | #endif
|
217 | 38671423 | Hidetoshi Seto | /* Fallback: use utimes() instead of utimensat() */
|
218 | 38671423 | Hidetoshi Seto | |
219 | 38671423 | Hidetoshi Seto | /* happy if special cases */
|
220 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) { |
221 | 38671423 | Hidetoshi Seto | return 0; |
222 | 38671423 | Hidetoshi Seto | } |
223 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW) { |
224 | 38671423 | Hidetoshi Seto | return utimes(path, NULL); |
225 | 38671423 | Hidetoshi Seto | } |
226 | 38671423 | Hidetoshi Seto | |
227 | 38671423 | Hidetoshi Seto | /* prepare for hard cases */
|
228 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) { |
229 | 38671423 | Hidetoshi Seto | gettimeofday(&tv_now, NULL);
|
230 | 38671423 | Hidetoshi Seto | } |
231 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) { |
232 | 38671423 | Hidetoshi Seto | stat(path, &st); |
233 | 38671423 | Hidetoshi Seto | } |
234 | 38671423 | Hidetoshi Seto | |
235 | 38671423 | Hidetoshi Seto | for (i = 0; i < 2; i++) { |
236 | 38671423 | Hidetoshi Seto | if (times[i].tv_nsec == UTIME_NOW) {
|
237 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = tv_now.tv_sec; |
238 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = tv_now.tv_usec; |
239 | 38671423 | Hidetoshi Seto | } else if (times[i].tv_nsec == UTIME_OMIT) { |
240 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
|
241 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = 0;
|
242 | 38671423 | Hidetoshi Seto | } else {
|
243 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = times[i].tv_sec; |
244 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = times[i].tv_nsec / 1000;
|
245 | 38671423 | Hidetoshi Seto | } |
246 | 38671423 | Hidetoshi Seto | } |
247 | 38671423 | Hidetoshi Seto | |
248 | 38671423 | Hidetoshi Seto | return utimes(path, &tv[0]); |
249 | 38671423 | Hidetoshi Seto | } |
250 | e2ea3515 | Laszlo Ersek | |
251 | e2ea3515 | Laszlo Ersek | char *
|
252 | e2ea3515 | Laszlo Ersek | qemu_get_local_state_pathname(const char *relative_pathname) |
253 | e2ea3515 | Laszlo Ersek | { |
254 | e2ea3515 | Laszlo Ersek | return g_strdup_printf("%s/%s", CONFIG_QEMU_LOCALSTATEDIR, |
255 | e2ea3515 | Laszlo Ersek | relative_pathname); |
256 | e2ea3515 | Laszlo Ersek | } |
257 | 13401ba0 | Stefan Hajnoczi | |
258 | 13401ba0 | Stefan Hajnoczi | void qemu_set_tty_echo(int fd, bool echo) |
259 | 13401ba0 | Stefan Hajnoczi | { |
260 | 13401ba0 | Stefan Hajnoczi | struct termios tty;
|
261 | 13401ba0 | Stefan Hajnoczi | |
262 | 13401ba0 | Stefan Hajnoczi | tcgetattr(fd, &tty); |
263 | 13401ba0 | Stefan Hajnoczi | |
264 | 13401ba0 | Stefan Hajnoczi | if (echo) {
|
265 | 13401ba0 | Stefan Hajnoczi | tty.c_lflag |= ECHO | ECHONL | ICANON | IEXTEN; |
266 | 13401ba0 | Stefan Hajnoczi | } else {
|
267 | 13401ba0 | Stefan Hajnoczi | tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN); |
268 | 13401ba0 | Stefan Hajnoczi | } |
269 | 13401ba0 | Stefan Hajnoczi | |
270 | 13401ba0 | Stefan Hajnoczi | tcsetattr(fd, TCSANOW, &tty); |
271 | 13401ba0 | Stefan Hajnoczi | } |