root / cutils.c @ 079d0b7f
History | View | Annotate | Download (12 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 | db1a4972 | Paolo Bonzini | #ifndef _WIN32
|
307 | db1a4972 | Paolo Bonzini | /* Sets a specific flag */
|
308 | db1a4972 | Paolo Bonzini | int fcntl_setfl(int fd, int flag) |
309 | db1a4972 | Paolo Bonzini | { |
310 | db1a4972 | Paolo Bonzini | int flags;
|
311 | db1a4972 | Paolo Bonzini | |
312 | db1a4972 | Paolo Bonzini | flags = fcntl(fd, F_GETFL); |
313 | db1a4972 | Paolo Bonzini | if (flags == -1) |
314 | db1a4972 | Paolo Bonzini | return -errno;
|
315 | db1a4972 | Paolo Bonzini | |
316 | db1a4972 | Paolo Bonzini | if (fcntl(fd, F_SETFL, flags | flag) == -1) |
317 | db1a4972 | Paolo Bonzini | return -errno;
|
318 | db1a4972 | Paolo Bonzini | |
319 | db1a4972 | Paolo Bonzini | return 0; |
320 | db1a4972 | Paolo Bonzini | } |
321 | db1a4972 | Paolo Bonzini | #endif
|
322 | db1a4972 | Paolo Bonzini | |
323 | eba90e4e | Markus Armbruster | static int64_t suffix_mul(char suffix, int64_t unit) |
324 | eba90e4e | Markus Armbruster | { |
325 | eba90e4e | Markus Armbruster | switch (qemu_toupper(suffix)) {
|
326 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_B:
|
327 | eba90e4e | Markus Armbruster | return 1; |
328 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_KB:
|
329 | eba90e4e | Markus Armbruster | return unit;
|
330 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_MB:
|
331 | eba90e4e | Markus Armbruster | return unit * unit;
|
332 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_GB:
|
333 | eba90e4e | Markus Armbruster | return unit * unit * unit;
|
334 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_TB:
|
335 | eba90e4e | Markus Armbruster | return unit * unit * unit * unit;
|
336 | eba90e4e | Markus Armbruster | } |
337 | eba90e4e | Markus Armbruster | return -1; |
338 | eba90e4e | Markus Armbruster | } |
339 | eba90e4e | Markus Armbruster | |
340 | 9f9b17a4 | Jes Sorensen | /*
|
341 | 9f9b17a4 | Jes Sorensen | * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
|
342 | 8dddfb55 | Markus Armbruster | * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
|
343 | eba90e4e | Markus Armbruster | * in *end, if not NULL. Return -1 on error.
|
344 | 9f9b17a4 | Jes Sorensen | */
|
345 | a732e1ba | Joerg Roedel | int64_t strtosz_suffix_unit(const char *nptr, char **end, |
346 | a732e1ba | Joerg Roedel | const char default_suffix, int64_t unit) |
347 | 9f9b17a4 | Jes Sorensen | { |
348 | 70b4f4bb | Jes Sorensen | int64_t retval = -1;
|
349 | f3bd362a | Jes Sorensen | char *endptr;
|
350 | eba90e4e | Markus Armbruster | unsigned char c; |
351 | 9f9b17a4 | Jes Sorensen | int mul_required = 0; |
352 | 9f9b17a4 | Jes Sorensen | double val, mul, integral, fraction;
|
353 | 9f9b17a4 | Jes Sorensen | |
354 | 9f9b17a4 | Jes Sorensen | errno = 0;
|
355 | 9f9b17a4 | Jes Sorensen | val = strtod(nptr, &endptr); |
356 | 9f9b17a4 | Jes Sorensen | if (isnan(val) || endptr == nptr || errno != 0) { |
357 | 9f9b17a4 | Jes Sorensen | goto fail;
|
358 | 9f9b17a4 | Jes Sorensen | } |
359 | 7eb05349 | Jes Sorensen | fraction = modf(val, &integral); |
360 | 7eb05349 | Jes Sorensen | if (fraction != 0) { |
361 | 9f9b17a4 | Jes Sorensen | mul_required = 1;
|
362 | 9f9b17a4 | Jes Sorensen | } |
363 | 9f9b17a4 | Jes Sorensen | c = *endptr; |
364 | eba90e4e | Markus Armbruster | mul = suffix_mul(c, unit); |
365 | eba90e4e | Markus Armbruster | if (mul >= 0) { |
366 | eba90e4e | Markus Armbruster | endptr++; |
367 | eba90e4e | Markus Armbruster | } else {
|
368 | eba90e4e | Markus Armbruster | mul = suffix_mul(default_suffix, unit); |
369 | eba90e4e | Markus Armbruster | assert(mul >= 0);
|
370 | 9f9b17a4 | Jes Sorensen | } |
371 | eba90e4e | Markus Armbruster | if (mul == 1 && mul_required) { |
372 | 9f9b17a4 | Jes Sorensen | goto fail;
|
373 | 9f9b17a4 | Jes Sorensen | } |
374 | 70b4f4bb | Jes Sorensen | if ((val * mul >= INT64_MAX) || val < 0) { |
375 | 9f9b17a4 | Jes Sorensen | goto fail;
|
376 | 9f9b17a4 | Jes Sorensen | } |
377 | 9f9b17a4 | Jes Sorensen | retval = val * mul; |
378 | 9f9b17a4 | Jes Sorensen | |
379 | 9f9b17a4 | Jes Sorensen | fail:
|
380 | 9f9b17a4 | Jes Sorensen | if (end) {
|
381 | 9f9b17a4 | Jes Sorensen | *end = endptr; |
382 | 9f9b17a4 | Jes Sorensen | } |
383 | 9f9b17a4 | Jes Sorensen | |
384 | 9f9b17a4 | Jes Sorensen | return retval;
|
385 | 9f9b17a4 | Jes Sorensen | } |
386 | d8427002 | Jes Sorensen | |
387 | a732e1ba | Joerg Roedel | int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) |
388 | a732e1ba | Joerg Roedel | { |
389 | fdc9c41a | Jan Kiszka | return strtosz_suffix_unit(nptr, end, default_suffix, 1024); |
390 | a732e1ba | Joerg Roedel | } |
391 | a732e1ba | Joerg Roedel | |
392 | 70b4f4bb | Jes Sorensen | int64_t strtosz(const char *nptr, char **end) |
393 | d8427002 | Jes Sorensen | { |
394 | d8427002 | Jes Sorensen | return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
|
395 | d8427002 | Jes Sorensen | } |
396 | 443916d1 | Stefan Berger | |
397 | 443916d1 | Stefan Berger | int qemu_parse_fd(const char *param) |
398 | 443916d1 | Stefan Berger | { |
399 | 443916d1 | Stefan Berger | int fd;
|
400 | 443916d1 | Stefan Berger | char *endptr = NULL; |
401 | 443916d1 | Stefan Berger | |
402 | 443916d1 | Stefan Berger | fd = strtol(param, &endptr, 10);
|
403 | 443916d1 | Stefan Berger | if (*endptr || (fd == 0 && param == endptr)) { |
404 | 443916d1 | Stefan Berger | return -1; |
405 | 443916d1 | Stefan Berger | } |
406 | 443916d1 | Stefan Berger | return fd;
|
407 | 443916d1 | Stefan Berger | } |
408 | 8c5135f9 | Paolo Bonzini | |
409 | 8c5135f9 | Paolo Bonzini | /*
|
410 | 8c5135f9 | Paolo Bonzini | * Send/recv data with iovec buffers
|
411 | 8c5135f9 | Paolo Bonzini | *
|
412 | 8c5135f9 | Paolo Bonzini | * This function send/recv data from/to the iovec buffer directly.
|
413 | 8c5135f9 | Paolo Bonzini | * The first `offset' bytes in the iovec buffer are skipped and next
|
414 | 8c5135f9 | Paolo Bonzini | * `len' bytes are used.
|
415 | 8c5135f9 | Paolo Bonzini | *
|
416 | 8c5135f9 | Paolo Bonzini | * For example,
|
417 | 8c5135f9 | Paolo Bonzini | *
|
418 | 8c5135f9 | Paolo Bonzini | * do_sendv_recvv(sockfd, iov, len, offset, 1);
|
419 | 8c5135f9 | Paolo Bonzini | *
|
420 | 8c5135f9 | Paolo Bonzini | * is equal to
|
421 | 8c5135f9 | Paolo Bonzini | *
|
422 | 8c5135f9 | Paolo Bonzini | * char *buf = malloc(size);
|
423 | 8c5135f9 | Paolo Bonzini | * iov_to_buf(iov, iovcnt, buf, offset, size);
|
424 | 8c5135f9 | Paolo Bonzini | * send(sockfd, buf, size, 0);
|
425 | 8c5135f9 | Paolo Bonzini | * free(buf);
|
426 | 8c5135f9 | Paolo Bonzini | */
|
427 | 8c5135f9 | Paolo Bonzini | static int do_sendv_recvv(int sockfd, struct iovec *iov, int len, int offset, |
428 | 8c5135f9 | Paolo Bonzini | int do_sendv)
|
429 | 8c5135f9 | Paolo Bonzini | { |
430 | 8c5135f9 | Paolo Bonzini | int ret, diff, iovlen;
|
431 | 8c5135f9 | Paolo Bonzini | struct iovec *last_iov;
|
432 | 8c5135f9 | Paolo Bonzini | |
433 | 8c5135f9 | Paolo Bonzini | /* last_iov is inclusive, so count from one. */
|
434 | 8c5135f9 | Paolo Bonzini | iovlen = 1;
|
435 | 8c5135f9 | Paolo Bonzini | last_iov = iov; |
436 | 8c5135f9 | Paolo Bonzini | len += offset; |
437 | 8c5135f9 | Paolo Bonzini | |
438 | 8c5135f9 | Paolo Bonzini | while (last_iov->iov_len < len) {
|
439 | 8c5135f9 | Paolo Bonzini | len -= last_iov->iov_len; |
440 | 8c5135f9 | Paolo Bonzini | |
441 | 8c5135f9 | Paolo Bonzini | last_iov++; |
442 | 8c5135f9 | Paolo Bonzini | iovlen++; |
443 | 8c5135f9 | Paolo Bonzini | } |
444 | 8c5135f9 | Paolo Bonzini | |
445 | 8c5135f9 | Paolo Bonzini | diff = last_iov->iov_len - len; |
446 | 8c5135f9 | Paolo Bonzini | last_iov->iov_len -= diff; |
447 | 8c5135f9 | Paolo Bonzini | |
448 | 8c5135f9 | Paolo Bonzini | while (iov->iov_len <= offset) {
|
449 | 8c5135f9 | Paolo Bonzini | offset -= iov->iov_len; |
450 | 8c5135f9 | Paolo Bonzini | |
451 | 8c5135f9 | Paolo Bonzini | iov++; |
452 | 8c5135f9 | Paolo Bonzini | iovlen--; |
453 | 8c5135f9 | Paolo Bonzini | } |
454 | 8c5135f9 | Paolo Bonzini | |
455 | 8c5135f9 | Paolo Bonzini | iov->iov_base = (char *) iov->iov_base + offset;
|
456 | 8c5135f9 | Paolo Bonzini | iov->iov_len -= offset; |
457 | 8c5135f9 | Paolo Bonzini | |
458 | 8c5135f9 | Paolo Bonzini | { |
459 | 8c5135f9 | Paolo Bonzini | #if defined CONFIG_IOVEC && defined CONFIG_POSIX
|
460 | 8c5135f9 | Paolo Bonzini | struct msghdr msg;
|
461 | 8c5135f9 | Paolo Bonzini | memset(&msg, 0, sizeof(msg)); |
462 | 8c5135f9 | Paolo Bonzini | msg.msg_iov = iov; |
463 | 8c5135f9 | Paolo Bonzini | msg.msg_iovlen = iovlen; |
464 | 8c5135f9 | Paolo Bonzini | |
465 | 8c5135f9 | Paolo Bonzini | do {
|
466 | 8c5135f9 | Paolo Bonzini | if (do_sendv) {
|
467 | 8c5135f9 | Paolo Bonzini | ret = sendmsg(sockfd, &msg, 0);
|
468 | 8c5135f9 | Paolo Bonzini | } else {
|
469 | 8c5135f9 | Paolo Bonzini | ret = recvmsg(sockfd, &msg, 0);
|
470 | 8c5135f9 | Paolo Bonzini | } |
471 | 8c5135f9 | Paolo Bonzini | } while (ret == -1 && errno == EINTR); |
472 | 8c5135f9 | Paolo Bonzini | #else
|
473 | 8c5135f9 | Paolo Bonzini | struct iovec *p = iov;
|
474 | 8c5135f9 | Paolo Bonzini | ret = 0;
|
475 | 8c5135f9 | Paolo Bonzini | while (iovlen > 0) { |
476 | 8c5135f9 | Paolo Bonzini | int rc;
|
477 | 8c5135f9 | Paolo Bonzini | if (do_sendv) {
|
478 | 8c5135f9 | Paolo Bonzini | rc = send(sockfd, p->iov_base, p->iov_len, 0);
|
479 | 8c5135f9 | Paolo Bonzini | } else {
|
480 | 8c5135f9 | Paolo Bonzini | rc = qemu_recv(sockfd, p->iov_base, p->iov_len, 0);
|
481 | 8c5135f9 | Paolo Bonzini | } |
482 | 8c5135f9 | Paolo Bonzini | if (rc == -1) { |
483 | 8c5135f9 | Paolo Bonzini | if (errno == EINTR) {
|
484 | 8c5135f9 | Paolo Bonzini | continue;
|
485 | 8c5135f9 | Paolo Bonzini | } |
486 | 8c5135f9 | Paolo Bonzini | if (ret == 0) { |
487 | 8c5135f9 | Paolo Bonzini | ret = -1;
|
488 | 8c5135f9 | Paolo Bonzini | } |
489 | 8c5135f9 | Paolo Bonzini | break;
|
490 | 8c5135f9 | Paolo Bonzini | } |
491 | 8c5135f9 | Paolo Bonzini | if (rc == 0) { |
492 | 8c5135f9 | Paolo Bonzini | break;
|
493 | 8c5135f9 | Paolo Bonzini | } |
494 | 8c5135f9 | Paolo Bonzini | ret += rc; |
495 | 8c5135f9 | Paolo Bonzini | iovlen--, p++; |
496 | 8c5135f9 | Paolo Bonzini | } |
497 | 8c5135f9 | Paolo Bonzini | #endif
|
498 | 8c5135f9 | Paolo Bonzini | } |
499 | 8c5135f9 | Paolo Bonzini | |
500 | 8c5135f9 | Paolo Bonzini | /* Undo the changes above */
|
501 | 8c5135f9 | Paolo Bonzini | iov->iov_base = (char *) iov->iov_base - offset;
|
502 | 8c5135f9 | Paolo Bonzini | iov->iov_len += offset; |
503 | 8c5135f9 | Paolo Bonzini | last_iov->iov_len += diff; |
504 | 8c5135f9 | Paolo Bonzini | return ret;
|
505 | 8c5135f9 | Paolo Bonzini | } |
506 | 8c5135f9 | Paolo Bonzini | |
507 | 8c5135f9 | Paolo Bonzini | int qemu_recvv(int sockfd, struct iovec *iov, int len, int iov_offset) |
508 | 8c5135f9 | Paolo Bonzini | { |
509 | 8c5135f9 | Paolo Bonzini | return do_sendv_recvv(sockfd, iov, len, iov_offset, 0); |
510 | 8c5135f9 | Paolo Bonzini | } |
511 | 8c5135f9 | Paolo Bonzini | |
512 | 8c5135f9 | Paolo Bonzini | int qemu_sendv(int sockfd, struct iovec *iov, int len, int iov_offset) |
513 | 8c5135f9 | Paolo Bonzini | { |
514 | 8c5135f9 | Paolo Bonzini | return do_sendv_recvv(sockfd, iov, len, iov_offset, 1); |
515 | 8c5135f9 | Paolo Bonzini | } |