root / oslib-posix.c @ dc1c13d9
History | View | Annotate | Download (6.5 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 | 36b58628 | Avi Kivity | #if defined(__linux__) && defined(__x86_64__)
|
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 | c2a8238a | Stefan Weil | # define CONFIG_VALGRIND
|
44 | fdec9918 | Christian Borntraeger | #elif defined(__linux__) && defined(__s390x__)
|
45 | fdec9918 | Christian Borntraeger | /* Use 1 MiB (segment size) alignment so gmap can be used by KVM. */
|
46 | fdec9918 | Christian Borntraeger | # define QEMU_VMALLOC_ALIGN (256 * 4096) |
47 | 36b58628 | Avi Kivity | #else
|
48 | 36b58628 | Avi Kivity | # define QEMU_VMALLOC_ALIGN getpagesize()
|
49 | 36b58628 | Avi Kivity | #endif
|
50 | 36b58628 | Avi Kivity | |
51 | c1b0b93b | Jes Sorensen | #include "config-host.h" |
52 | c1b0b93b | Jes Sorensen | #include "sysemu.h" |
53 | c1b0b93b | Jes Sorensen | #include "trace.h" |
54 | 9549e764 | Jes Sorensen | #include "qemu_socket.h" |
55 | c1b0b93b | Jes Sorensen | |
56 | c2a8238a | Stefan Weil | #if defined(CONFIG_VALGRIND)
|
57 | c2a8238a | Stefan Weil | static int running_on_valgrind = -1; |
58 | c2a8238a | Stefan Weil | #else
|
59 | c2a8238a | Stefan Weil | # define running_on_valgrind 0 |
60 | c2a8238a | Stefan Weil | #endif
|
61 | cbcfa041 | Paolo Bonzini | #ifdef CONFIG_LINUX
|
62 | cbcfa041 | Paolo Bonzini | #include <sys/syscall.h> |
63 | cbcfa041 | Paolo Bonzini | #endif
|
64 | cbcfa041 | Paolo Bonzini | #ifdef CONFIG_EVENTFD
|
65 | cbcfa041 | Paolo Bonzini | #include <sys/eventfd.h> |
66 | cbcfa041 | Paolo Bonzini | #endif
|
67 | cbcfa041 | Paolo Bonzini | |
68 | cbcfa041 | Paolo Bonzini | int qemu_get_thread_id(void) |
69 | cbcfa041 | Paolo Bonzini | { |
70 | cbcfa041 | Paolo Bonzini | #if defined(__linux__)
|
71 | cbcfa041 | Paolo Bonzini | return syscall(SYS_gettid);
|
72 | cbcfa041 | Paolo Bonzini | #else
|
73 | cbcfa041 | Paolo Bonzini | return getpid();
|
74 | cbcfa041 | Paolo Bonzini | #endif
|
75 | cbcfa041 | Paolo Bonzini | } |
76 | f97742d0 | Alexandre Raymond | |
77 | f97742d0 | Alexandre Raymond | int qemu_daemon(int nochdir, int noclose) |
78 | f97742d0 | Alexandre Raymond | { |
79 | f97742d0 | Alexandre Raymond | return daemon(nochdir, noclose);
|
80 | f97742d0 | Alexandre Raymond | } |
81 | f97742d0 | Alexandre Raymond | |
82 | b152aa84 | Jes Sorensen | void *qemu_oom_check(void *ptr) |
83 | c1b0b93b | Jes Sorensen | { |
84 | c1b0b93b | Jes Sorensen | if (ptr == NULL) { |
85 | c1b0b93b | Jes Sorensen | fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
|
86 | c1b0b93b | Jes Sorensen | abort(); |
87 | c1b0b93b | Jes Sorensen | } |
88 | c1b0b93b | Jes Sorensen | return ptr;
|
89 | c1b0b93b | Jes Sorensen | } |
90 | c1b0b93b | Jes Sorensen | |
91 | c1b0b93b | Jes Sorensen | void *qemu_memalign(size_t alignment, size_t size)
|
92 | c1b0b93b | Jes Sorensen | { |
93 | c1b0b93b | Jes Sorensen | void *ptr;
|
94 | c1b0b93b | Jes Sorensen | #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
|
95 | c1b0b93b | Jes Sorensen | int ret;
|
96 | c1b0b93b | Jes Sorensen | ret = posix_memalign(&ptr, alignment, size); |
97 | c1b0b93b | Jes Sorensen | if (ret != 0) { |
98 | c1b0b93b | Jes Sorensen | fprintf(stderr, "Failed to allocate %zu B: %s\n",
|
99 | c1b0b93b | Jes Sorensen | size, strerror(ret)); |
100 | c1b0b93b | Jes Sorensen | abort(); |
101 | c1b0b93b | Jes Sorensen | } |
102 | c1b0b93b | Jes Sorensen | #elif defined(CONFIG_BSD)
|
103 | b152aa84 | Jes Sorensen | ptr = qemu_oom_check(valloc(size)); |
104 | c1b0b93b | Jes Sorensen | #else
|
105 | b152aa84 | Jes Sorensen | ptr = qemu_oom_check(memalign(alignment, size)); |
106 | c1b0b93b | Jes Sorensen | #endif
|
107 | c1b0b93b | Jes Sorensen | trace_qemu_memalign(alignment, size, ptr); |
108 | c1b0b93b | Jes Sorensen | return ptr;
|
109 | c1b0b93b | Jes Sorensen | } |
110 | c1b0b93b | Jes Sorensen | |
111 | 71ea2e01 | Blue Swirl | /* conflicts with qemu_vmalloc in bsd-user/mmap.c */
|
112 | 71ea2e01 | Blue Swirl | #if !defined(CONFIG_BSD_USER)
|
113 | c1b0b93b | Jes Sorensen | /* alloc shared memory pages */
|
114 | c1b0b93b | Jes Sorensen | void *qemu_vmalloc(size_t size)
|
115 | c1b0b93b | Jes Sorensen | { |
116 | c7f4111a | Jes Sorensen | void *ptr;
|
117 | 36b58628 | Avi Kivity | size_t align = QEMU_VMALLOC_ALIGN; |
118 | 36b58628 | Avi Kivity | |
119 | c2a8238a | Stefan Weil | #if defined(CONFIG_VALGRIND)
|
120 | c2a8238a | Stefan Weil | if (running_on_valgrind < 0) { |
121 | c2a8238a | Stefan Weil | /* First call, test whether we are running on Valgrind.
|
122 | c2a8238a | Stefan Weil | This is a substitute for RUNNING_ON_VALGRIND from valgrind.h. */
|
123 | c2a8238a | Stefan Weil | const char *ld = getenv("LD_PRELOAD"); |
124 | c2a8238a | Stefan Weil | running_on_valgrind = (ld != NULL && strstr(ld, "vgpreload")); |
125 | c2a8238a | Stefan Weil | } |
126 | c2a8238a | Stefan Weil | #endif
|
127 | c2a8238a | Stefan Weil | |
128 | c2a8238a | Stefan Weil | if (size < align || running_on_valgrind) {
|
129 | 36b58628 | Avi Kivity | align = getpagesize(); |
130 | 36b58628 | Avi Kivity | } |
131 | 36b58628 | Avi Kivity | ptr = qemu_memalign(align, size); |
132 | c7f4111a | Jes Sorensen | trace_qemu_vmalloc(size, ptr); |
133 | c7f4111a | Jes Sorensen | return ptr;
|
134 | c1b0b93b | Jes Sorensen | } |
135 | 71ea2e01 | Blue Swirl | #endif
|
136 | c1b0b93b | Jes Sorensen | |
137 | c1b0b93b | Jes Sorensen | void qemu_vfree(void *ptr) |
138 | c1b0b93b | Jes Sorensen | { |
139 | c1b0b93b | Jes Sorensen | trace_qemu_vfree(ptr); |
140 | c1b0b93b | Jes Sorensen | free(ptr); |
141 | c1b0b93b | Jes Sorensen | } |
142 | 9549e764 | Jes Sorensen | |
143 | 154b9a0c | Paolo Bonzini | void socket_set_block(int fd) |
144 | 154b9a0c | Paolo Bonzini | { |
145 | 154b9a0c | Paolo Bonzini | int f;
|
146 | 154b9a0c | Paolo Bonzini | f = fcntl(fd, F_GETFL); |
147 | 154b9a0c | Paolo Bonzini | fcntl(fd, F_SETFL, f & ~O_NONBLOCK); |
148 | 154b9a0c | Paolo Bonzini | } |
149 | 154b9a0c | Paolo Bonzini | |
150 | 9549e764 | Jes Sorensen | void socket_set_nonblock(int fd) |
151 | 9549e764 | Jes Sorensen | { |
152 | 9549e764 | Jes Sorensen | int f;
|
153 | 9549e764 | Jes Sorensen | f = fcntl(fd, F_GETFL); |
154 | 9549e764 | Jes Sorensen | fcntl(fd, F_SETFL, f | O_NONBLOCK); |
155 | 9549e764 | Jes Sorensen | } |
156 | 9549e764 | Jes Sorensen | |
157 | 9549e764 | Jes Sorensen | void qemu_set_cloexec(int fd) |
158 | 9549e764 | Jes Sorensen | { |
159 | 9549e764 | Jes Sorensen | int f;
|
160 | 9549e764 | Jes Sorensen | f = fcntl(fd, F_GETFD); |
161 | 9549e764 | Jes Sorensen | fcntl(fd, F_SETFD, f | FD_CLOEXEC); |
162 | 9549e764 | Jes Sorensen | } |
163 | 70e72ce4 | Jes Sorensen | |
164 | 70e72ce4 | Jes Sorensen | /*
|
165 | 70e72ce4 | Jes Sorensen | * Creates a pipe with FD_CLOEXEC set on both file descriptors
|
166 | 70e72ce4 | Jes Sorensen | */
|
167 | 70e72ce4 | Jes Sorensen | int qemu_pipe(int pipefd[2]) |
168 | 70e72ce4 | Jes Sorensen | { |
169 | 70e72ce4 | Jes Sorensen | int ret;
|
170 | 70e72ce4 | Jes Sorensen | |
171 | 70e72ce4 | Jes Sorensen | #ifdef CONFIG_PIPE2
|
172 | 70e72ce4 | Jes Sorensen | ret = pipe2(pipefd, O_CLOEXEC); |
173 | 70e72ce4 | Jes Sorensen | if (ret != -1 || errno != ENOSYS) { |
174 | 70e72ce4 | Jes Sorensen | return ret;
|
175 | 70e72ce4 | Jes Sorensen | } |
176 | 70e72ce4 | Jes Sorensen | #endif
|
177 | 70e72ce4 | Jes Sorensen | ret = pipe(pipefd); |
178 | 70e72ce4 | Jes Sorensen | if (ret == 0) { |
179 | 70e72ce4 | Jes Sorensen | qemu_set_cloexec(pipefd[0]);
|
180 | 70e72ce4 | Jes Sorensen | qemu_set_cloexec(pipefd[1]);
|
181 | 70e72ce4 | Jes Sorensen | } |
182 | 70e72ce4 | Jes Sorensen | |
183 | 70e72ce4 | Jes Sorensen | return ret;
|
184 | 70e72ce4 | Jes Sorensen | } |
185 | 38671423 | Hidetoshi Seto | |
186 | cbcfa041 | Paolo Bonzini | /*
|
187 | cbcfa041 | Paolo Bonzini | * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set.
|
188 | cbcfa041 | Paolo Bonzini | */
|
189 | cbcfa041 | Paolo Bonzini | int qemu_eventfd(int fds[2]) |
190 | cbcfa041 | Paolo Bonzini | { |
191 | cbcfa041 | Paolo Bonzini | #ifdef CONFIG_EVENTFD
|
192 | cbcfa041 | Paolo Bonzini | int ret;
|
193 | cbcfa041 | Paolo Bonzini | |
194 | cbcfa041 | Paolo Bonzini | ret = eventfd(0, 0); |
195 | cbcfa041 | Paolo Bonzini | if (ret >= 0) { |
196 | cbcfa041 | Paolo Bonzini | fds[0] = ret;
|
197 | cbcfa041 | Paolo Bonzini | fds[1] = dup(ret);
|
198 | cbcfa041 | Paolo Bonzini | if (fds[1] == -1) { |
199 | cbcfa041 | Paolo Bonzini | close(ret); |
200 | cbcfa041 | Paolo Bonzini | return -1; |
201 | cbcfa041 | Paolo Bonzini | } |
202 | cbcfa041 | Paolo Bonzini | qemu_set_cloexec(ret); |
203 | cbcfa041 | Paolo Bonzini | qemu_set_cloexec(fds[1]);
|
204 | cbcfa041 | Paolo Bonzini | return 0; |
205 | cbcfa041 | Paolo Bonzini | } |
206 | cbcfa041 | Paolo Bonzini | if (errno != ENOSYS) {
|
207 | cbcfa041 | Paolo Bonzini | return -1; |
208 | cbcfa041 | Paolo Bonzini | } |
209 | cbcfa041 | Paolo Bonzini | #endif
|
210 | cbcfa041 | Paolo Bonzini | |
211 | cbcfa041 | Paolo Bonzini | return qemu_pipe(fds);
|
212 | cbcfa041 | Paolo Bonzini | } |
213 | cbcfa041 | Paolo Bonzini | |
214 | ae0f940e | Paolo Bonzini | int qemu_utimens(const char *path, const struct timespec *times) |
215 | 38671423 | Hidetoshi Seto | { |
216 | 38671423 | Hidetoshi Seto | struct timeval tv[2], tv_now; |
217 | 38671423 | Hidetoshi Seto | struct stat st;
|
218 | 38671423 | Hidetoshi Seto | int i;
|
219 | 38671423 | Hidetoshi Seto | #ifdef CONFIG_UTIMENSAT
|
220 | 38671423 | Hidetoshi Seto | int ret;
|
221 | 38671423 | Hidetoshi Seto | |
222 | ae0f940e | Paolo Bonzini | ret = utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW); |
223 | 38671423 | Hidetoshi Seto | if (ret != -1 || errno != ENOSYS) { |
224 | 38671423 | Hidetoshi Seto | return ret;
|
225 | 38671423 | Hidetoshi Seto | } |
226 | 38671423 | Hidetoshi Seto | #endif
|
227 | 38671423 | Hidetoshi Seto | /* Fallback: use utimes() instead of utimensat() */
|
228 | 38671423 | Hidetoshi Seto | |
229 | 38671423 | Hidetoshi Seto | /* happy if special cases */
|
230 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) { |
231 | 38671423 | Hidetoshi Seto | return 0; |
232 | 38671423 | Hidetoshi Seto | } |
233 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_NOW && times[1].tv_nsec == UTIME_NOW) { |
234 | 38671423 | Hidetoshi Seto | return utimes(path, NULL); |
235 | 38671423 | Hidetoshi Seto | } |
236 | 38671423 | Hidetoshi Seto | |
237 | 38671423 | Hidetoshi Seto | /* prepare for hard cases */
|
238 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) { |
239 | 38671423 | Hidetoshi Seto | gettimeofday(&tv_now, NULL);
|
240 | 38671423 | Hidetoshi Seto | } |
241 | 38671423 | Hidetoshi Seto | if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) { |
242 | 38671423 | Hidetoshi Seto | stat(path, &st); |
243 | 38671423 | Hidetoshi Seto | } |
244 | 38671423 | Hidetoshi Seto | |
245 | 38671423 | Hidetoshi Seto | for (i = 0; i < 2; i++) { |
246 | 38671423 | Hidetoshi Seto | if (times[i].tv_nsec == UTIME_NOW) {
|
247 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = tv_now.tv_sec; |
248 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = tv_now.tv_usec; |
249 | 38671423 | Hidetoshi Seto | } else if (times[i].tv_nsec == UTIME_OMIT) { |
250 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = (i == 0) ? st.st_atime : st.st_mtime;
|
251 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = 0;
|
252 | 38671423 | Hidetoshi Seto | } else {
|
253 | 38671423 | Hidetoshi Seto | tv[i].tv_sec = times[i].tv_sec; |
254 | 38671423 | Hidetoshi Seto | tv[i].tv_usec = times[i].tv_nsec / 1000;
|
255 | 38671423 | Hidetoshi Seto | } |
256 | 38671423 | Hidetoshi Seto | } |
257 | 38671423 | Hidetoshi Seto | |
258 | 38671423 | Hidetoshi Seto | return utimes(path, &tv[0]); |
259 | 38671423 | Hidetoshi Seto | } |