Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser.c revision 1.18
      1 /*	$NetBSD: rumpuser.c,v 1.18 2012/07/27 09:09:05 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007-2010 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include "rumpuser_port.h"
     29 
     30 #if !defined(lint)
     31 __RCSID("$NetBSD: rumpuser.c,v 1.18 2012/07/27 09:09:05 pooka Exp $");
     32 #endif /* !lint */
     33 
     34 #include <sys/ioctl.h>
     35 #include <sys/mman.h>
     36 #include <sys/uio.h>
     37 #include <sys/stat.h>
     38 #include <sys/time.h>
     39 
     40 #ifdef __NetBSD__
     41 #include <sys/disk.h>
     42 #include <sys/disklabel.h>
     43 #include <sys/dkio.h>
     44 #include <sys/sysctl.h>
     45 #include <sys/event.h>
     46 #endif
     47 
     48 #include <assert.h>
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <fcntl.h>
     52 #include <poll.h>
     53 #include <signal.h>
     54 #include <stdarg.h>
     55 #include <stdint.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <time.h>
     60 #include <unistd.h>
     61 
     62 #include <rump/rumpuser.h>
     63 
     64 #include "rumpuser_int.h"
     65 
     66 int
     67 rumpuser_getversion(void)
     68 {
     69 
     70 	return RUMPUSER_VERSION;
     71 }
     72 
     73 int
     74 rumpuser_getfileinfo(const char *path, uint64_t *sizep, int *ftp, int *error)
     75 {
     76 	struct stat sb;
     77 	uint64_t size;
     78 	int needsdev = 0, rv = 0, ft;
     79 	int fd = -1;
     80 
     81 	if (stat(path, &sb) == -1) {
     82 		seterror(errno);
     83 		return -1;
     84 	}
     85 
     86 	switch (sb.st_mode & S_IFMT) {
     87 	case S_IFDIR:
     88 		ft = RUMPUSER_FT_DIR;
     89 		break;
     90 	case S_IFREG:
     91 		ft = RUMPUSER_FT_REG;
     92 		break;
     93 	case S_IFBLK:
     94 		ft = RUMPUSER_FT_BLK;
     95 		needsdev = 1;
     96 		break;
     97 	case S_IFCHR:
     98 		ft = RUMPUSER_FT_CHR;
     99 		needsdev = 1;
    100 		break;
    101 	default:
    102 		ft = RUMPUSER_FT_OTHER;
    103 		break;
    104 	}
    105 
    106 	if (!needsdev) {
    107 		size = sb.st_size;
    108 	} else if (sizep) {
    109 		/*
    110 		 * Welcome to the jungle.  Of course querying the kernel
    111 		 * for a device partition size is supposed to be far from
    112 		 * trivial.  On NetBSD we use ioctl.  On $other platform
    113 		 * we have a problem.  We try "the lseek trick" and just
    114 		 * fail if that fails.  Platform specific code can later
    115 		 * be written here if appropriate.
    116 		 *
    117 		 * On NetBSD we hope and pray that for block devices nobody
    118 		 * else is holding them open, because otherwise the kernel
    119 		 * will not permit us to open it.  Thankfully, this is
    120 		 * usually called only in bootstrap and then we can
    121 		 * forget about it.
    122 		 */
    123 #ifndef __NetBSD__
    124 		off_t off;
    125 
    126 		fd = open(path, O_RDONLY);
    127 		if (fd == -1) {
    128 			seterror(errno);
    129 			rv = -1;
    130 			goto out;
    131 		}
    132 
    133 		off = lseek(fd, 0, SEEK_END);
    134 		if (off != 0) {
    135 			size = off;
    136 			goto out;
    137 		}
    138 		fprintf(stderr, "error: device size query not implemented on "
    139 		    "this platform\n");
    140 		seterror(EOPNOTSUPP);
    141 		rv = -1;
    142 		goto out;
    143 #else
    144 		struct disklabel lab;
    145 		struct partition *parta;
    146 		struct dkwedge_info dkw;
    147 
    148 		fd = open(path, O_RDONLY);
    149 		if (fd == -1) {
    150 			seterror(errno);
    151 			rv = -1;
    152 			goto out;
    153 		}
    154 
    155 		if (ioctl(fd, DIOCGDINFO, &lab) == 0) {
    156 			parta = &lab.d_partitions[DISKPART(sb.st_rdev)];
    157 			size = (uint64_t)lab.d_secsize * parta->p_size;
    158 			goto out;
    159 		}
    160 
    161 		if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
    162 			/*
    163 			 * XXX: should use DIOCGDISKINFO to query
    164 			 * sector size, but that requires proplib,
    165 			 * so just don't bother for now.  it's nice
    166 			 * that something as difficult as figuring out
    167 			 * a partition's size has been made so easy.
    168 			 */
    169 			size = dkw.dkw_size << DEV_BSHIFT;
    170 			goto out;
    171 		}
    172 
    173 		seterror(errno);
    174 		rv = -1;
    175 #endif /* __NetBSD__ */
    176 	}
    177 
    178  out:
    179 	if (rv == 0 && sizep)
    180 		*sizep = size;
    181 	if (rv == 0 && ftp)
    182 		*ftp = ft;
    183 	if (fd != -1)
    184 		close(fd);
    185 
    186 	return rv;
    187 }
    188 
    189 int
    190 rumpuser_nanosleep(uint64_t *sec, uint64_t *nsec, int *error)
    191 {
    192 	struct timespec rqt, rmt;
    193 	int rv;
    194 
    195 	/*LINTED*/
    196 	rqt.tv_sec = *sec;
    197 	/*LINTED*/
    198 	rqt.tv_nsec = *nsec;
    199 
    200 	KLOCK_WRAP(rv = nanosleep(&rqt, &rmt));
    201 	if (rv == -1)
    202 		seterror(errno);
    203 
    204 	*sec = rmt.tv_sec;
    205 	*nsec = rmt.tv_nsec;
    206 
    207 	return rv;
    208 }
    209 
    210 void *
    211 rumpuser_malloc(size_t howmuch, int alignment)
    212 {
    213 	void *mem;
    214 	int rv;
    215 
    216 	if (alignment == 0)
    217 		alignment = sizeof(void *);
    218 
    219 	rv = posix_memalign(&mem, (size_t)alignment, howmuch);
    220 	if (__predict_false(rv != 0)) {
    221 		if (rv == EINVAL) {
    222 			printf("rumpuser_malloc: invalid alignment %d\n",
    223 			    alignment);
    224 			abort();
    225 		}
    226 		mem = NULL;
    227 	}
    228 
    229 	return mem;
    230 }
    231 
    232 void *
    233 rumpuser_realloc(void *ptr, size_t howmuch)
    234 {
    235 
    236 	return realloc(ptr, howmuch);
    237 }
    238 
    239 void
    240 rumpuser_free(void *ptr)
    241 {
    242 
    243 	free(ptr);
    244 }
    245 
    246 void *
    247 rumpuser_anonmmap(void *prefaddr, size_t size, int alignbit,
    248 	int exec, int *error)
    249 {
    250 	void *rv;
    251 	int prot;
    252 
    253 #ifndef MAP_ALIGNED
    254 #define MAP_ALIGNED(a) 0
    255 	if (alignbit)
    256 		fprintf(stderr, "rumpuser_anonmmap: warning, requested "
    257 		    "alignment not supported by hypervisor\n");
    258 #endif
    259 
    260 	prot = PROT_READ|PROT_WRITE;
    261 	if (exec)
    262 		prot |= PROT_EXEC;
    263 	rv = mmap(prefaddr, size, prot,
    264 	    MAP_ANON | MAP_ALIGNED(alignbit), -1, 0);
    265 	if (rv == MAP_FAILED) {
    266 		seterror(errno);
    267 		return NULL;
    268 	}
    269 	return rv;
    270 }
    271 
    272 void
    273 rumpuser_unmap(void *addr, size_t len)
    274 {
    275 	int rv;
    276 
    277 	rv = munmap(addr, len);
    278 	assert(rv == 0);
    279 }
    280 
    281 void *
    282 rumpuser_filemmap(int fd, off_t offset, size_t len, int flags, int *error)
    283 {
    284 	void *rv;
    285 	int mmflags, prot;
    286 
    287 	if (flags & RUMPUSER_FILEMMAP_TRUNCATE)
    288 		ftruncate(fd, offset + len);
    289 
    290 	mmflags = MAP_FILE;
    291 	if (flags & RUMPUSER_FILEMMAP_SHARED)
    292 		mmflags |= MAP_SHARED;
    293 	else
    294 		mmflags |= MAP_PRIVATE;
    295 
    296 	prot = 0;
    297 	if (flags & RUMPUSER_FILEMMAP_READ)
    298 		prot |= PROT_READ;
    299 	if (flags & RUMPUSER_FILEMMAP_WRITE)
    300 		prot |= PROT_WRITE;
    301 
    302 	rv = mmap(NULL, len, PROT_READ|PROT_WRITE, mmflags, fd, offset);
    303 	if (rv == MAP_FAILED) {
    304 		seterror(errno);
    305 		return NULL;
    306 	}
    307 
    308 	seterror(0);
    309 	return rv;
    310 }
    311 
    312 int
    313 rumpuser_memsync(void *addr, size_t len, int *error)
    314 {
    315 
    316 	DOCALL_KLOCK(int, (msync(addr, len, MS_SYNC)));
    317 }
    318 
    319 int
    320 rumpuser_open(const char *path, int flags, int *error)
    321 {
    322 
    323 	DOCALL(int, (open(path, flags, 0644)));
    324 }
    325 
    326 int
    327 rumpuser_ioctl(int fd, u_long cmd, void *data, int *error)
    328 {
    329 
    330 	DOCALL_KLOCK(int, (ioctl(fd, cmd, data)));
    331 }
    332 
    333 int
    334 rumpuser_close(int fd, int *error)
    335 {
    336 
    337 	DOCALL(int, close(fd));
    338 }
    339 
    340 int
    341 rumpuser_fsync(int fd, int *error)
    342 {
    343 
    344 	DOCALL_KLOCK(int, fsync(fd));
    345 }
    346 
    347 ssize_t
    348 rumpuser_read(int fd, void *data, size_t size, int *error)
    349 {
    350 	ssize_t rv;
    351 
    352 	KLOCK_WRAP(rv = read(fd, data, size));
    353 	if (rv == -1)
    354 		seterror(errno);
    355 
    356 	return rv;
    357 }
    358 
    359 ssize_t
    360 rumpuser_pread(int fd, void *data, size_t size, off_t offset, int *error)
    361 {
    362 	ssize_t rv;
    363 
    364 	KLOCK_WRAP(rv = pread(fd, data, size, offset));
    365 	if (rv == -1)
    366 		seterror(errno);
    367 
    368 	return rv;
    369 }
    370 
    371 void
    372 rumpuser_read_bio(int fd, void *data, size_t size, off_t offset,
    373 	rump_biodone_fn biodone, void *biodonecookie)
    374 {
    375 	ssize_t rv;
    376 	int error = 0;
    377 
    378 	rv = rumpuser_pread(fd, data, size, offset, &error);
    379 	/* check against <0 instead of ==-1 to get typing below right */
    380 	if (rv < 0)
    381 		rv = 0;
    382 
    383 	/* LINTED: see above */
    384 	biodone(biodonecookie, rv, error);
    385 }
    386 
    387 ssize_t
    388 rumpuser_write(int fd, const void *data, size_t size, int *error)
    389 {
    390 	ssize_t rv;
    391 
    392 	KLOCK_WRAP(rv = write(fd, data, size));
    393 	if (rv == -1)
    394 		seterror(errno);
    395 
    396 	return rv;
    397 }
    398 
    399 ssize_t
    400 rumpuser_pwrite(int fd, const void *data, size_t size, off_t offset, int *error)
    401 {
    402 	ssize_t rv;
    403 
    404 	KLOCK_WRAP(rv = pwrite(fd, data, size, offset));
    405 	if (rv == -1)
    406 		seterror(errno);
    407 
    408 	return rv;
    409 }
    410 
    411 void
    412 rumpuser_write_bio(int fd, const void *data, size_t size, off_t offset,
    413 	rump_biodone_fn biodone, void *biodonecookie)
    414 {
    415 	ssize_t rv;
    416 	int error = 0;
    417 
    418 	rv = rumpuser_pwrite(fd, data, size, offset, &error);
    419 	/* check against <0 instead of ==-1 to get typing below right */
    420 	if (rv < 0)
    421 		rv = 0;
    422 
    423 	/* LINTED: see above */
    424 	biodone(biodonecookie, rv, error);
    425 }
    426 
    427 ssize_t
    428 rumpuser_readv(int fd, const struct rumpuser_iovec *riov, int iovcnt,
    429 	int *error)
    430 {
    431 	struct iovec *iovp;
    432 	ssize_t rv;
    433 	int i;
    434 
    435 	iovp = malloc(iovcnt * sizeof(struct iovec));
    436 	if (iovp == NULL) {
    437 		seterror(ENOMEM);
    438 		return -1;
    439 	}
    440 	for (i = 0; i < iovcnt; i++) {
    441 		iovp[i].iov_base = riov[i].iov_base;
    442 		/*LINTED*/
    443 		iovp[i].iov_len = riov[i].iov_len;
    444 	}
    445 
    446 	KLOCK_WRAP(rv = readv(fd, iovp, iovcnt));
    447 	if (rv == -1)
    448 		seterror(errno);
    449 	free(iovp);
    450 
    451 	return rv;
    452 }
    453 
    454 ssize_t
    455 rumpuser_writev(int fd, const struct rumpuser_iovec *riov, int iovcnt,
    456 	int *error)
    457 {
    458 	struct iovec *iovp;
    459 	ssize_t rv;
    460 	int i;
    461 
    462 	iovp = malloc(iovcnt * sizeof(struct iovec));
    463 	if (iovp == NULL) {
    464 		seterror(ENOMEM);
    465 		return -1;
    466 	}
    467 	for (i = 0; i < iovcnt; i++) {
    468 		iovp[i].iov_base = riov[i].iov_base;
    469 		/*LINTED*/
    470 		iovp[i].iov_len = riov[i].iov_len;
    471 	}
    472 
    473 	KLOCK_WRAP(rv = writev(fd, iovp, iovcnt));
    474 	if (rv == -1)
    475 		seterror(errno);
    476 	free(iovp);
    477 
    478 	return rv;
    479 }
    480 
    481 int
    482 rumpuser_gettime(uint64_t *sec, uint64_t *nsec, int *error)
    483 {
    484 	struct timeval tv;
    485 	int rv;
    486 
    487 	rv = gettimeofday(&tv, NULL);
    488 	if (rv == -1) {
    489 		seterror(errno);
    490 		return rv;
    491 	}
    492 
    493 	*sec = tv.tv_sec;
    494 	*nsec = tv.tv_usec * 1000;
    495 
    496 	return 0;
    497 }
    498 
    499 int
    500 rumpuser_getenv(const char *name, char *buf, size_t blen, int *error)
    501 {
    502 
    503 #ifdef __linux__
    504 	char *tmp;
    505 
    506 	*error = 0;
    507 	if ((tmp = getenv(name)) != NULL) {
    508 		if (strlen(tmp) >= blen) {
    509 			*error = ERANGE;
    510 			return -1;
    511 		}
    512 		strcpy(buf, tmp);
    513 		return 0;
    514 	} else {
    515 		*error = ENOENT;
    516 		return -1;
    517 	}
    518 #else
    519 	DOCALL(int, getenv_r(name, buf, blen));
    520 #endif
    521 }
    522 
    523 int
    524 rumpuser_gethostname(char *name, size_t namelen, int *error)
    525 {
    526 	char tmp[MAXHOSTNAMELEN];
    527 
    528 	if (gethostname(tmp, sizeof(tmp)) == -1) {
    529 		snprintf(name, namelen, "rump-%05d.rumpdomain", getpid());
    530 	} else {
    531 		snprintf(name, namelen, "rump-%05d.%s.rumpdomain",
    532 		    getpid(), tmp);
    533 	}
    534 
    535 	*error = 0;
    536 	return 0;
    537 }
    538 
    539 int
    540 rumpuser_poll(struct pollfd *fds, int nfds, int timeout, int *error)
    541 {
    542 
    543 	DOCALL_KLOCK(int, (poll(fds, (nfds_t)nfds, timeout)));
    544 }
    545 
    546 int
    547 rumpuser_putchar(int c, int *error)
    548 {
    549 
    550 	DOCALL(int, (putchar(c)));
    551 }
    552 
    553 void
    554 rumpuser_exit(int rv)
    555 {
    556 
    557 	if (rv == RUMPUSER_PANIC)
    558 		abort();
    559 	else
    560 		exit(rv);
    561 }
    562 
    563 void
    564 rumpuser_seterrno(int error)
    565 {
    566 
    567 	errno = error;
    568 }
    569 
    570 #ifdef __NetBSD__
    571 int
    572 rumpuser_writewatchfile_setup(int kq, int fd, intptr_t opaque, int *error)
    573 {
    574 	struct kevent kev;
    575 
    576 	if (kq == -1) {
    577 		kq = kqueue();
    578 		if (kq == -1) {
    579 			seterror(errno);
    580 			return -1;
    581 		}
    582 	}
    583 
    584 	EV_SET(&kev, fd, EVFILT_VNODE, EV_ADD|EV_ENABLE|EV_CLEAR,
    585 	    NOTE_WRITE, 0, opaque);
    586 	if (kevent(kq, &kev, 1, NULL, 0, NULL) == -1) {
    587 		seterror(errno);
    588 		return -1;
    589 	}
    590 
    591 	return kq;
    592 }
    593 
    594 int
    595 rumpuser_writewatchfile_wait(int kq, intptr_t *opaque, int *error)
    596 {
    597 	struct kevent kev;
    598 	int rv;
    599 
    600  again:
    601 	KLOCK_WRAP(rv = kevent(kq, NULL, 0, &kev, 1, NULL));
    602 	if (rv == -1) {
    603 		if (errno == EINTR)
    604 			goto again;
    605 		seterror(errno);
    606 		return -1;
    607 	}
    608 
    609 	if (opaque)
    610 		*opaque = kev.udata;
    611 	return rv;
    612 }
    613 #endif
    614 
    615 /*
    616  * This is meant for safe debugging prints from the kernel.
    617  */
    618 int
    619 rumpuser_dprintf(const char *format, ...)
    620 {
    621 	va_list ap;
    622 	int rv;
    623 
    624 	va_start(ap, format);
    625 	rv = vfprintf(stderr, format, ap);
    626 	va_end(ap);
    627 
    628 	return rv;
    629 }
    630 
    631 int
    632 rumpuser_kill(int64_t pid, int sig, int *error)
    633 {
    634 
    635 #ifdef __NetBSD__
    636 	if (pid == RUMPUSER_PID_SELF) {
    637 		DOCALL(int, raise(sig));
    638 	} else {
    639 		DOCALL(int, kill((pid_t)pid, sig));
    640 	}
    641 #else
    642 	/* XXXfixme: signal numbers may not match on non-NetBSD */
    643 	seterror(EOPNOTSUPP);
    644 	return -1;
    645 #endif
    646 }
    647 
    648 int
    649 rumpuser_getnhostcpu(void)
    650 {
    651 	int ncpu = 1;
    652 
    653 #ifdef __NetBSD__
    654 	size_t sz = sizeof(ncpu);
    655 
    656 	sysctlbyname("hw.ncpu", &ncpu, &sz, NULL, 0);
    657 #elif __linux__
    658 	FILE *fp;
    659 	char *line = NULL;
    660 	size_t n = 0;
    661 
    662 	/* If anyone knows a better way, I'm all ears */
    663 	if ((fp = fopen("/proc/cpuinfo", "r")) != NULL) {
    664 		ncpu = 0;
    665 		while (getline(&line, &n, fp) != -1) {
    666 			if (strncmp(line,
    667 			    "processor", sizeof("processor")-1) == 0)
    668 			    	ncpu++;
    669 		}
    670 		if (ncpu == 0)
    671 			ncpu = 1;
    672 		free(line);
    673 		fclose(fp);
    674 	}
    675 #endif
    676 
    677 	return ncpu;
    678 }
    679 
    680 /* XXX: this hypercall needs a better name */
    681 uint32_t
    682 rumpuser_arc4random(void)
    683 {
    684 
    685 	return arc4random();
    686 }
    687