2 * Simple C functions to supplement the C library
4 * Copyright (c) 2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu-common.h"
25 #include "host-utils.h"
28 #include "qemu_socket.h"
30 void pstrcpy(char *buf, int buf_size, const char *str)
40 if (c == 0 || q >= buf + buf_size - 1)
47 /* strcat and truncate. */
48 char *pstrcat(char *buf, int buf_size, const char *s)
53 pstrcpy(buf + len, buf_size - len, s);
57 int strstart(const char *str, const char *val, const char **ptr)
73 int stristart(const char *str, const char *val, const char **ptr)
79 if (qemu_toupper(*p) != qemu_toupper(*q))
89 /* XXX: use host strnlen if available ? */
90 int qemu_strnlen(const char *s, int max_len)
94 for(i = 0; i < max_len; i++) {
102 time_t mktimegm(struct tm *tm)
105 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
110 t = 86400 * (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 +
112 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
118 return 32 - clz32(i);
122 * Make sure data goes on disk, but if possible do not bother to
123 * write out the inode just for timestamp updates.
125 * Unfortunately even in 2009 many operating systems do not support
126 * fdatasync and have to fall back to fsync.
128 int qemu_fdatasync(int fd)
130 #ifdef CONFIG_FDATASYNC
131 return fdatasync(fd);
139 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
141 qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec));
143 qiov->nalloc = alloc_hint;
147 void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
155 for (i = 0; i < niov; i++)
156 qiov->size += iov[i].iov_len;
159 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
161 assert(qiov->nalloc != -1);
163 if (qiov->niov == qiov->nalloc) {
164 qiov->nalloc = 2 * qiov->nalloc + 1;
165 qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec));
167 qiov->iov[qiov->niov].iov_base = base;
168 qiov->iov[qiov->niov].iov_len = len;
174 * 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.
180 void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
188 assert(dst->nalloc != -1);
191 for (i = 0; (i < src->niov) && (done != size); i++) {
192 if (skip >= src->iov[i].iov_len) {
193 /* Skip the whole iov */
194 skip -= src->iov[i].iov_len;
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;
203 if (done + iov_len > size) {
204 qemu_iovec_add(dst, iov_base, size - done);
207 qemu_iovec_add(dst, iov_base, iov_len);
213 void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size)
215 qemu_iovec_copy(dst, src, 0, size);
218 void qemu_iovec_destroy(QEMUIOVector *qiov)
220 assert(qiov->nalloc != -1);
222 qemu_iovec_reset(qiov);
228 void qemu_iovec_reset(QEMUIOVector *qiov)
230 assert(qiov->nalloc != -1);
236 void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf)
238 uint8_t *p = (uint8_t *)buf;
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;
247 void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count)
249 const uint8_t *p = (const uint8_t *)buf;
253 for (i = 0; i < qiov->niov && count; ++i) {
255 if (copy > qiov->iov[i].iov_len)
256 copy = qiov->iov[i].iov_len;
257 memcpy(qiov->iov[i].iov_base, p, copy);
263 void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count)
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);
275 void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
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;
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;
296 if (done + iov_len > count) {
297 memset(iov_base, c, count - done);
300 memset(iov_base, c, iov_len);
307 * Checks if a buffer is all zeroes
309 * Attention! The len must be a multiple of 4 * sizeof(long) due to
310 * restriction of optimizations in this function.
312 bool buffer_is_zero(const void *buf, size_t len)
315 * Use long as the biggest available internal data type that fits into the
316 * CPU register and unroll the loop to smooth out the effect of memory
322 const long * const data = buf;
324 assert(len % (4 * sizeof(long)) == 0);
327 for (i = 0; i < len; i += 4) {
333 if (d0 || d1 || d2 || d3) {
342 /* Sets a specific flag */
343 int fcntl_setfl(int fd, int flag)
347 flags = fcntl(fd, F_GETFL);
351 if (fcntl(fd, F_SETFL, flags | flag) == -1)
358 static int64_t suffix_mul(char suffix, int64_t unit)
360 switch (qemu_toupper(suffix)) {
361 case STRTOSZ_DEFSUFFIX_B:
363 case STRTOSZ_DEFSUFFIX_KB:
365 case STRTOSZ_DEFSUFFIX_MB:
367 case STRTOSZ_DEFSUFFIX_GB:
368 return unit * unit * unit;
369 case STRTOSZ_DEFSUFFIX_TB:
370 return unit * unit * unit * unit;
376 * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
377 * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
378 * in *end, if not NULL. Return -1 on error.
380 int64_t strtosz_suffix_unit(const char *nptr, char **end,
381 const char default_suffix, int64_t unit)
386 int mul_required = 0;
387 double val, mul, integral, fraction;
390 val = strtod(nptr, &endptr);
391 if (isnan(val) || endptr == nptr || errno != 0) {
394 fraction = modf(val, &integral);
399 mul = suffix_mul(c, unit);
403 mul = suffix_mul(default_suffix, unit);
406 if (mul == 1 && mul_required) {
409 if ((val * mul >= INT64_MAX) || val < 0) {
422 int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
424 return strtosz_suffix_unit(nptr, end, default_suffix, 1024);
427 int64_t strtosz(const char *nptr, char **end)
429 return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
432 int qemu_parse_fd(const char *param)
437 fd = strtol(param, &endptr, 10);
438 if (*endptr || (fd == 0 && param == endptr)) {
445 * Send/recv data with iovec buffers
447 * This function send/recv data from/to the iovec buffer directly.
448 * The first `offset' bytes in the iovec buffer are skipped and next
449 * `len' bytes are used.
453 * do_sendv_recvv(sockfd, iov, len, offset, 1);
457 * char *buf = malloc(size);
458 * iov_to_buf(iov, iovcnt, buf, offset, size);
459 * send(sockfd, buf, size, 0);
462 static int do_sendv_recvv(int sockfd, struct iovec *iov, int len, int offset,
465 int ret, diff, iovlen;
466 struct iovec *last_iov;
468 /* last_iov is inclusive, so count from one. */
473 while (last_iov->iov_len < len) {
474 len -= last_iov->iov_len;
480 diff = last_iov->iov_len - len;
481 last_iov->iov_len -= diff;
483 while (iov->iov_len <= offset) {
484 offset -= iov->iov_len;
490 iov->iov_base = (char *) iov->iov_base + offset;
491 iov->iov_len -= offset;
494 #if defined CONFIG_IOVEC && defined CONFIG_POSIX
496 memset(&msg, 0, sizeof(msg));
498 msg.msg_iovlen = iovlen;
502 ret = sendmsg(sockfd, &msg, 0);
504 ret = recvmsg(sockfd, &msg, 0);
506 } while (ret == -1 && errno == EINTR);
508 struct iovec *p = iov;
513 rc = send(sockfd, p->iov_base, p->iov_len, 0);
515 rc = qemu_recv(sockfd, p->iov_base, p->iov_len, 0);
518 if (errno == EINTR) {
535 /* Undo the changes above */
536 iov->iov_base = (char *) iov->iov_base - offset;
537 iov->iov_len += offset;
538 last_iov->iov_len += diff;
542 int qemu_recvv(int sockfd, struct iovec *iov, int len, int iov_offset)
544 return do_sendv_recvv(sockfd, iov, len, iov_offset, 0);
547 int qemu_sendv(int sockfd, struct iovec *iov, int len, int iov_offset)
549 return do_sendv_recvv(sockfd, iov, len, iov_offset, 1);