Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser.c revision 1.42
      1 /*	$NetBSD: rumpuser.c,v 1.42 2013/04/29 15:40:38 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.42 2013/04/29 15:40:38 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 #endif
     45 
     46 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
     47 #include <sys/sysctl.h>
     48 #endif
     49 
     50 #include <assert.h>
     51 #include <errno.h>
     52 #include <fcntl.h>
     53 #include <netdb.h>
     54 #include <signal.h>
     55 #include <stdarg.h>
     56 #include <stdint.h>
     57 #include <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <time.h>
     61 #include <unistd.h>
     62 
     63 #include <rump/rumpuser.h>
     64 
     65 #include "rumpuser_int.h"
     66 
     67 struct rumpuser_hyperup rumpuser__hyp;
     68 
     69 int
     70 rumpuser_init(int version, const struct rumpuser_hyperup *hyp)
     71 {
     72 
     73 	if (version != RUMPUSER_VERSION) {
     74 		fprintf(stderr, "rumpuser mismatch, kern: %d, hypervisor %d\n",
     75 		    version, RUMPUSER_VERSION);
     76 		return 1;
     77 	}
     78 
     79 #ifdef RUMPUSER_USE_DEVRANDOM
     80 	uint32_t rv;
     81 	int fd;
     82 
     83 	if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
     84 		srandom(time(NULL));
     85 	} else {
     86 		if (read(fd, &rv, sizeof(rv)) != sizeof(rv))
     87 			srandom(time(NULL));
     88 		else
     89 			srandom(rv);
     90 		close(fd);
     91 	}
     92 #endif
     93 
     94 	rumpuser__thrinit();
     95 	rumpuser__hyp = *hyp;
     96 
     97 	return 0;
     98 }
     99 
    100 int
    101 rumpuser_getfileinfo(const char *path, uint64_t *sizep, int *ftp, int *error)
    102 {
    103 	struct stat sb;
    104 	uint64_t size;
    105 	int needsdev = 0, rv = 0, ft;
    106 	int fd = -1;
    107 
    108 	if (stat(path, &sb) == -1) {
    109 		seterror(errno);
    110 		return -1;
    111 	}
    112 
    113 	switch (sb.st_mode & S_IFMT) {
    114 	case S_IFDIR:
    115 		ft = RUMPUSER_FT_DIR;
    116 		break;
    117 	case S_IFREG:
    118 		ft = RUMPUSER_FT_REG;
    119 		break;
    120 	case S_IFBLK:
    121 		ft = RUMPUSER_FT_BLK;
    122 		needsdev = 1;
    123 		break;
    124 	case S_IFCHR:
    125 		ft = RUMPUSER_FT_CHR;
    126 		needsdev = 1;
    127 		break;
    128 	default:
    129 		ft = RUMPUSER_FT_OTHER;
    130 		break;
    131 	}
    132 
    133 	if (!needsdev) {
    134 		size = sb.st_size;
    135 	} else if (sizep) {
    136 		/*
    137 		 * Welcome to the jungle.  Of course querying the kernel
    138 		 * for a device partition size is supposed to be far from
    139 		 * trivial.  On NetBSD we use ioctl.  On $other platform
    140 		 * we have a problem.  We try "the lseek trick" and just
    141 		 * fail if that fails.  Platform specific code can later
    142 		 * be written here if appropriate.
    143 		 *
    144 		 * On NetBSD we hope and pray that for block devices nobody
    145 		 * else is holding them open, because otherwise the kernel
    146 		 * will not permit us to open it.  Thankfully, this is
    147 		 * usually called only in bootstrap and then we can
    148 		 * forget about it.
    149 		 */
    150 #ifndef __NetBSD__
    151 		off_t off;
    152 
    153 		fd = open(path, O_RDONLY);
    154 		if (fd == -1) {
    155 			seterror(errno);
    156 			rv = -1;
    157 			goto out;
    158 		}
    159 
    160 		off = lseek(fd, 0, SEEK_END);
    161 		if (off != 0) {
    162 			size = off;
    163 			goto out;
    164 		}
    165 		fprintf(stderr, "error: device size query not implemented on "
    166 		    "this platform\n");
    167 		seterror(EOPNOTSUPP);
    168 		rv = -1;
    169 		goto out;
    170 #else
    171 		struct disklabel lab;
    172 		struct partition *parta;
    173 		struct dkwedge_info dkw;
    174 
    175 		fd = open(path, O_RDONLY);
    176 		if (fd == -1) {
    177 			seterror(errno);
    178 			rv = -1;
    179 			goto out;
    180 		}
    181 
    182 		if (ioctl(fd, DIOCGDINFO, &lab) == 0) {
    183 			parta = &lab.d_partitions[DISKPART(sb.st_rdev)];
    184 			size = (uint64_t)lab.d_secsize * parta->p_size;
    185 			goto out;
    186 		}
    187 
    188 		if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
    189 			/*
    190 			 * XXX: should use DIOCGDISKINFO to query
    191 			 * sector size, but that requires proplib,
    192 			 * so just don't bother for now.  it's nice
    193 			 * that something as difficult as figuring out
    194 			 * a partition's size has been made so easy.
    195 			 */
    196 			size = dkw.dkw_size << DEV_BSHIFT;
    197 			goto out;
    198 		}
    199 
    200 		seterror(errno);
    201 		rv = -1;
    202 #endif /* __NetBSD__ */
    203 	}
    204 
    205  out:
    206 	if (rv == 0 && sizep)
    207 		*sizep = size;
    208 	if (rv == 0 && ftp)
    209 		*ftp = ft;
    210 	if (fd != -1)
    211 		close(fd);
    212 
    213 	return rv;
    214 }
    215 
    216 void *
    217 rumpuser_malloc(size_t howmuch, int alignment)
    218 {
    219 	void *mem;
    220 	int rv;
    221 
    222 	if (alignment == 0)
    223 		alignment = sizeof(void *);
    224 
    225 	rv = posix_memalign(&mem, (size_t)alignment, howmuch);
    226 	if (__predict_false(rv != 0)) {
    227 		if (rv == EINVAL) {
    228 			printf("rumpuser_malloc: invalid alignment %d\n",
    229 			    alignment);
    230 			abort();
    231 		}
    232 		mem = NULL;
    233 	}
    234 
    235 	return mem;
    236 }
    237 
    238 /*ARGSUSED1*/
    239 void
    240 rumpuser_free(void *ptr, size_t size)
    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_PRIVATE | 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 int
    282 rumpuser_open(const char *path, int ruflags, int *error)
    283 {
    284 	int flags;
    285 
    286 	switch (ruflags & RUMPUSER_OPEN_ACCMODE) {
    287 	case RUMPUSER_OPEN_RDONLY:
    288 		flags = O_RDONLY;
    289 		break;
    290 	case RUMPUSER_OPEN_WRONLY:
    291 		flags = O_WRONLY;
    292 		break;
    293 	case RUMPUSER_OPEN_RDWR:
    294 		flags = O_RDWR;
    295 		break;
    296 	default:
    297 		*error = EINVAL;
    298 		return -1;
    299 	}
    300 
    301 #define TESTSET(_ru_, _h_) if (ruflags & _ru_) flags |= _h_;
    302 	TESTSET(RUMPUSER_OPEN_CREATE, O_CREAT);
    303 	TESTSET(RUMPUSER_OPEN_EXCL, O_EXCL);
    304 #undef TESTSET
    305 
    306 	DOCALL_KLOCK(int, (open(path, flags, 0644)));
    307 }
    308 
    309 int
    310 rumpuser_close(int fd, int *error)
    311 {
    312 	int nlocks;
    313 
    314 	rumpkern_unsched(&nlocks, NULL);
    315 	fsync(fd);
    316 	close(fd);
    317 	rumpkern_sched(nlocks, NULL);
    318 
    319 	return 0;
    320 }
    321 
    322 ssize_t
    323 rumpuser_read(int fd, void *data, size_t size, int *error)
    324 {
    325 	ssize_t rv;
    326 
    327 	KLOCK_WRAP(rv = read(fd, data, size));
    328 	if (rv == -1)
    329 		seterror(errno);
    330 
    331 	return rv;
    332 }
    333 
    334 ssize_t
    335 rumpuser_pread(int fd, void *data, size_t size, off_t offset, int *error)
    336 {
    337 	ssize_t rv;
    338 
    339 	KLOCK_WRAP(rv = pread(fd, data, size, offset));
    340 	if (rv == -1)
    341 		seterror(errno);
    342 
    343 	return rv;
    344 }
    345 
    346 ssize_t
    347 rumpuser_write(int fd, const void *data, size_t size, int *error)
    348 {
    349 	ssize_t rv;
    350 
    351 	KLOCK_WRAP(rv = write(fd, data, size));
    352 	if (rv == -1)
    353 		seterror(errno);
    354 
    355 	return rv;
    356 }
    357 
    358 ssize_t
    359 rumpuser_pwrite(int fd, const void *data, size_t size, off_t offset, int *error)
    360 {
    361 	ssize_t rv;
    362 
    363 	KLOCK_WRAP(rv = pwrite(fd, data, size, offset));
    364 	if (rv == -1)
    365 		seterror(errno);
    366 
    367 	return rv;
    368 }
    369 
    370 ssize_t
    371 rumpuser_readv(int fd, const struct rumpuser_iovec *riov, int iovcnt,
    372 	int *error)
    373 {
    374 	struct iovec *iovp;
    375 	ssize_t rv;
    376 	int i;
    377 
    378 	iovp = malloc(iovcnt * sizeof(struct iovec));
    379 	if (iovp == NULL) {
    380 		seterror(ENOMEM);
    381 		return -1;
    382 	}
    383 	for (i = 0; i < iovcnt; i++) {
    384 		iovp[i].iov_base = riov[i].iov_base;
    385 		/*LINTED*/
    386 		iovp[i].iov_len = riov[i].iov_len;
    387 	}
    388 
    389 	KLOCK_WRAP(rv = readv(fd, iovp, iovcnt));
    390 	if (rv == -1)
    391 		seterror(errno);
    392 	free(iovp);
    393 
    394 	return rv;
    395 }
    396 
    397 ssize_t
    398 rumpuser_writev(int fd, const struct rumpuser_iovec *riov, int iovcnt,
    399 	int *error)
    400 {
    401 	struct iovec *iovp;
    402 	ssize_t rv;
    403 	int i;
    404 
    405 	iovp = malloc(iovcnt * sizeof(struct iovec));
    406 	if (iovp == NULL) {
    407 		seterror(ENOMEM);
    408 		return -1;
    409 	}
    410 	for (i = 0; i < iovcnt; i++) {
    411 		iovp[i].iov_base = riov[i].iov_base;
    412 		/*LINTED*/
    413 		iovp[i].iov_len = riov[i].iov_len;
    414 	}
    415 
    416 	KLOCK_WRAP(rv = writev(fd, iovp, iovcnt));
    417 	if (rv == -1)
    418 		seterror(errno);
    419 	free(iovp);
    420 
    421 	return rv;
    422 }
    423 
    424 int
    425 rumpuser_clock_gettime(uint64_t *sec, uint64_t *nsec, enum rumpclock rclk)
    426 {
    427 	struct timespec ts;
    428 	clockid_t clk;
    429 	int rv;
    430 
    431 	switch (rclk) {
    432 	case RUMPUSER_CLOCK_RELWALL:
    433 		clk = CLOCK_REALTIME;
    434 		break;
    435 	case RUMPUSER_CLOCK_ABSMONO:
    436 #ifdef HAVE_CLOCK_NANOSLEEP
    437 		clk = CLOCK_MONOTONIC;
    438 #else
    439 		clk = CLOCK_REALTIME;
    440 #endif
    441 		break;
    442 	default:
    443 		abort();
    444 	}
    445 
    446 	rv = clock_gettime(clk, &ts);
    447 	if (rv == -1) {
    448 		return errno;
    449 	}
    450 	*sec = ts.tv_sec;
    451 	*nsec = ts.tv_nsec;
    452 
    453 	return 0;
    454 }
    455 
    456 int
    457 rumpuser_clock_sleep(uint64_t sec, uint64_t nsec, enum rumpclock clk)
    458 {
    459 	struct timespec rqt, rmt;
    460 	int nlocks;
    461 	int rv;
    462 
    463 	rumpkern_unsched(&nlocks, NULL);
    464 
    465 	/*LINTED*/
    466 	rqt.tv_sec = sec;
    467 	/*LINTED*/
    468 	rqt.tv_nsec = nsec;
    469 
    470 	switch (clk) {
    471 	case RUMPUSER_CLOCK_RELWALL:
    472 		do {
    473 			rv = nanosleep(&rqt, &rmt);
    474 			rqt = rmt;
    475 		} while (rv == -1 && errno == EINTR);
    476 		if (rv == -1) {
    477 			rv = errno;
    478 		}
    479 		break;
    480 	case RUMPUSER_CLOCK_ABSMONO:
    481 		do {
    482 #ifdef HAVE_CLOCK_NANOSLEEP
    483 			rv = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
    484 			    &rqt, NULL);
    485 #else
    486 			/* le/la/der/die/das sigh. timevalspec tailspin */
    487 			struct timespec ts, tsr;
    488 			clock_gettime(CLOCK_REALTIME, &ts);
    489 			if (ts.tv_sec == rqt.tv_sec ?
    490 			    ts.tv_nsec > rqt.tv_nsec : ts.tv_sec > rqt.tv_sec) {
    491 				rv = 0;
    492 			} else {
    493 				tsr.tv_sec = rqt.tv_sec - ts.tv_sec;
    494 				tsr.tv_nsec = rqt.tv_nsec - ts.tv_nsec;
    495 				if (tsr.tv_nsec < 0) {
    496 					tsr.tv_sec--;
    497 					tsr.tv_nsec += 1000*1000*1000;
    498 				}
    499 				rv = nanosleep(&tsr, NULL);
    500 			}
    501 #endif
    502 		} while (rv == -1 && errno == EINTR);
    503 		if (rv == -1) {
    504 			rv = errno;
    505 		}
    506 		break;
    507 	default:
    508 		abort();
    509 	}
    510 
    511 	rumpkern_sched(nlocks, NULL);
    512 	return rv;
    513 }
    514 
    515 int
    516 rumpuser_getenv(const char *name, char *buf, size_t blen, int *error)
    517 {
    518 
    519 	DOCALL(int, getenv_r(name, buf, blen));
    520 }
    521 
    522 int
    523 rumpuser_gethostname(char *name, size_t namelen, int *error)
    524 {
    525 	char tmp[MAXHOSTNAMELEN];
    526 
    527 	if (gethostname(tmp, sizeof(tmp)) == -1) {
    528 		snprintf(name, namelen, "rump-%05d.rumpdomain", (int)getpid());
    529 	} else {
    530 		snprintf(name, namelen, "rump-%05d.%s.rumpdomain",
    531 		    (int)getpid(), tmp);
    532 	}
    533 
    534 	*error = 0;
    535 	return 0;
    536 }
    537 
    538 int
    539 rumpuser_putchar(int c, int *error)
    540 {
    541 
    542 	DOCALL(int, (putchar(c)));
    543 }
    544 
    545 void
    546 rumpuser_exit(int rv)
    547 {
    548 
    549 	if (rv == RUMPUSER_PANIC)
    550 		abort();
    551 	else
    552 		exit(rv);
    553 }
    554 
    555 void
    556 rumpuser_seterrno(int error)
    557 {
    558 
    559 	errno = error;
    560 }
    561 
    562 /*
    563  * This is meant for safe debugging prints from the kernel.
    564  */
    565 int
    566 rumpuser_dprintf(const char *format, ...)
    567 {
    568 	va_list ap;
    569 	int rv;
    570 
    571 	va_start(ap, format);
    572 	rv = vfprintf(stderr, format, ap);
    573 	va_end(ap);
    574 
    575 	return rv;
    576 }
    577 
    578 int
    579 rumpuser_kill(int64_t pid, int sig, int *error)
    580 {
    581 
    582 #ifdef __NetBSD__
    583 	if (pid == RUMPUSER_PID_SELF) {
    584 		DOCALL(int, raise(sig));
    585 	} else {
    586 		DOCALL(int, kill((pid_t)pid, sig));
    587 	}
    588 #else
    589 	/* XXXfixme: signal numbers may not match on non-NetBSD */
    590 	seterror(EOPNOTSUPP);
    591 	return -1;
    592 #endif
    593 }
    594 
    595 int
    596 rumpuser_getnhostcpu(void)
    597 {
    598 	int ncpu = 1;
    599 
    600 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
    601 	size_t sz = sizeof(ncpu);
    602 
    603 	sysctlbyname("hw.ncpu", &ncpu, &sz, NULL, 0);
    604 #elif defined(__linux__) || defined(__CYGWIN__)
    605 	FILE *fp;
    606 	char *line = NULL;
    607 	size_t n = 0;
    608 
    609 	/* If anyone knows a better way, I'm all ears */
    610 	if ((fp = fopen("/proc/cpuinfo", "r")) != NULL) {
    611 		ncpu = 0;
    612 		while (getline(&line, &n, fp) != -1) {
    613 			if (strncmp(line,
    614 			    "processor", sizeof("processor")-1) == 0)
    615 			    	ncpu++;
    616 		}
    617 		if (ncpu == 0)
    618 			ncpu = 1;
    619 		free(line);
    620 		fclose(fp);
    621 	}
    622 #elif __sun__
    623 	/* XXX: this is just a rough estimate ... */
    624 	ncpu = sysconf(_SC_NPROCESSORS_ONLN);
    625 #endif
    626 
    627 	return ncpu;
    628 }
    629 
    630 size_t
    631 rumpuser_getrandom(void *buf, size_t buflen, int flags)
    632 {
    633 	size_t origlen = buflen;
    634 	uint32_t *p = buf;
    635 	uint32_t tmp;
    636 	int chunk;
    637 
    638 	do {
    639 		chunk = buflen < 4 ? buflen : 4; /* portable MIN ... */
    640 		tmp = RUMPUSER_RANDOM();
    641 		memcpy(p, &tmp, chunk);
    642 		p++;
    643 		buflen -= chunk;
    644 	} while (chunk);
    645 
    646 	return origlen;
    647 }
    648