blob: a6ffd4644562462b38faa7c9ed79e4a5dc361c99 [file] [log] [blame]
bellard18607dc2007-01-07 22:04:40 +00001/*
2 * Simple C functions to supplement the C library
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard18607dc2007-01-07 22:04:40 +00004 * Copyright (c) 2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
pbrookfaf07962007-11-11 02:51:17 +000024#include "qemu-common.h"
aliguori8d371d42008-12-04 20:08:06 +000025#include "host-utils.h"
Jes Sorensen9f9b17a2010-10-21 17:15:46 +020026#include <math.h>
bellard18607dc2007-01-07 22:04:40 +000027
Paolo Bonzini8c5135f2011-09-08 13:46:25 +020028#include "qemu_socket.h"
29
bellard18607dc2007-01-07 22:04:40 +000030void pstrcpy(char *buf, int buf_size, const char *str)
31{
32 int c;
33 char *q = buf;
34
35 if (buf_size <= 0)
36 return;
37
38 for(;;) {
39 c = *str++;
40 if (c == 0 || q >= buf + buf_size - 1)
41 break;
42 *q++ = c;
43 }
44 *q = '\0';
45}
46
47/* strcat and truncate. */
48char *pstrcat(char *buf, int buf_size, const char *s)
49{
50 int len;
51 len = strlen(buf);
ths5fafdf22007-09-16 21:08:06 +000052 if (len < buf_size)
bellard18607dc2007-01-07 22:04:40 +000053 pstrcpy(buf + len, buf_size - len, s);
54 return buf;
55}
56
57int strstart(const char *str, const char *val, const char **ptr)
58{
59 const char *p, *q;
60 p = str;
61 q = val;
62 while (*q != '\0') {
63 if (*p != *q)
64 return 0;
65 p++;
66 q++;
67 }
68 if (ptr)
69 *ptr = p;
70 return 1;
71}
72
73int stristart(const char *str, const char *val, const char **ptr)
74{
75 const char *p, *q;
76 p = str;
77 q = val;
78 while (*q != '\0') {
blueswir1cd390082008-11-16 13:53:32 +000079 if (qemu_toupper(*p) != qemu_toupper(*q))
bellard18607dc2007-01-07 22:04:40 +000080 return 0;
81 p++;
82 q++;
83 }
84 if (ptr)
85 *ptr = p;
86 return 1;
87}
bellard3c6b2082007-11-10 19:36:39 +000088
Blue Swirld43277c2009-07-01 18:24:44 +000089/* XXX: use host strnlen if available ? */
90int qemu_strnlen(const char *s, int max_len)
91{
92 int i;
93
94 for(i = 0; i < max_len; i++) {
95 if (s[i] == '\0') {
96 break;
97 }
98 }
99 return i;
100}
101
bellard3c6b2082007-11-10 19:36:39 +0000102time_t mktimegm(struct tm *tm)
103{
104 time_t t;
105 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
106 if (m < 3) {
107 m += 12;
108 y--;
109 }
110 t = 86400 * (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 +
111 y / 400 - 719469);
112 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
113 return t;
114}
aliguorib39ade82008-12-04 19:19:45 +0000115
blueswir1ad46db92008-12-11 19:37:54 +0000116int qemu_fls(int i)
aliguorib39ade82008-12-04 19:19:45 +0000117{
aliguori8d371d42008-12-04 20:08:06 +0000118 return 32 - clz32(i);
aliguorib39ade82008-12-04 19:19:45 +0000119}
aliguori44e3ee82009-01-22 16:59:20 +0000120
Christoph Hellwig6f1953c2009-09-04 19:01:32 +0200121/*
122 * Make sure data goes on disk, but if possible do not bother to
123 * write out the inode just for timestamp updates.
124 *
125 * Unfortunately even in 2009 many operating systems do not support
126 * fdatasync and have to fall back to fsync.
127 */
128int qemu_fdatasync(int fd)
129{
Blue Swirl5f6b9e82009-09-20 06:56:26 +0000130#ifdef CONFIG_FDATASYNC
Christoph Hellwig6f1953c2009-09-04 19:01:32 +0200131 return fdatasync(fd);
132#else
133 return fsync(fd);
134#endif
135}
136
aliguori44e3ee82009-01-22 16:59:20 +0000137/* io vectors */
138
139void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
140{
Anthony Liguori7267c092011-08-20 22:09:37 -0500141 qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec));
aliguori44e3ee82009-01-22 16:59:20 +0000142 qiov->niov = 0;
143 qiov->nalloc = alloc_hint;
aliguori249aa742009-01-26 17:17:52 +0000144 qiov->size = 0;
aliguori44e3ee82009-01-22 16:59:20 +0000145}
146
aliguori522584a2009-03-28 17:46:10 +0000147void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
148{
149 int i;
150
151 qiov->iov = iov;
152 qiov->niov = niov;
153 qiov->nalloc = -1;
154 qiov->size = 0;
155 for (i = 0; i < niov; i++)
156 qiov->size += iov[i].iov_len;
157}
158
aliguori44e3ee82009-01-22 16:59:20 +0000159void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
160{
aliguori522584a2009-03-28 17:46:10 +0000161 assert(qiov->nalloc != -1);
162
aliguori44e3ee82009-01-22 16:59:20 +0000163 if (qiov->niov == qiov->nalloc) {
164 qiov->nalloc = 2 * qiov->nalloc + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500165 qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec));
aliguori44e3ee82009-01-22 16:59:20 +0000166 }
167 qiov->iov[qiov->niov].iov_base = base;
168 qiov->iov[qiov->niov].iov_len = len;
aliguori249aa742009-01-26 17:17:52 +0000169 qiov->size += len;
aliguori44e3ee82009-01-22 16:59:20 +0000170 ++qiov->niov;
171}
172
Kevin Wolf40b4f532009-09-09 17:53:37 +0200173/*
Kevin Wolfb8a83a42010-09-13 18:06:11 +0200174 * Copies iovecs from src to the end of dst. It starts copying after skipping
175 * the given number of bytes in src and copies until src is completely copied
176 * or the total size of the copied iovec reaches size.The size of the last
177 * copied iovec is changed in order to fit the specified total size if it isn't
178 * a perfect fit already.
Kevin Wolf40b4f532009-09-09 17:53:37 +0200179 */
Kevin Wolfb8a83a42010-09-13 18:06:11 +0200180void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
181 size_t size)
Kevin Wolf40b4f532009-09-09 17:53:37 +0200182{
183 int i;
184 size_t done;
Kevin Wolfb8a83a42010-09-13 18:06:11 +0200185 void *iov_base;
186 uint64_t iov_len;
Kevin Wolf40b4f532009-09-09 17:53:37 +0200187
188 assert(dst->nalloc != -1);
189
190 done = 0;
191 for (i = 0; (i < src->niov) && (done != size); i++) {
Kevin Wolfb8a83a42010-09-13 18:06:11 +0200192 if (skip >= src->iov[i].iov_len) {
193 /* Skip the whole iov */
194 skip -= src->iov[i].iov_len;
195 continue;
196 } else {
197 /* Skip only part (or nothing) of the iov */
198 iov_base = (uint8_t*) src->iov[i].iov_base + skip;
199 iov_len = src->iov[i].iov_len - skip;
200 skip = 0;
201 }
202
203 if (done + iov_len > size) {
204 qemu_iovec_add(dst, iov_base, size - done);
Kevin Wolf40b4f532009-09-09 17:53:37 +0200205 break;
206 } else {
Kevin Wolfb8a83a42010-09-13 18:06:11 +0200207 qemu_iovec_add(dst, iov_base, iov_len);
Kevin Wolf40b4f532009-09-09 17:53:37 +0200208 }
Kevin Wolfb8a83a42010-09-13 18:06:11 +0200209 done += iov_len;
Kevin Wolf40b4f532009-09-09 17:53:37 +0200210 }
211}
212
Kevin Wolfb8a83a42010-09-13 18:06:11 +0200213void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size)
214{
215 qemu_iovec_copy(dst, src, 0, size);
216}
217
aliguori44e3ee82009-01-22 16:59:20 +0000218void qemu_iovec_destroy(QEMUIOVector *qiov)
219{
aliguori522584a2009-03-28 17:46:10 +0000220 assert(qiov->nalloc != -1);
221
Paolo Bonzinibd83b362011-11-25 12:06:22 +0100222 qemu_iovec_reset(qiov);
Anthony Liguori7267c092011-08-20 22:09:37 -0500223 g_free(qiov->iov);
Paolo Bonzinibd83b362011-11-25 12:06:22 +0100224 qiov->nalloc = 0;
225 qiov->iov = NULL;
aliguori44e3ee82009-01-22 16:59:20 +0000226}
227
aliguoribe959462009-02-05 21:23:54 +0000228void qemu_iovec_reset(QEMUIOVector *qiov)
229{
aliguori522584a2009-03-28 17:46:10 +0000230 assert(qiov->nalloc != -1);
231
aliguoribe959462009-02-05 21:23:54 +0000232 qiov->niov = 0;
233 qiov->size = 0;
234}
235
aliguori44e3ee82009-01-22 16:59:20 +0000236void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf)
237{
238 uint8_t *p = (uint8_t *)buf;
239 int i;
240
241 for (i = 0; i < qiov->niov; ++i) {
242 memcpy(p, qiov->iov[i].iov_base, qiov->iov[i].iov_len);
243 p += qiov->iov[i].iov_len;
244 }
245}
246
aliguori249aa742009-01-26 17:17:52 +0000247void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count)
aliguori44e3ee82009-01-22 16:59:20 +0000248{
249 const uint8_t *p = (const uint8_t *)buf;
aliguori249aa742009-01-26 17:17:52 +0000250 size_t copy;
aliguori44e3ee82009-01-22 16:59:20 +0000251 int i;
252
aliguori249aa742009-01-26 17:17:52 +0000253 for (i = 0; i < qiov->niov && count; ++i) {
254 copy = count;
255 if (copy > qiov->iov[i].iov_len)
256 copy = qiov->iov[i].iov_len;
257 memcpy(qiov->iov[i].iov_base, p, copy);
258 p += copy;
259 count -= copy;
aliguori44e3ee82009-01-22 16:59:20 +0000260 }
261}
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100262
Kevin Wolfb8a83a42010-09-13 18:06:11 +0200263void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count)
264{
265 size_t n;
266 int i;
267
268 for (i = 0; i < qiov->niov && count; ++i) {
269 n = MIN(count, qiov->iov[i].iov_len);
270 memset(qiov->iov[i].iov_base, c, n);
271 count -= n;
272 }
273}
274
Chunqiang Tange0d9c6f2011-02-03 10:12:49 -0500275void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
276 size_t skip)
277{
278 int i;
279 size_t done;
280 void *iov_base;
281 uint64_t iov_len;
282
283 done = 0;
284 for (i = 0; (i < qiov->niov) && (done != count); i++) {
285 if (skip >= qiov->iov[i].iov_len) {
286 /* Skip the whole iov */
287 skip -= qiov->iov[i].iov_len;
288 continue;
289 } else {
290 /* Skip only part (or nothing) of the iov */
291 iov_base = (uint8_t*) qiov->iov[i].iov_base + skip;
292 iov_len = qiov->iov[i].iov_len - skip;
293 skip = 0;
294 }
295
296 if (done + iov_len > count) {
297 memset(iov_base, c, count - done);
298 break;
299 } else {
300 memset(iov_base, c, iov_len);
301 }
302 done += iov_len;
303 }
304}
305
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100306#ifndef _WIN32
307/* Sets a specific flag */
308int fcntl_setfl(int fd, int flag)
309{
310 int flags;
311
312 flags = fcntl(fd, F_GETFL);
313 if (flags == -1)
314 return -errno;
315
316 if (fcntl(fd, F_SETFL, flags | flag) == -1)
317 return -errno;
318
319 return 0;
320}
321#endif
322
Markus Armbrustereba90e42011-11-22 09:46:06 +0100323static int64_t suffix_mul(char suffix, int64_t unit)
324{
325 switch (qemu_toupper(suffix)) {
326 case STRTOSZ_DEFSUFFIX_B:
327 return 1;
328 case STRTOSZ_DEFSUFFIX_KB:
329 return unit;
330 case STRTOSZ_DEFSUFFIX_MB:
331 return unit * unit;
332 case STRTOSZ_DEFSUFFIX_GB:
333 return unit * unit * unit;
334 case STRTOSZ_DEFSUFFIX_TB:
335 return unit * unit * unit * unit;
336 }
337 return -1;
338}
339
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200340/*
341 * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
Markus Armbruster8dddfb52011-11-22 09:46:01 +0100342 * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
Markus Armbrustereba90e42011-11-22 09:46:06 +0100343 * in *end, if not NULL. Return -1 on error.
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200344 */
Joerg Roedela732e1b2011-07-07 16:13:11 +0200345int64_t strtosz_suffix_unit(const char *nptr, char **end,
346 const char default_suffix, int64_t unit)
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200347{
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100348 int64_t retval = -1;
Jes Sorensenf3bd3622011-01-24 16:33:28 +0100349 char *endptr;
Markus Armbrustereba90e42011-11-22 09:46:06 +0100350 unsigned char c;
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200351 int mul_required = 0;
352 double val, mul, integral, fraction;
353
354 errno = 0;
355 val = strtod(nptr, &endptr);
356 if (isnan(val) || endptr == nptr || errno != 0) {
357 goto fail;
358 }
Jes Sorensen7eb05342011-01-24 16:33:30 +0100359 fraction = modf(val, &integral);
360 if (fraction != 0) {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200361 mul_required = 1;
362 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200363 c = *endptr;
Markus Armbrustereba90e42011-11-22 09:46:06 +0100364 mul = suffix_mul(c, unit);
365 if (mul >= 0) {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200366 endptr++;
Markus Armbrustereba90e42011-11-22 09:46:06 +0100367 } else {
368 mul = suffix_mul(default_suffix, unit);
369 assert(mul >= 0);
370 }
371 if (mul == 1 && mul_required) {
372 goto fail;
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200373 }
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100374 if ((val * mul >= INT64_MAX) || val < 0) {
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200375 goto fail;
376 }
377 retval = val * mul;
378
379fail:
380 if (end) {
381 *end = endptr;
382 }
383
384 return retval;
385}
Jes Sorensend8427002010-12-09 14:17:24 +0100386
Joerg Roedela732e1b2011-07-07 16:13:11 +0200387int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
388{
Jan Kiszkafdc9c412011-08-15 16:24:48 -0700389 return strtosz_suffix_unit(nptr, end, default_suffix, 1024);
Joerg Roedela732e1b2011-07-07 16:13:11 +0200390}
391
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100392int64_t strtosz(const char *nptr, char **end)
Jes Sorensend8427002010-12-09 14:17:24 +0100393{
394 return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
395}
Stefan Berger443916d2011-09-28 06:41:32 -0400396
397int qemu_parse_fd(const char *param)
398{
399 int fd;
400 char *endptr = NULL;
401
402 fd = strtol(param, &endptr, 10);
403 if (*endptr || (fd == 0 && param == endptr)) {
404 return -1;
405 }
406 return fd;
407}
Paolo Bonzini8c5135f2011-09-08 13:46:25 +0200408
409/*
410 * Send/recv data with iovec buffers
411 *
412 * This function send/recv data from/to the iovec buffer directly.
413 * The first `offset' bytes in the iovec buffer are skipped and next
414 * `len' bytes are used.
415 *
416 * For example,
417 *
418 * do_sendv_recvv(sockfd, iov, len, offset, 1);
419 *
420 * is equal to
421 *
422 * char *buf = malloc(size);
423 * iov_to_buf(iov, iovcnt, buf, offset, size);
424 * send(sockfd, buf, size, 0);
425 * free(buf);
426 */
427static int do_sendv_recvv(int sockfd, struct iovec *iov, int len, int offset,
428 int do_sendv)
429{
430 int ret, diff, iovlen;
431 struct iovec *last_iov;
432
433 /* last_iov is inclusive, so count from one. */
434 iovlen = 1;
435 last_iov = iov;
436 len += offset;
437
438 while (last_iov->iov_len < len) {
439 len -= last_iov->iov_len;
440
441 last_iov++;
442 iovlen++;
443 }
444
445 diff = last_iov->iov_len - len;
446 last_iov->iov_len -= diff;
447
448 while (iov->iov_len <= offset) {
449 offset -= iov->iov_len;
450
451 iov++;
452 iovlen--;
453 }
454
455 iov->iov_base = (char *) iov->iov_base + offset;
456 iov->iov_len -= offset;
457
458 {
459#if defined CONFIG_IOVEC && defined CONFIG_POSIX
460 struct msghdr msg;
461 memset(&msg, 0, sizeof(msg));
462 msg.msg_iov = iov;
463 msg.msg_iovlen = iovlen;
464
465 do {
466 if (do_sendv) {
467 ret = sendmsg(sockfd, &msg, 0);
468 } else {
469 ret = recvmsg(sockfd, &msg, 0);
470 }
471 } while (ret == -1 && errno == EINTR);
472#else
473 struct iovec *p = iov;
474 ret = 0;
475 while (iovlen > 0) {
476 int rc;
477 if (do_sendv) {
478 rc = send(sockfd, p->iov_base, p->iov_len, 0);
479 } else {
480 rc = qemu_recv(sockfd, p->iov_base, p->iov_len, 0);
481 }
482 if (rc == -1) {
483 if (errno == EINTR) {
484 continue;
485 }
486 if (ret == 0) {
487 ret = -1;
488 }
489 break;
490 }
491 if (rc == 0) {
492 break;
493 }
494 ret += rc;
495 iovlen--, p++;
496 }
497#endif
498 }
499
500 /* Undo the changes above */
501 iov->iov_base = (char *) iov->iov_base - offset;
502 iov->iov_len += offset;
503 last_iov->iov_len += diff;
504 return ret;
505}
506
507int qemu_recvv(int sockfd, struct iovec *iov, int len, int iov_offset)
508{
509 return do_sendv_recvv(sockfd, iov, len, iov_offset, 0);
510}
511
512int qemu_sendv(int sockfd, struct iovec *iov, int len, int iov_offset)
513{
514 return do_sendv_recvv(sockfd, iov, len, iov_offset, 1);
515}
516