root / cutils.c @ 0ecb72a5
History | View | Annotate | Download (12.8 kB)
1 | 18607dcb | bellard | /*
|
---|---|---|---|
2 | 18607dcb | bellard | * Simple C functions to supplement the C library
|
3 | 5fafdf24 | ths | *
|
4 | 18607dcb | bellard | * Copyright (c) 2006 Fabrice Bellard
|
5 | 18607dcb | bellard | *
|
6 | 18607dcb | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 18607dcb | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 18607dcb | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 18607dcb | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 18607dcb | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 18607dcb | bellard | * furnished to do so, subject to the following conditions:
|
12 | 18607dcb | bellard | *
|
13 | 18607dcb | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 18607dcb | bellard | * all copies or substantial portions of the Software.
|
15 | 18607dcb | bellard | *
|
16 | 18607dcb | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 18607dcb | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 18607dcb | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 18607dcb | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 18607dcb | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 18607dcb | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 18607dcb | bellard | * THE SOFTWARE.
|
23 | 18607dcb | bellard | */
|
24 | faf07963 | pbrook | #include "qemu-common.h" |
25 | 8d371d4b | aliguori | #include "host-utils.h" |
26 | 9f9b17a4 | Jes Sorensen | #include <math.h> |
27 | 18607dcb | bellard | |
28 | 8c5135f9 | Paolo Bonzini | #include "qemu_socket.h" |
29 | 8c5135f9 | Paolo Bonzini | |
30 | 18607dcb | bellard | void pstrcpy(char *buf, int buf_size, const char *str) |
31 | 18607dcb | bellard | { |
32 | 18607dcb | bellard | int c;
|
33 | 18607dcb | bellard | char *q = buf;
|
34 | 18607dcb | bellard | |
35 | 18607dcb | bellard | if (buf_size <= 0) |
36 | 18607dcb | bellard | return;
|
37 | 18607dcb | bellard | |
38 | 18607dcb | bellard | for(;;) {
|
39 | 18607dcb | bellard | c = *str++; |
40 | 18607dcb | bellard | if (c == 0 || q >= buf + buf_size - 1) |
41 | 18607dcb | bellard | break;
|
42 | 18607dcb | bellard | *q++ = c; |
43 | 18607dcb | bellard | } |
44 | 18607dcb | bellard | *q = '\0';
|
45 | 18607dcb | bellard | } |
46 | 18607dcb | bellard | |
47 | 18607dcb | bellard | /* strcat and truncate. */
|
48 | 18607dcb | bellard | char *pstrcat(char *buf, int buf_size, const char *s) |
49 | 18607dcb | bellard | { |
50 | 18607dcb | bellard | int len;
|
51 | 18607dcb | bellard | len = strlen(buf); |
52 | 5fafdf24 | ths | if (len < buf_size)
|
53 | 18607dcb | bellard | pstrcpy(buf + len, buf_size - len, s); |
54 | 18607dcb | bellard | return buf;
|
55 | 18607dcb | bellard | } |
56 | 18607dcb | bellard | |
57 | 18607dcb | bellard | int strstart(const char *str, const char *val, const char **ptr) |
58 | 18607dcb | bellard | { |
59 | 18607dcb | bellard | const char *p, *q; |
60 | 18607dcb | bellard | p = str; |
61 | 18607dcb | bellard | q = val; |
62 | 18607dcb | bellard | while (*q != '\0') { |
63 | 18607dcb | bellard | if (*p != *q)
|
64 | 18607dcb | bellard | return 0; |
65 | 18607dcb | bellard | p++; |
66 | 18607dcb | bellard | q++; |
67 | 18607dcb | bellard | } |
68 | 18607dcb | bellard | if (ptr)
|
69 | 18607dcb | bellard | *ptr = p; |
70 | 18607dcb | bellard | return 1; |
71 | 18607dcb | bellard | } |
72 | 18607dcb | bellard | |
73 | 18607dcb | bellard | int stristart(const char *str, const char *val, const char **ptr) |
74 | 18607dcb | bellard | { |
75 | 18607dcb | bellard | const char *p, *q; |
76 | 18607dcb | bellard | p = str; |
77 | 18607dcb | bellard | q = val; |
78 | 18607dcb | bellard | while (*q != '\0') { |
79 | cd390083 | blueswir1 | if (qemu_toupper(*p) != qemu_toupper(*q))
|
80 | 18607dcb | bellard | return 0; |
81 | 18607dcb | bellard | p++; |
82 | 18607dcb | bellard | q++; |
83 | 18607dcb | bellard | } |
84 | 18607dcb | bellard | if (ptr)
|
85 | 18607dcb | bellard | *ptr = p; |
86 | 18607dcb | bellard | return 1; |
87 | 18607dcb | bellard | } |
88 | 3c6b2088 | bellard | |
89 | d43277c5 | Blue Swirl | /* XXX: use host strnlen if available ? */
|
90 | d43277c5 | Blue Swirl | int qemu_strnlen(const char *s, int max_len) |
91 | d43277c5 | Blue Swirl | { |
92 | d43277c5 | Blue Swirl | int i;
|
93 | d43277c5 | Blue Swirl | |
94 | d43277c5 | Blue Swirl | for(i = 0; i < max_len; i++) { |
95 | d43277c5 | Blue Swirl | if (s[i] == '\0') { |
96 | d43277c5 | Blue Swirl | break;
|
97 | d43277c5 | Blue Swirl | } |
98 | d43277c5 | Blue Swirl | } |
99 | d43277c5 | Blue Swirl | return i;
|
100 | d43277c5 | Blue Swirl | } |
101 | d43277c5 | Blue Swirl | |
102 | 3c6b2088 | bellard | time_t mktimegm(struct tm *tm)
|
103 | 3c6b2088 | bellard | { |
104 | 3c6b2088 | bellard | time_t t; |
105 | 3c6b2088 | bellard | int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday; |
106 | 3c6b2088 | bellard | if (m < 3) { |
107 | 3c6b2088 | bellard | m += 12;
|
108 | 3c6b2088 | bellard | y--; |
109 | 3c6b2088 | bellard | } |
110 | 3c6b2088 | bellard | t = 86400 * (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + |
111 | 3c6b2088 | bellard | y / 400 - 719469); |
112 | 3c6b2088 | bellard | t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec; |
113 | 3c6b2088 | bellard | return t;
|
114 | 3c6b2088 | bellard | } |
115 | b39ade83 | aliguori | |
116 | ad46db9a | blueswir1 | int qemu_fls(int i) |
117 | b39ade83 | aliguori | { |
118 | 8d371d4b | aliguori | return 32 - clz32(i); |
119 | b39ade83 | aliguori | } |
120 | 44e3ee8a | aliguori | |
121 | 6f1953c4 | Christoph Hellwig | /*
|
122 | 6f1953c4 | Christoph Hellwig | * Make sure data goes on disk, but if possible do not bother to
|
123 | 6f1953c4 | Christoph Hellwig | * write out the inode just for timestamp updates.
|
124 | 6f1953c4 | Christoph Hellwig | *
|
125 | 6f1953c4 | Christoph Hellwig | * Unfortunately even in 2009 many operating systems do not support
|
126 | 6f1953c4 | Christoph Hellwig | * fdatasync and have to fall back to fsync.
|
127 | 6f1953c4 | Christoph Hellwig | */
|
128 | 6f1953c4 | Christoph Hellwig | int qemu_fdatasync(int fd) |
129 | 6f1953c4 | Christoph Hellwig | { |
130 | 5f6b9e8f | Blue Swirl | #ifdef CONFIG_FDATASYNC
|
131 | 6f1953c4 | Christoph Hellwig | return fdatasync(fd);
|
132 | 6f1953c4 | Christoph Hellwig | #else
|
133 | 6f1953c4 | Christoph Hellwig | return fsync(fd);
|
134 | 6f1953c4 | Christoph Hellwig | #endif
|
135 | 6f1953c4 | Christoph Hellwig | } |
136 | 6f1953c4 | Christoph Hellwig | |
137 | 44e3ee8a | aliguori | /* io vectors */
|
138 | 44e3ee8a | aliguori | |
139 | 44e3ee8a | aliguori | void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint) |
140 | 44e3ee8a | aliguori | { |
141 | 7267c094 | Anthony Liguori | qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec)); |
142 | 44e3ee8a | aliguori | qiov->niov = 0;
|
143 | 44e3ee8a | aliguori | qiov->nalloc = alloc_hint; |
144 | 249aa745 | aliguori | qiov->size = 0;
|
145 | 44e3ee8a | aliguori | } |
146 | 44e3ee8a | aliguori | |
147 | 522584a5 | aliguori | void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov) |
148 | 522584a5 | aliguori | { |
149 | 522584a5 | aliguori | int i;
|
150 | 522584a5 | aliguori | |
151 | 522584a5 | aliguori | qiov->iov = iov; |
152 | 522584a5 | aliguori | qiov->niov = niov; |
153 | 522584a5 | aliguori | qiov->nalloc = -1;
|
154 | 522584a5 | aliguori | qiov->size = 0;
|
155 | 522584a5 | aliguori | for (i = 0; i < niov; i++) |
156 | 522584a5 | aliguori | qiov->size += iov[i].iov_len; |
157 | 522584a5 | aliguori | } |
158 | 522584a5 | aliguori | |
159 | 44e3ee8a | aliguori | void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) |
160 | 44e3ee8a | aliguori | { |
161 | 522584a5 | aliguori | assert(qiov->nalloc != -1);
|
162 | 522584a5 | aliguori | |
163 | 44e3ee8a | aliguori | if (qiov->niov == qiov->nalloc) {
|
164 | 44e3ee8a | aliguori | qiov->nalloc = 2 * qiov->nalloc + 1; |
165 | 7267c094 | Anthony Liguori | qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec)); |
166 | 44e3ee8a | aliguori | } |
167 | 44e3ee8a | aliguori | qiov->iov[qiov->niov].iov_base = base; |
168 | 44e3ee8a | aliguori | qiov->iov[qiov->niov].iov_len = len; |
169 | 249aa745 | aliguori | qiov->size += len; |
170 | 44e3ee8a | aliguori | ++qiov->niov; |
171 | 44e3ee8a | aliguori | } |
172 | 44e3ee8a | aliguori | |
173 | 40b4f539 | Kevin Wolf | /*
|
174 | b8a83a4f | Kevin Wolf | * Copies iovecs from src to the end of dst. It starts copying after skipping
|
175 | b8a83a4f | Kevin Wolf | * the given number of bytes in src and copies until src is completely copied
|
176 | b8a83a4f | Kevin Wolf | * or the total size of the copied iovec reaches size.The size of the last
|
177 | b8a83a4f | Kevin Wolf | * copied iovec is changed in order to fit the specified total size if it isn't
|
178 | b8a83a4f | Kevin Wolf | * a perfect fit already.
|
179 | 40b4f539 | Kevin Wolf | */
|
180 | b8a83a4f | Kevin Wolf | void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
|
181 | b8a83a4f | Kevin Wolf | size_t size) |
182 | 40b4f539 | Kevin Wolf | { |
183 | 40b4f539 | Kevin Wolf | int i;
|
184 | 40b4f539 | Kevin Wolf | size_t done; |
185 | b8a83a4f | Kevin Wolf | void *iov_base;
|
186 | b8a83a4f | Kevin Wolf | uint64_t iov_len; |
187 | 40b4f539 | Kevin Wolf | |
188 | 40b4f539 | Kevin Wolf | assert(dst->nalloc != -1);
|
189 | 40b4f539 | Kevin Wolf | |
190 | 40b4f539 | Kevin Wolf | done = 0;
|
191 | 40b4f539 | Kevin Wolf | for (i = 0; (i < src->niov) && (done != size); i++) { |
192 | b8a83a4f | Kevin Wolf | if (skip >= src->iov[i].iov_len) {
|
193 | b8a83a4f | Kevin Wolf | /* Skip the whole iov */
|
194 | b8a83a4f | Kevin Wolf | skip -= src->iov[i].iov_len; |
195 | b8a83a4f | Kevin Wolf | continue;
|
196 | b8a83a4f | Kevin Wolf | } else {
|
197 | b8a83a4f | Kevin Wolf | /* Skip only part (or nothing) of the iov */
|
198 | b8a83a4f | Kevin Wolf | iov_base = (uint8_t*) src->iov[i].iov_base + skip; |
199 | b8a83a4f | Kevin Wolf | iov_len = src->iov[i].iov_len - skip; |
200 | b8a83a4f | Kevin Wolf | skip = 0;
|
201 | b8a83a4f | Kevin Wolf | } |
202 | b8a83a4f | Kevin Wolf | |
203 | b8a83a4f | Kevin Wolf | if (done + iov_len > size) {
|
204 | b8a83a4f | Kevin Wolf | qemu_iovec_add(dst, iov_base, size - done); |
205 | 40b4f539 | Kevin Wolf | break;
|
206 | 40b4f539 | Kevin Wolf | } else {
|
207 | b8a83a4f | Kevin Wolf | qemu_iovec_add(dst, iov_base, iov_len); |
208 | 40b4f539 | Kevin Wolf | } |
209 | b8a83a4f | Kevin Wolf | done += iov_len; |
210 | 40b4f539 | Kevin Wolf | } |
211 | 40b4f539 | Kevin Wolf | } |
212 | 40b4f539 | Kevin Wolf | |
213 | b8a83a4f | Kevin Wolf | void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size)
|
214 | b8a83a4f | Kevin Wolf | { |
215 | b8a83a4f | Kevin Wolf | qemu_iovec_copy(dst, src, 0, size);
|
216 | b8a83a4f | Kevin Wolf | } |
217 | b8a83a4f | Kevin Wolf | |
218 | 44e3ee8a | aliguori | void qemu_iovec_destroy(QEMUIOVector *qiov)
|
219 | 44e3ee8a | aliguori | { |
220 | 522584a5 | aliguori | assert(qiov->nalloc != -1);
|
221 | 522584a5 | aliguori | |
222 | bd83b362 | Paolo Bonzini | qemu_iovec_reset(qiov); |
223 | 7267c094 | Anthony Liguori | g_free(qiov->iov); |
224 | bd83b362 | Paolo Bonzini | qiov->nalloc = 0;
|
225 | bd83b362 | Paolo Bonzini | qiov->iov = NULL;
|
226 | 44e3ee8a | aliguori | } |
227 | 44e3ee8a | aliguori | |
228 | be959463 | aliguori | void qemu_iovec_reset(QEMUIOVector *qiov)
|
229 | be959463 | aliguori | { |
230 | 522584a5 | aliguori | assert(qiov->nalloc != -1);
|
231 | 522584a5 | aliguori | |
232 | be959463 | aliguori | qiov->niov = 0;
|
233 | be959463 | aliguori | qiov->size = 0;
|
234 | be959463 | aliguori | } |
235 | be959463 | aliguori | |
236 | 44e3ee8a | aliguori | void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf) |
237 | 44e3ee8a | aliguori | { |
238 | 44e3ee8a | aliguori | uint8_t *p = (uint8_t *)buf; |
239 | 44e3ee8a | aliguori | int i;
|
240 | 44e3ee8a | aliguori | |
241 | 44e3ee8a | aliguori | for (i = 0; i < qiov->niov; ++i) { |
242 | 44e3ee8a | aliguori | memcpy(p, qiov->iov[i].iov_base, qiov->iov[i].iov_len); |
243 | 44e3ee8a | aliguori | p += qiov->iov[i].iov_len; |
244 | 44e3ee8a | aliguori | } |
245 | 44e3ee8a | aliguori | } |
246 | 44e3ee8a | aliguori | |
247 | 249aa745 | aliguori | void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count) |
248 | 44e3ee8a | aliguori | { |
249 | 44e3ee8a | aliguori | const uint8_t *p = (const uint8_t *)buf; |
250 | 249aa745 | aliguori | size_t copy; |
251 | 44e3ee8a | aliguori | int i;
|
252 | 44e3ee8a | aliguori | |
253 | 249aa745 | aliguori | for (i = 0; i < qiov->niov && count; ++i) { |
254 | 249aa745 | aliguori | copy = count; |
255 | 249aa745 | aliguori | if (copy > qiov->iov[i].iov_len)
|
256 | 249aa745 | aliguori | copy = qiov->iov[i].iov_len; |
257 | 249aa745 | aliguori | memcpy(qiov->iov[i].iov_base, p, copy); |
258 | 249aa745 | aliguori | p += copy; |
259 | 249aa745 | aliguori | count -= copy; |
260 | 44e3ee8a | aliguori | } |
261 | 44e3ee8a | aliguori | } |
262 | db1a4972 | Paolo Bonzini | |
263 | b8a83a4f | Kevin Wolf | void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count) |
264 | b8a83a4f | Kevin Wolf | { |
265 | b8a83a4f | Kevin Wolf | size_t n; |
266 | b8a83a4f | Kevin Wolf | int i;
|
267 | b8a83a4f | Kevin Wolf | |
268 | b8a83a4f | Kevin Wolf | for (i = 0; i < qiov->niov && count; ++i) { |
269 | b8a83a4f | Kevin Wolf | n = MIN(count, qiov->iov[i].iov_len); |
270 | b8a83a4f | Kevin Wolf | memset(qiov->iov[i].iov_base, c, n); |
271 | b8a83a4f | Kevin Wolf | count -= n; |
272 | b8a83a4f | Kevin Wolf | } |
273 | b8a83a4f | Kevin Wolf | } |
274 | b8a83a4f | Kevin Wolf | |
275 | e0d9c6f9 | Chunqiang Tang | void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count, |
276 | e0d9c6f9 | Chunqiang Tang | size_t skip) |
277 | e0d9c6f9 | Chunqiang Tang | { |
278 | e0d9c6f9 | Chunqiang Tang | int i;
|
279 | e0d9c6f9 | Chunqiang Tang | size_t done; |
280 | e0d9c6f9 | Chunqiang Tang | void *iov_base;
|
281 | e0d9c6f9 | Chunqiang Tang | uint64_t iov_len; |
282 | e0d9c6f9 | Chunqiang Tang | |
283 | e0d9c6f9 | Chunqiang Tang | done = 0;
|
284 | e0d9c6f9 | Chunqiang Tang | for (i = 0; (i < qiov->niov) && (done != count); i++) { |
285 | e0d9c6f9 | Chunqiang Tang | if (skip >= qiov->iov[i].iov_len) {
|
286 | e0d9c6f9 | Chunqiang Tang | /* Skip the whole iov */
|
287 | e0d9c6f9 | Chunqiang Tang | skip -= qiov->iov[i].iov_len; |
288 | e0d9c6f9 | Chunqiang Tang | continue;
|
289 | e0d9c6f9 | Chunqiang Tang | } else {
|
290 | e0d9c6f9 | Chunqiang Tang | /* Skip only part (or nothing) of the iov */
|
291 | e0d9c6f9 | Chunqiang Tang | iov_base = (uint8_t*) qiov->iov[i].iov_base + skip; |
292 | e0d9c6f9 | Chunqiang Tang | iov_len = qiov->iov[i].iov_len - skip; |
293 | e0d9c6f9 | Chunqiang Tang | skip = 0;
|
294 | e0d9c6f9 | Chunqiang Tang | } |
295 | e0d9c6f9 | Chunqiang Tang | |
296 | e0d9c6f9 | Chunqiang Tang | if (done + iov_len > count) {
|
297 | e0d9c6f9 | Chunqiang Tang | memset(iov_base, c, count - done); |
298 | e0d9c6f9 | Chunqiang Tang | break;
|
299 | e0d9c6f9 | Chunqiang Tang | } else {
|
300 | e0d9c6f9 | Chunqiang Tang | memset(iov_base, c, iov_len); |
301 | e0d9c6f9 | Chunqiang Tang | } |
302 | e0d9c6f9 | Chunqiang Tang | done += iov_len; |
303 | e0d9c6f9 | Chunqiang Tang | } |
304 | e0d9c6f9 | Chunqiang Tang | } |
305 | e0d9c6f9 | Chunqiang Tang | |
306 | 1a6d39fd | Stefan Hajnoczi | /*
|
307 | 1a6d39fd | Stefan Hajnoczi | * Checks if a buffer is all zeroes
|
308 | 1a6d39fd | Stefan Hajnoczi | *
|
309 | 1a6d39fd | Stefan Hajnoczi | * Attention! The len must be a multiple of 4 * sizeof(long) due to
|
310 | 1a6d39fd | Stefan Hajnoczi | * restriction of optimizations in this function.
|
311 | 1a6d39fd | Stefan Hajnoczi | */
|
312 | 1a6d39fd | Stefan Hajnoczi | bool buffer_is_zero(const void *buf, size_t len) |
313 | 1a6d39fd | Stefan Hajnoczi | { |
314 | 1a6d39fd | Stefan Hajnoczi | /*
|
315 | 1a6d39fd | Stefan Hajnoczi | * Use long as the biggest available internal data type that fits into the
|
316 | 1a6d39fd | Stefan Hajnoczi | * CPU register and unroll the loop to smooth out the effect of memory
|
317 | 1a6d39fd | Stefan Hajnoczi | * latency.
|
318 | 1a6d39fd | Stefan Hajnoczi | */
|
319 | 1a6d39fd | Stefan Hajnoczi | |
320 | 1a6d39fd | Stefan Hajnoczi | size_t i; |
321 | 1a6d39fd | Stefan Hajnoczi | long d0, d1, d2, d3;
|
322 | 1a6d39fd | Stefan Hajnoczi | const long * const data = buf; |
323 | 1a6d39fd | Stefan Hajnoczi | |
324 | 1a6d39fd | Stefan Hajnoczi | assert(len % (4 * sizeof(long)) == 0); |
325 | 1a6d39fd | Stefan Hajnoczi | len /= sizeof(long); |
326 | 1a6d39fd | Stefan Hajnoczi | |
327 | 1a6d39fd | Stefan Hajnoczi | for (i = 0; i < len; i += 4) { |
328 | 1a6d39fd | Stefan Hajnoczi | d0 = data[i + 0];
|
329 | 1a6d39fd | Stefan Hajnoczi | d1 = data[i + 1];
|
330 | 1a6d39fd | Stefan Hajnoczi | d2 = data[i + 2];
|
331 | 1a6d39fd | Stefan Hajnoczi | d3 = data[i + 3];
|
332 | 1a6d39fd | Stefan Hajnoczi | |
333 | 1a6d39fd | Stefan Hajnoczi | if (d0 || d1 || d2 || d3) {
|
334 | 1a6d39fd | Stefan Hajnoczi | return false; |
335 | 1a6d39fd | Stefan Hajnoczi | } |
336 | 1a6d39fd | Stefan Hajnoczi | } |
337 | 1a6d39fd | Stefan Hajnoczi | |
338 | 1a6d39fd | Stefan Hajnoczi | return true; |
339 | 1a6d39fd | Stefan Hajnoczi | } |
340 | 1a6d39fd | Stefan Hajnoczi | |
341 | db1a4972 | Paolo Bonzini | #ifndef _WIN32
|
342 | db1a4972 | Paolo Bonzini | /* Sets a specific flag */
|
343 | db1a4972 | Paolo Bonzini | int fcntl_setfl(int fd, int flag) |
344 | db1a4972 | Paolo Bonzini | { |
345 | db1a4972 | Paolo Bonzini | int flags;
|
346 | db1a4972 | Paolo Bonzini | |
347 | db1a4972 | Paolo Bonzini | flags = fcntl(fd, F_GETFL); |
348 | db1a4972 | Paolo Bonzini | if (flags == -1) |
349 | db1a4972 | Paolo Bonzini | return -errno;
|
350 | db1a4972 | Paolo Bonzini | |
351 | db1a4972 | Paolo Bonzini | if (fcntl(fd, F_SETFL, flags | flag) == -1) |
352 | db1a4972 | Paolo Bonzini | return -errno;
|
353 | db1a4972 | Paolo Bonzini | |
354 | db1a4972 | Paolo Bonzini | return 0; |
355 | db1a4972 | Paolo Bonzini | } |
356 | db1a4972 | Paolo Bonzini | #endif
|
357 | db1a4972 | Paolo Bonzini | |
358 | eba90e4e | Markus Armbruster | static int64_t suffix_mul(char suffix, int64_t unit) |
359 | eba90e4e | Markus Armbruster | { |
360 | eba90e4e | Markus Armbruster | switch (qemu_toupper(suffix)) {
|
361 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_B:
|
362 | eba90e4e | Markus Armbruster | return 1; |
363 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_KB:
|
364 | eba90e4e | Markus Armbruster | return unit;
|
365 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_MB:
|
366 | eba90e4e | Markus Armbruster | return unit * unit;
|
367 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_GB:
|
368 | eba90e4e | Markus Armbruster | return unit * unit * unit;
|
369 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_TB:
|
370 | eba90e4e | Markus Armbruster | return unit * unit * unit * unit;
|
371 | eba90e4e | Markus Armbruster | } |
372 | eba90e4e | Markus Armbruster | return -1; |
373 | eba90e4e | Markus Armbruster | } |
374 | eba90e4e | Markus Armbruster | |
375 | 9f9b17a4 | Jes Sorensen | /*
|
376 | 9f9b17a4 | Jes Sorensen | * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
|
377 | 8dddfb55 | Markus Armbruster | * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
|
378 | eba90e4e | Markus Armbruster | * in *end, if not NULL. Return -1 on error.
|
379 | 9f9b17a4 | Jes Sorensen | */
|
380 | a732e1ba | Joerg Roedel | int64_t strtosz_suffix_unit(const char *nptr, char **end, |
381 | a732e1ba | Joerg Roedel | const char default_suffix, int64_t unit) |
382 | 9f9b17a4 | Jes Sorensen | { |
383 | 70b4f4bb | Jes Sorensen | int64_t retval = -1;
|
384 | f3bd362a | Jes Sorensen | char *endptr;
|
385 | eba90e4e | Markus Armbruster | unsigned char c; |
386 | 9f9b17a4 | Jes Sorensen | int mul_required = 0; |
387 | 9f9b17a4 | Jes Sorensen | double val, mul, integral, fraction;
|
388 | 9f9b17a4 | Jes Sorensen | |
389 | 9f9b17a4 | Jes Sorensen | errno = 0;
|
390 | 9f9b17a4 | Jes Sorensen | val = strtod(nptr, &endptr); |
391 | 9f9b17a4 | Jes Sorensen | if (isnan(val) || endptr == nptr || errno != 0) { |
392 | 9f9b17a4 | Jes Sorensen | goto fail;
|
393 | 9f9b17a4 | Jes Sorensen | } |
394 | 7eb05349 | Jes Sorensen | fraction = modf(val, &integral); |
395 | 7eb05349 | Jes Sorensen | if (fraction != 0) { |
396 | 9f9b17a4 | Jes Sorensen | mul_required = 1;
|
397 | 9f9b17a4 | Jes Sorensen | } |
398 | 9f9b17a4 | Jes Sorensen | c = *endptr; |
399 | eba90e4e | Markus Armbruster | mul = suffix_mul(c, unit); |
400 | eba90e4e | Markus Armbruster | if (mul >= 0) { |
401 | eba90e4e | Markus Armbruster | endptr++; |
402 | eba90e4e | Markus Armbruster | } else {
|
403 | eba90e4e | Markus Armbruster | mul = suffix_mul(default_suffix, unit); |
404 | eba90e4e | Markus Armbruster | assert(mul >= 0);
|
405 | 9f9b17a4 | Jes Sorensen | } |
406 | eba90e4e | Markus Armbruster | if (mul == 1 && mul_required) { |
407 | 9f9b17a4 | Jes Sorensen | goto fail;
|
408 | 9f9b17a4 | Jes Sorensen | } |
409 | 70b4f4bb | Jes Sorensen | if ((val * mul >= INT64_MAX) || val < 0) { |
410 | 9f9b17a4 | Jes Sorensen | goto fail;
|
411 | 9f9b17a4 | Jes Sorensen | } |
412 | 9f9b17a4 | Jes Sorensen | retval = val * mul; |
413 | 9f9b17a4 | Jes Sorensen | |
414 | 9f9b17a4 | Jes Sorensen | fail:
|
415 | 9f9b17a4 | Jes Sorensen | if (end) {
|
416 | 9f9b17a4 | Jes Sorensen | *end = endptr; |
417 | 9f9b17a4 | Jes Sorensen | } |
418 | 9f9b17a4 | Jes Sorensen | |
419 | 9f9b17a4 | Jes Sorensen | return retval;
|
420 | 9f9b17a4 | Jes Sorensen | } |
421 | d8427002 | Jes Sorensen | |
422 | a732e1ba | Joerg Roedel | int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) |
423 | a732e1ba | Joerg Roedel | { |
424 | fdc9c41a | Jan Kiszka | return strtosz_suffix_unit(nptr, end, default_suffix, 1024); |
425 | a732e1ba | Joerg Roedel | } |
426 | a732e1ba | Joerg Roedel | |
427 | 70b4f4bb | Jes Sorensen | int64_t strtosz(const char *nptr, char **end) |
428 | d8427002 | Jes Sorensen | { |
429 | d8427002 | Jes Sorensen | return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
|
430 | d8427002 | Jes Sorensen | } |
431 | 443916d1 | Stefan Berger | |
432 | 443916d1 | Stefan Berger | int qemu_parse_fd(const char *param) |
433 | 443916d1 | Stefan Berger | { |
434 | 443916d1 | Stefan Berger | int fd;
|
435 | 443916d1 | Stefan Berger | char *endptr = NULL; |
436 | 443916d1 | Stefan Berger | |
437 | 443916d1 | Stefan Berger | fd = strtol(param, &endptr, 10);
|
438 | 443916d1 | Stefan Berger | if (*endptr || (fd == 0 && param == endptr)) { |
439 | 443916d1 | Stefan Berger | return -1; |
440 | 443916d1 | Stefan Berger | } |
441 | 443916d1 | Stefan Berger | return fd;
|
442 | 443916d1 | Stefan Berger | } |
443 | 8c5135f9 | Paolo Bonzini | |
444 | 8c5135f9 | Paolo Bonzini | /*
|
445 | 8c5135f9 | Paolo Bonzini | * Send/recv data with iovec buffers
|
446 | 8c5135f9 | Paolo Bonzini | *
|
447 | 8c5135f9 | Paolo Bonzini | * This function send/recv data from/to the iovec buffer directly.
|
448 | 8c5135f9 | Paolo Bonzini | * The first `offset' bytes in the iovec buffer are skipped and next
|
449 | 8c5135f9 | Paolo Bonzini | * `len' bytes are used.
|
450 | 8c5135f9 | Paolo Bonzini | *
|
451 | 8c5135f9 | Paolo Bonzini | * For example,
|
452 | 8c5135f9 | Paolo Bonzini | *
|
453 | 8c5135f9 | Paolo Bonzini | * do_sendv_recvv(sockfd, iov, len, offset, 1);
|
454 | 8c5135f9 | Paolo Bonzini | *
|
455 | 8c5135f9 | Paolo Bonzini | * is equal to
|
456 | 8c5135f9 | Paolo Bonzini | *
|
457 | 8c5135f9 | Paolo Bonzini | * char *buf = malloc(size);
|
458 | 8c5135f9 | Paolo Bonzini | * iov_to_buf(iov, iovcnt, buf, offset, size);
|
459 | 8c5135f9 | Paolo Bonzini | * send(sockfd, buf, size, 0);
|
460 | 8c5135f9 | Paolo Bonzini | * free(buf);
|
461 | 8c5135f9 | Paolo Bonzini | */
|
462 | 8c5135f9 | Paolo Bonzini | static int do_sendv_recvv(int sockfd, struct iovec *iov, int len, int offset, |
463 | 8c5135f9 | Paolo Bonzini | int do_sendv)
|
464 | 8c5135f9 | Paolo Bonzini | { |
465 | 8c5135f9 | Paolo Bonzini | int ret, diff, iovlen;
|
466 | 8c5135f9 | Paolo Bonzini | struct iovec *last_iov;
|
467 | 8c5135f9 | Paolo Bonzini | |
468 | 8c5135f9 | Paolo Bonzini | /* last_iov is inclusive, so count from one. */
|
469 | 8c5135f9 | Paolo Bonzini | iovlen = 1;
|
470 | 8c5135f9 | Paolo Bonzini | last_iov = iov; |
471 | 8c5135f9 | Paolo Bonzini | len += offset; |
472 | 8c5135f9 | Paolo Bonzini | |
473 | 8c5135f9 | Paolo Bonzini | while (last_iov->iov_len < len) {
|
474 | 8c5135f9 | Paolo Bonzini | len -= last_iov->iov_len; |
475 | 8c5135f9 | Paolo Bonzini | |
476 | 8c5135f9 | Paolo Bonzini | last_iov++; |
477 | 8c5135f9 | Paolo Bonzini | iovlen++; |
478 | 8c5135f9 | Paolo Bonzini | } |
479 | 8c5135f9 | Paolo Bonzini | |
480 | 8c5135f9 | Paolo Bonzini | diff = last_iov->iov_len - len; |
481 | 8c5135f9 | Paolo Bonzini | last_iov->iov_len -= diff; |
482 | 8c5135f9 | Paolo Bonzini | |
483 | 8c5135f9 | Paolo Bonzini | while (iov->iov_len <= offset) {
|
484 | 8c5135f9 | Paolo Bonzini | offset -= iov->iov_len; |
485 | 8c5135f9 | Paolo Bonzini | |
486 | 8c5135f9 | Paolo Bonzini | iov++; |
487 | 8c5135f9 | Paolo Bonzini | iovlen--; |
488 | 8c5135f9 | Paolo Bonzini | } |
489 | 8c5135f9 | Paolo Bonzini | |
490 | 8c5135f9 | Paolo Bonzini | iov->iov_base = (char *) iov->iov_base + offset;
|
491 | 8c5135f9 | Paolo Bonzini | iov->iov_len -= offset; |
492 | 8c5135f9 | Paolo Bonzini | |
493 | 8c5135f9 | Paolo Bonzini | { |
494 | 8c5135f9 | Paolo Bonzini | #if defined CONFIG_IOVEC && defined CONFIG_POSIX
|
495 | 8c5135f9 | Paolo Bonzini | struct msghdr msg;
|
496 | 8c5135f9 | Paolo Bonzini | memset(&msg, 0, sizeof(msg)); |
497 | 8c5135f9 | Paolo Bonzini | msg.msg_iov = iov; |
498 | 8c5135f9 | Paolo Bonzini | msg.msg_iovlen = iovlen; |
499 | 8c5135f9 | Paolo Bonzini | |
500 | 8c5135f9 | Paolo Bonzini | do {
|
501 | 8c5135f9 | Paolo Bonzini | if (do_sendv) {
|
502 | 8c5135f9 | Paolo Bonzini | ret = sendmsg(sockfd, &msg, 0);
|
503 | 8c5135f9 | Paolo Bonzini | } else {
|
504 | 8c5135f9 | Paolo Bonzini | ret = recvmsg(sockfd, &msg, 0);
|
505 | 8c5135f9 | Paolo Bonzini | } |
506 | 8c5135f9 | Paolo Bonzini | } while (ret == -1 && errno == EINTR); |
507 | 8c5135f9 | Paolo Bonzini | #else
|
508 | 8c5135f9 | Paolo Bonzini | struct iovec *p = iov;
|
509 | 8c5135f9 | Paolo Bonzini | ret = 0;
|
510 | 8c5135f9 | Paolo Bonzini | while (iovlen > 0) { |
511 | 8c5135f9 | Paolo Bonzini | int rc;
|
512 | 8c5135f9 | Paolo Bonzini | if (do_sendv) {
|
513 | 8c5135f9 | Paolo Bonzini | rc = send(sockfd, p->iov_base, p->iov_len, 0);
|
514 | 8c5135f9 | Paolo Bonzini | } else {
|
515 | 8c5135f9 | Paolo Bonzini | rc = qemu_recv(sockfd, p->iov_base, p->iov_len, 0);
|
516 | 8c5135f9 | Paolo Bonzini | } |
517 | 8c5135f9 | Paolo Bonzini | if (rc == -1) { |
518 | 8c5135f9 | Paolo Bonzini | if (errno == EINTR) {
|
519 | 8c5135f9 | Paolo Bonzini | continue;
|
520 | 8c5135f9 | Paolo Bonzini | } |
521 | 8c5135f9 | Paolo Bonzini | if (ret == 0) { |
522 | 8c5135f9 | Paolo Bonzini | ret = -1;
|
523 | 8c5135f9 | Paolo Bonzini | } |
524 | 8c5135f9 | Paolo Bonzini | break;
|
525 | 8c5135f9 | Paolo Bonzini | } |
526 | 8c5135f9 | Paolo Bonzini | if (rc == 0) { |
527 | 8c5135f9 | Paolo Bonzini | break;
|
528 | 8c5135f9 | Paolo Bonzini | } |
529 | 8c5135f9 | Paolo Bonzini | ret += rc; |
530 | 8c5135f9 | Paolo Bonzini | iovlen--, p++; |
531 | 8c5135f9 | Paolo Bonzini | } |
532 | 8c5135f9 | Paolo Bonzini | #endif
|
533 | 8c5135f9 | Paolo Bonzini | } |
534 | 8c5135f9 | Paolo Bonzini | |
535 | 8c5135f9 | Paolo Bonzini | /* Undo the changes above */
|
536 | 8c5135f9 | Paolo Bonzini | iov->iov_base = (char *) iov->iov_base - offset;
|
537 | 8c5135f9 | Paolo Bonzini | iov->iov_len += offset; |
538 | 8c5135f9 | Paolo Bonzini | last_iov->iov_len += diff; |
539 | 8c5135f9 | Paolo Bonzini | return ret;
|
540 | 8c5135f9 | Paolo Bonzini | } |
541 | 8c5135f9 | Paolo Bonzini | |
542 | 8c5135f9 | Paolo Bonzini | int qemu_recvv(int sockfd, struct iovec *iov, int len, int iov_offset) |
543 | 8c5135f9 | Paolo Bonzini | { |
544 | 8c5135f9 | Paolo Bonzini | return do_sendv_recvv(sockfd, iov, len, iov_offset, 0); |
545 | 8c5135f9 | Paolo Bonzini | } |
546 | 8c5135f9 | Paolo Bonzini | |
547 | 8c5135f9 | Paolo Bonzini | int qemu_sendv(int sockfd, struct iovec *iov, int len, int iov_offset) |
548 | 8c5135f9 | Paolo Bonzini | { |
549 | 8c5135f9 | Paolo Bonzini | return do_sendv_recvv(sockfd, iov, len, iov_offset, 1); |
550 | 8c5135f9 | Paolo Bonzini | } |