2013-10-27 20:39:46 +04:00
|
|
|
/* $NetBSD: rumpuser.c,v 1.55 2013/10/27 16:39:46 rmind Exp $ */
|
2007-08-06 02:28:00 +04:00
|
|
|
|
|
|
|
/*
|
2010-02-26 21:54:20 +03:00
|
|
|
* Copyright (c) 2007-2010 Antti Kantee. All Rights Reserved.
|
2007-08-06 02:28:00 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2012-07-27 13:09:05 +04:00
|
|
|
#include "rumpuser_port.h"
|
|
|
|
|
2008-12-18 03:21:52 +03:00
|
|
|
#if !defined(lint)
|
2013-10-27 20:39:46 +04:00
|
|
|
__RCSID("$NetBSD: rumpuser.c,v 1.55 2013/10/27 16:39:46 rmind Exp $");
|
2008-12-18 03:21:52 +03:00
|
|
|
#endif /* !lint */
|
|
|
|
|
2007-08-20 19:58:13 +04:00
|
|
|
#include <sys/ioctl.h>
|
2009-01-23 15:47:32 +03:00
|
|
|
#include <sys/mman.h>
|
2008-10-09 05:19:06 +04:00
|
|
|
#include <sys/uio.h>
|
2012-07-27 13:09:05 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2009-11-19 16:25:48 +03:00
|
|
|
#ifdef __NetBSD__
|
2011-02-07 00:05:53 +03:00
|
|
|
#include <sys/disk.h>
|
2009-11-19 16:25:48 +03:00
|
|
|
#include <sys/disklabel.h>
|
2011-02-07 00:05:53 +03:00
|
|
|
#include <sys/dkio.h>
|
2009-11-19 16:25:48 +03:00
|
|
|
#endif
|
|
|
|
|
2013-10-27 20:39:46 +04:00
|
|
|
#if defined(__NetBSD__) || defined(__FreeBSD__) || \
|
|
|
|
defined(__DragonFly__) || defined(__APPLE__)
|
|
|
|
#define __BSD__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__BSD__)
|
2012-12-14 14:48:48 +04:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-23 15:47:32 +03:00
|
|
|
#include <assert.h>
|
2007-08-06 02:28:00 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2012-11-18 23:29:40 +04:00
|
|
|
#include <netdb.h>
|
2010-04-21 15:13:29 +04:00
|
|
|
#include <signal.h>
|
2009-06-10 22:34:49 +04:00
|
|
|
#include <stdarg.h>
|
2007-08-20 19:58:13 +04:00
|
|
|
#include <stdint.h>
|
2007-08-06 02:28:00 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2007-08-16 02:13:15 +04:00
|
|
|
#include <string.h>
|
2008-06-24 18:11:44 +04:00
|
|
|
#include <time.h>
|
2007-08-06 02:28:00 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2008-10-03 01:37:59 +04:00
|
|
|
#include <rump/rumpuser.h>
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2008-10-03 01:37:59 +04:00
|
|
|
#include "rumpuser_int.h"
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2013-04-29 18:51:39 +04:00
|
|
|
struct rumpuser_hyperup rumpuser__hyp;
|
2013-04-27 18:59:08 +04:00
|
|
|
|
2010-06-09 18:08:17 +04:00
|
|
|
int
|
2013-04-29 18:51:39 +04:00
|
|
|
rumpuser_init(int version, const struct rumpuser_hyperup *hyp)
|
2010-06-09 18:08:17 +04:00
|
|
|
{
|
|
|
|
|
2013-04-27 18:59:08 +04:00
|
|
|
if (version != RUMPUSER_VERSION) {
|
|
|
|
fprintf(stderr, "rumpuser mismatch, kern: %d, hypervisor %d\n",
|
|
|
|
version, RUMPUSER_VERSION);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-04-27 20:56:29 +04:00
|
|
|
#ifdef RUMPUSER_USE_DEVRANDOM
|
2013-04-27 18:59:08 +04:00
|
|
|
uint32_t rv;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
|
|
|
|
srandom(time(NULL));
|
|
|
|
} else {
|
|
|
|
if (read(fd, &rv, sizeof(rv)) != sizeof(rv))
|
|
|
|
srandom(time(NULL));
|
|
|
|
else
|
|
|
|
srandom(rv);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rumpuser__thrinit();
|
2013-04-29 18:51:39 +04:00
|
|
|
rumpuser__hyp = *hyp;
|
2013-04-27 18:59:08 +04:00
|
|
|
|
|
|
|
return 0;
|
2010-06-09 18:08:17 +04:00
|
|
|
}
|
|
|
|
|
2007-08-06 02:28:00 +04:00
|
|
|
int
|
2013-04-30 04:03:52 +04:00
|
|
|
rumpuser_getfileinfo(const char *path, uint64_t *sizep, int *ftp)
|
2007-08-06 02:28:00 +04:00
|
|
|
{
|
2009-02-26 03:32:49 +03:00
|
|
|
struct stat sb;
|
2013-04-30 04:03:52 +04:00
|
|
|
uint64_t size = 0;
|
|
|
|
int needsdev = 0, rv = 0, ft = 0;
|
2011-01-05 12:43:00 +03:00
|
|
|
int fd = -1;
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2009-11-19 16:25:48 +03:00
|
|
|
if (stat(path, &sb) == -1) {
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = errno;
|
|
|
|
goto out;
|
2009-02-26 03:32:49 +03:00
|
|
|
}
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2009-02-26 03:32:49 +03:00
|
|
|
switch (sb.st_mode & S_IFMT) {
|
|
|
|
case S_IFDIR:
|
2010-02-18 15:21:28 +03:00
|
|
|
ft = RUMPUSER_FT_DIR;
|
2009-02-26 03:32:49 +03:00
|
|
|
break;
|
|
|
|
case S_IFREG:
|
2010-02-18 15:21:28 +03:00
|
|
|
ft = RUMPUSER_FT_REG;
|
2009-02-26 03:32:49 +03:00
|
|
|
break;
|
|
|
|
case S_IFBLK:
|
2010-02-18 15:21:28 +03:00
|
|
|
ft = RUMPUSER_FT_BLK;
|
2009-11-19 16:25:48 +03:00
|
|
|
needsdev = 1;
|
2009-02-26 03:32:49 +03:00
|
|
|
break;
|
2009-04-07 22:35:49 +04:00
|
|
|
case S_IFCHR:
|
2010-02-18 15:21:28 +03:00
|
|
|
ft = RUMPUSER_FT_CHR;
|
2009-11-19 16:25:48 +03:00
|
|
|
needsdev = 1;
|
2009-04-07 22:35:49 +04:00
|
|
|
break;
|
2009-02-26 03:32:49 +03:00
|
|
|
default:
|
2010-02-18 15:21:28 +03:00
|
|
|
ft = RUMPUSER_FT_OTHER;
|
2009-02-26 03:32:49 +03:00
|
|
|
break;
|
|
|
|
}
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2009-11-19 16:25:48 +03:00
|
|
|
if (!needsdev) {
|
2010-02-18 15:21:28 +03:00
|
|
|
size = sb.st_size;
|
2010-02-18 15:32:30 +03:00
|
|
|
} else if (sizep) {
|
2009-11-19 16:25:48 +03:00
|
|
|
/*
|
|
|
|
* Welcome to the jungle. Of course querying the kernel
|
|
|
|
* for a device partition size is supposed to be far from
|
|
|
|
* trivial. On NetBSD we use ioctl. On $other platform
|
|
|
|
* we have a problem. We try "the lseek trick" and just
|
|
|
|
* fail if that fails. Platform specific code can later
|
|
|
|
* be written here if appropriate.
|
|
|
|
*
|
|
|
|
* On NetBSD we hope and pray that for block devices nobody
|
|
|
|
* else is holding them open, because otherwise the kernel
|
|
|
|
* will not permit us to open it. Thankfully, this is
|
|
|
|
* usually called only in bootstrap and then we can
|
|
|
|
* forget about it.
|
|
|
|
*/
|
|
|
|
#ifndef __NetBSD__
|
|
|
|
off_t off;
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = errno;
|
2010-02-18 15:21:28 +03:00
|
|
|
goto out;
|
2009-11-19 16:25:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
off = lseek(fd, 0, SEEK_END);
|
|
|
|
if (off != 0) {
|
2010-02-18 15:21:28 +03:00
|
|
|
size = off;
|
|
|
|
goto out;
|
2009-11-19 16:25:48 +03:00
|
|
|
}
|
|
|
|
fprintf(stderr, "error: device size query not implemented on "
|
|
|
|
"this platform\n");
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = EOPNOTSUPP;
|
2010-02-18 15:21:28 +03:00
|
|
|
goto out;
|
2009-11-19 16:25:48 +03:00
|
|
|
#else
|
|
|
|
struct disklabel lab;
|
|
|
|
struct partition *parta;
|
2011-02-07 00:05:53 +03:00
|
|
|
struct dkwedge_info dkw;
|
2009-11-19 16:25:48 +03:00
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = errno;
|
2010-02-18 15:21:28 +03:00
|
|
|
goto out;
|
2009-11-19 16:25:48 +03:00
|
|
|
}
|
|
|
|
|
2011-02-07 00:05:53 +03:00
|
|
|
if (ioctl(fd, DIOCGDINFO, &lab) == 0) {
|
|
|
|
parta = &lab.d_partitions[DISKPART(sb.st_rdev)];
|
|
|
|
size = (uint64_t)lab.d_secsize * parta->p_size;
|
2010-02-18 15:21:28 +03:00
|
|
|
goto out;
|
2009-11-19 16:25:48 +03:00
|
|
|
}
|
|
|
|
|
2011-02-07 00:05:53 +03:00
|
|
|
if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
|
|
|
|
/*
|
|
|
|
* XXX: should use DIOCGDISKINFO to query
|
|
|
|
* sector size, but that requires proplib,
|
|
|
|
* so just don't bother for now. it's nice
|
|
|
|
* that something as difficult as figuring out
|
|
|
|
* a partition's size has been made so easy.
|
|
|
|
*/
|
|
|
|
size = dkw.dkw_size << DEV_BSHIFT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = errno;
|
2009-11-19 16:25:48 +03:00
|
|
|
#endif /* __NetBSD__ */
|
|
|
|
}
|
|
|
|
|
2010-02-18 15:21:28 +03:00
|
|
|
out:
|
|
|
|
if (rv == 0 && sizep)
|
|
|
|
*sizep = size;
|
|
|
|
if (rv == 0 && ftp)
|
|
|
|
*ftp = ft;
|
2011-01-05 12:43:00 +03:00
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
2010-02-18 15:21:28 +03:00
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2007-08-06 02:28:00 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
int
|
|
|
|
rumpuser_malloc(size_t howmuch, int alignment, void **memp)
|
2007-08-06 02:28:00 +04:00
|
|
|
{
|
2010-06-02 00:11:33 +04:00
|
|
|
void *mem;
|
2010-06-02 22:15:35 +04:00
|
|
|
int rv;
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2010-06-02 00:11:33 +04:00
|
|
|
if (alignment == 0)
|
|
|
|
alignment = sizeof(void *);
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2011-01-22 17:22:10 +03:00
|
|
|
rv = posix_memalign(&mem, (size_t)alignment, howmuch);
|
2010-06-02 22:15:35 +04:00
|
|
|
if (__predict_false(rv != 0)) {
|
|
|
|
if (rv == EINVAL) {
|
|
|
|
printf("rumpuser_malloc: invalid alignment %d\n",
|
|
|
|
alignment);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 00:11:33 +04:00
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
*memp = mem;
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2007-08-06 02:28:00 +04:00
|
|
|
}
|
|
|
|
|
2013-04-27 19:34:53 +04:00
|
|
|
/*ARGSUSED1*/
|
2007-08-06 02:28:00 +04:00
|
|
|
void
|
2013-04-27 19:34:53 +04:00
|
|
|
rumpuser_free(void *ptr, size_t size)
|
2007-08-06 02:28:00 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
int
|
2010-06-09 15:35:36 +04:00
|
|
|
rumpuser_anonmmap(void *prefaddr, size_t size, int alignbit,
|
2013-04-30 04:03:52 +04:00
|
|
|
int exec, void **memp)
|
2009-01-23 15:47:32 +03:00
|
|
|
{
|
2013-04-30 04:03:52 +04:00
|
|
|
void *mem;
|
|
|
|
int prot, rv;
|
2009-01-23 15:47:32 +03:00
|
|
|
|
2012-07-27 13:09:05 +04:00
|
|
|
#ifndef MAP_ALIGNED
|
|
|
|
#define MAP_ALIGNED(a) 0
|
|
|
|
if (alignbit)
|
|
|
|
fprintf(stderr, "rumpuser_anonmmap: warning, requested "
|
|
|
|
"alignment not supported by hypervisor\n");
|
|
|
|
#endif
|
|
|
|
|
2009-01-23 15:47:32 +03:00
|
|
|
prot = PROT_READ|PROT_WRITE;
|
|
|
|
if (exec)
|
|
|
|
prot |= PROT_EXEC;
|
2013-04-30 04:03:52 +04:00
|
|
|
mem = mmap(prefaddr, size, prot,
|
2012-12-13 19:35:09 +04:00
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_ALIGNED(alignbit), -1, 0);
|
2013-04-30 04:03:52 +04:00
|
|
|
if (mem == MAP_FAILED) {
|
|
|
|
rv = errno;
|
|
|
|
} else {
|
|
|
|
*memp = mem;
|
|
|
|
rv = 0;
|
2009-01-27 13:05:08 +03:00
|
|
|
}
|
2013-04-30 04:03:52 +04:00
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2009-01-23 15:47:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_unmap(void *addr, size_t len)
|
|
|
|
{
|
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
munmap(addr, len);
|
2009-01-23 15:47:32 +03:00
|
|
|
}
|
|
|
|
|
2007-08-06 02:28:00 +04:00
|
|
|
int
|
2013-04-30 04:03:52 +04:00
|
|
|
rumpuser_open(const char *path, int ruflags, int *fdp)
|
2007-08-06 02:28:00 +04:00
|
|
|
{
|
2013-04-30 04:03:52 +04:00
|
|
|
int fd, flags, rv;
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2012-09-14 20:29:21 +04:00
|
|
|
switch (ruflags & RUMPUSER_OPEN_ACCMODE) {
|
|
|
|
case RUMPUSER_OPEN_RDONLY:
|
|
|
|
flags = O_RDONLY;
|
|
|
|
break;
|
|
|
|
case RUMPUSER_OPEN_WRONLY:
|
|
|
|
flags = O_WRONLY;
|
|
|
|
break;
|
|
|
|
case RUMPUSER_OPEN_RDWR:
|
|
|
|
flags = O_RDWR;
|
|
|
|
break;
|
|
|
|
default:
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2012-09-14 20:29:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define TESTSET(_ru_, _h_) if (ruflags & _ru_) flags |= _h_;
|
|
|
|
TESTSET(RUMPUSER_OPEN_CREATE, O_CREAT);
|
|
|
|
TESTSET(RUMPUSER_OPEN_EXCL, O_EXCL);
|
|
|
|
#undef TESTSET
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
KLOCK_WRAP(fd = open(path, flags, 0644));
|
|
|
|
if (fd == -1) {
|
|
|
|
rv = errno;
|
|
|
|
} else {
|
|
|
|
*fdp = fd;
|
|
|
|
rv = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2007-08-06 02:28:00 +04:00
|
|
|
}
|
|
|
|
|
2007-09-10 23:11:44 +04:00
|
|
|
int
|
2013-04-30 04:03:52 +04:00
|
|
|
rumpuser_close(int fd)
|
2007-08-06 02:28:00 +04:00
|
|
|
{
|
2013-04-29 19:40:38 +04:00
|
|
|
int nlocks;
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2013-04-29 19:40:38 +04:00
|
|
|
rumpkern_unsched(&nlocks, NULL);
|
|
|
|
fsync(fd);
|
|
|
|
close(fd);
|
|
|
|
rumpkern_sched(nlocks, NULL);
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(0);
|
2007-09-21 03:43:44 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 00:08:48 +04:00
|
|
|
/*
|
|
|
|
* Assume "struct rumpuser_iovec" and "struct iovec" are the same.
|
|
|
|
* If you encounter POSIX platforms where they aren't, add some
|
|
|
|
* translation for iovlen > 1.
|
|
|
|
*/
|
2013-04-30 04:03:52 +04:00
|
|
|
int
|
2013-04-30 00:08:48 +04:00
|
|
|
rumpuser_iovread(int fd, struct rumpuser_iovec *ruiov, size_t iovlen,
|
2013-05-15 18:58:24 +04:00
|
|
|
int64_t roff, size_t *retp)
|
2009-02-26 03:59:31 +03:00
|
|
|
{
|
2013-04-30 00:08:48 +04:00
|
|
|
struct iovec *iov = (struct iovec *)ruiov;
|
2013-05-15 18:58:24 +04:00
|
|
|
off_t off = (off_t)roff;
|
2013-04-30 04:03:52 +04:00
|
|
|
ssize_t nn;
|
|
|
|
int rv;
|
2009-02-26 03:59:31 +03:00
|
|
|
|
2013-04-30 15:26:26 +04:00
|
|
|
if (off == RUMPUSER_IOV_NOSEEK) {
|
2013-04-30 04:03:52 +04:00
|
|
|
KLOCK_WRAP(nn = readv(fd, iov, iovlen));
|
2013-04-30 15:26:26 +04:00
|
|
|
} else {
|
|
|
|
int nlocks;
|
|
|
|
|
|
|
|
rumpkern_unsched(&nlocks, NULL);
|
|
|
|
if (lseek(fd, off, SEEK_SET) == off) {
|
|
|
|
nn = readv(fd, iov, iovlen);
|
|
|
|
} else {
|
|
|
|
nn = -1;
|
|
|
|
}
|
|
|
|
rumpkern_sched(nlocks, NULL);
|
|
|
|
}
|
2009-02-26 03:59:31 +03:00
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
if (nn == -1) {
|
|
|
|
rv = errno;
|
|
|
|
} else {
|
|
|
|
*retp = (size_t)nn;
|
|
|
|
rv = 0;
|
|
|
|
}
|
2009-02-26 03:59:31 +03:00
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2009-02-26 03:59:31 +03:00
|
|
|
}
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
int
|
2013-04-30 00:08:48 +04:00
|
|
|
rumpuser_iovwrite(int fd, const struct rumpuser_iovec *ruiov, size_t iovlen,
|
2013-05-15 18:58:24 +04:00
|
|
|
int64_t roff, size_t *retp)
|
2009-02-26 03:59:31 +03:00
|
|
|
{
|
2013-04-30 00:08:48 +04:00
|
|
|
const struct iovec *iov = (const struct iovec *)ruiov;
|
2013-05-15 18:58:24 +04:00
|
|
|
off_t off = (off_t)roff;
|
2013-04-30 04:03:52 +04:00
|
|
|
ssize_t nn;
|
|
|
|
int rv;
|
2009-02-26 03:59:31 +03:00
|
|
|
|
2013-04-30 15:26:26 +04:00
|
|
|
if (off == RUMPUSER_IOV_NOSEEK) {
|
2013-04-30 04:03:52 +04:00
|
|
|
KLOCK_WRAP(nn = writev(fd, iov, iovlen));
|
2013-04-30 15:26:26 +04:00
|
|
|
} else {
|
|
|
|
int nlocks;
|
|
|
|
|
|
|
|
rumpkern_unsched(&nlocks, NULL);
|
|
|
|
if (lseek(fd, off, SEEK_SET) == off) {
|
|
|
|
nn = writev(fd, iov, iovlen);
|
|
|
|
} else {
|
|
|
|
nn = -1;
|
|
|
|
}
|
|
|
|
rumpkern_sched(nlocks, NULL);
|
|
|
|
}
|
2009-02-26 03:59:31 +03:00
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
if (nn == -1) {
|
|
|
|
rv = errno;
|
|
|
|
} else {
|
|
|
|
*retp = (size_t)nn;
|
|
|
|
rv = 0;
|
|
|
|
}
|
2009-02-26 03:59:31 +03:00
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2009-02-26 03:59:31 +03:00
|
|
|
}
|
|
|
|
|
2013-05-15 19:57:01 +04:00
|
|
|
int
|
|
|
|
rumpuser_syncfd(int fd, int flags, uint64_t start, uint64_t len)
|
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For now, assume fd is regular file and does not care
|
|
|
|
* about read syncing
|
|
|
|
*/
|
|
|
|
if ((flags & RUMPUSER_SYNCFD_BOTH) == 0) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((flags & RUMPUSER_SYNCFD_WRITE) == 0) {
|
|
|
|
rv = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __NetBSD__
|
|
|
|
{
|
|
|
|
int fsflags = FDATASYNC;
|
|
|
|
|
|
|
|
if (fsflags & RUMPUSER_SYNCFD_SYNC)
|
|
|
|
fsflags |= FDISKSYNC;
|
|
|
|
if (fsync_range(fd, fsflags, start, len) == -1)
|
|
|
|
rv = errno;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* el-simplo */
|
|
|
|
if (fsync(fd) == -1)
|
|
|
|
rv = errno;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
out:
|
|
|
|
ET(rv);
|
|
|
|
}
|
|
|
|
|
2007-08-06 02:28:00 +04:00
|
|
|
int
|
2013-05-15 18:52:49 +04:00
|
|
|
rumpuser_clock_gettime(int enum_rumpclock, int64_t *sec, long *nsec)
|
2007-08-06 02:28:00 +04:00
|
|
|
{
|
2013-05-15 18:52:49 +04:00
|
|
|
enum rumpclock rclk = enum_rumpclock;
|
2013-04-28 17:17:24 +04:00
|
|
|
struct timespec ts;
|
|
|
|
clockid_t clk;
|
2009-02-27 18:15:19 +03:00
|
|
|
int rv;
|
|
|
|
|
2013-04-28 17:17:24 +04:00
|
|
|
switch (rclk) {
|
|
|
|
case RUMPUSER_CLOCK_RELWALL:
|
|
|
|
clk = CLOCK_REALTIME;
|
|
|
|
break;
|
|
|
|
case RUMPUSER_CLOCK_ABSMONO:
|
|
|
|
#ifdef HAVE_CLOCK_NANOSLEEP
|
|
|
|
clk = CLOCK_MONOTONIC;
|
|
|
|
#else
|
|
|
|
clk = CLOCK_REALTIME;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
2009-02-27 18:15:19 +03:00
|
|
|
}
|
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
if (clock_gettime(clk, &ts) == -1) {
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = errno;
|
|
|
|
} else {
|
|
|
|
*sec = ts.tv_sec;
|
|
|
|
*nsec = ts.tv_nsec;
|
|
|
|
rv = 0;
|
2013-04-28 17:17:24 +04:00
|
|
|
}
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2007-08-06 02:28:00 +04:00
|
|
|
}
|
|
|
|
|
2013-04-28 17:17:24 +04:00
|
|
|
int
|
2013-05-15 18:52:49 +04:00
|
|
|
rumpuser_clock_sleep(int enum_rumpclock, int64_t sec, long nsec)
|
2013-04-28 17:17:24 +04:00
|
|
|
{
|
2013-05-15 18:52:49 +04:00
|
|
|
enum rumpclock rclk = enum_rumpclock;
|
2013-04-28 17:17:24 +04:00
|
|
|
struct timespec rqt, rmt;
|
|
|
|
int nlocks;
|
|
|
|
int rv;
|
|
|
|
|
2013-04-29 18:51:39 +04:00
|
|
|
rumpkern_unsched(&nlocks, NULL);
|
2013-04-28 17:17:24 +04:00
|
|
|
|
|
|
|
/*LINTED*/
|
|
|
|
rqt.tv_sec = sec;
|
|
|
|
/*LINTED*/
|
|
|
|
rqt.tv_nsec = nsec;
|
|
|
|
|
2013-05-15 18:52:49 +04:00
|
|
|
switch (rclk) {
|
2013-04-28 17:17:24 +04:00
|
|
|
case RUMPUSER_CLOCK_RELWALL:
|
|
|
|
do {
|
|
|
|
rv = nanosleep(&rqt, &rmt);
|
|
|
|
rqt = rmt;
|
|
|
|
} while (rv == -1 && errno == EINTR);
|
|
|
|
if (rv == -1) {
|
|
|
|
rv = errno;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RUMPUSER_CLOCK_ABSMONO:
|
|
|
|
do {
|
|
|
|
#ifdef HAVE_CLOCK_NANOSLEEP
|
|
|
|
rv = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
|
|
|
|
&rqt, NULL);
|
|
|
|
#else
|
|
|
|
/* le/la/der/die/das sigh. timevalspec tailspin */
|
|
|
|
struct timespec ts, tsr;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
if (ts.tv_sec == rqt.tv_sec ?
|
|
|
|
ts.tv_nsec > rqt.tv_nsec : ts.tv_sec > rqt.tv_sec) {
|
|
|
|
rv = 0;
|
|
|
|
} else {
|
|
|
|
tsr.tv_sec = rqt.tv_sec - ts.tv_sec;
|
|
|
|
tsr.tv_nsec = rqt.tv_nsec - ts.tv_nsec;
|
|
|
|
if (tsr.tv_nsec < 0) {
|
|
|
|
tsr.tv_sec--;
|
|
|
|
tsr.tv_nsec += 1000*1000*1000;
|
|
|
|
}
|
|
|
|
rv = nanosleep(&tsr, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} while (rv == -1 && errno == EINTR);
|
|
|
|
if (rv == -1) {
|
|
|
|
rv = errno;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +04:00
|
|
|
rumpkern_sched(nlocks, NULL);
|
2013-04-30 16:39:20 +04:00
|
|
|
|
|
|
|
ET(rv);
|
2013-04-28 17:17:24 +04:00
|
|
|
}
|
|
|
|
|
2013-04-29 21:31:05 +04:00
|
|
|
static int
|
|
|
|
gethostncpu(void)
|
2008-03-11 13:50:16 +03:00
|
|
|
{
|
2013-04-29 21:31:05 +04:00
|
|
|
int ncpu = 1;
|
|
|
|
|
2013-10-27 20:39:46 +04:00
|
|
|
#if defined(__BSD__)
|
2013-04-29 21:31:05 +04:00
|
|
|
size_t sz = sizeof(ncpu);
|
|
|
|
|
|
|
|
sysctlbyname("hw.ncpu", &ncpu, &sz, NULL, 0);
|
|
|
|
#elif defined(__linux__) || defined(__CYGWIN__)
|
|
|
|
FILE *fp;
|
|
|
|
char *line = NULL;
|
|
|
|
size_t n = 0;
|
2008-03-11 13:50:16 +03:00
|
|
|
|
2013-04-29 21:31:05 +04:00
|
|
|
/* If anyone knows a better way, I'm all ears */
|
|
|
|
if ((fp = fopen("/proc/cpuinfo", "r")) != NULL) {
|
|
|
|
ncpu = 0;
|
|
|
|
while (getline(&line, &n, fp) != -1) {
|
|
|
|
if (strncmp(line,
|
|
|
|
"processor", sizeof("processor")-1) == 0)
|
|
|
|
ncpu++;
|
|
|
|
}
|
|
|
|
if (ncpu == 0)
|
|
|
|
ncpu = 1;
|
|
|
|
free(line);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
#elif __sun__
|
|
|
|
/* XXX: this is just a rough estimate ... */
|
|
|
|
ncpu = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ncpu;
|
2008-03-11 13:50:16 +03:00
|
|
|
}
|
|
|
|
|
2007-08-06 02:28:00 +04:00
|
|
|
int
|
2013-04-29 21:31:05 +04:00
|
|
|
rumpuser_getparam(const char *name, void *buf, size_t blen)
|
2007-08-06 02:28:00 +04:00
|
|
|
{
|
2013-04-30 04:03:52 +04:00
|
|
|
int rv;
|
2007-08-06 02:28:00 +04:00
|
|
|
|
2013-04-29 21:31:05 +04:00
|
|
|
if (strcmp(name, RUMPUSER_PARAM_NCPU) == 0) {
|
|
|
|
int ncpu;
|
|
|
|
|
|
|
|
if (getenv_r("RUMP_NCPU", buf, blen) == -1) {
|
2013-08-14 12:29:25 +04:00
|
|
|
sprintf(buf, "2"); /* default */
|
|
|
|
} else if (strcmp(buf, "host") == 0) {
|
2013-04-29 21:31:05 +04:00
|
|
|
ncpu = gethostncpu();
|
|
|
|
snprintf(buf, blen, "%d", ncpu);
|
|
|
|
}
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = 0;
|
2013-04-29 21:31:05 +04:00
|
|
|
} else if (strcmp(name, RUMPUSER_PARAM_HOSTNAME) == 0) {
|
|
|
|
char tmp[MAXHOSTNAMELEN];
|
|
|
|
|
|
|
|
if (gethostname(tmp, sizeof(tmp)) == -1) {
|
|
|
|
snprintf(buf, blen, "rump-%05d", (int)getpid());
|
|
|
|
} else {
|
|
|
|
snprintf(buf, blen, "rump-%05d.%s",
|
|
|
|
(int)getpid(), tmp);
|
|
|
|
}
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = 0;
|
2013-04-29 21:31:05 +04:00
|
|
|
} else if (*name == '_') {
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = EINVAL;
|
2011-01-20 18:00:12 +03:00
|
|
|
} else {
|
2013-04-29 21:31:05 +04:00
|
|
|
if (getenv_r(name, buf, blen) == -1)
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = errno;
|
2013-04-29 21:31:05 +04:00
|
|
|
else
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = 0;
|
2011-01-20 18:00:12 +03:00
|
|
|
}
|
2013-04-30 04:03:52 +04:00
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2007-08-06 02:28:00 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
void
|
|
|
|
rumpuser_putchar(int c)
|
2009-01-02 05:54:13 +03:00
|
|
|
{
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
putchar(c);
|
2009-01-02 05:54:13 +03:00
|
|
|
}
|
|
|
|
|
2013-10-27 20:39:46 +04:00
|
|
|
__dead void
|
2009-10-09 18:41:36 +04:00
|
|
|
rumpuser_exit(int rv)
|
2009-01-02 05:54:13 +03:00
|
|
|
{
|
|
|
|
|
2009-10-09 18:41:36 +04:00
|
|
|
if (rv == RUMPUSER_PANIC)
|
|
|
|
abort();
|
|
|
|
else
|
|
|
|
exit(rv);
|
2009-01-02 05:54:13 +03:00
|
|
|
}
|
2009-01-23 22:19:32 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
rumpuser_seterrno(int error)
|
|
|
|
{
|
|
|
|
|
|
|
|
errno = error;
|
|
|
|
}
|
2009-02-28 18:49:12 +03:00
|
|
|
|
2009-06-10 22:36:33 +04:00
|
|
|
/*
|
|
|
|
* This is meant for safe debugging prints from the kernel.
|
|
|
|
*/
|
2013-04-30 04:03:52 +04:00
|
|
|
void
|
2009-06-10 22:34:49 +04:00
|
|
|
rumpuser_dprintf(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2013-04-30 04:03:52 +04:00
|
|
|
vfprintf(stderr, format, ap);
|
2009-06-10 22:34:49 +04:00
|
|
|
va_end(ap);
|
|
|
|
}
|
2010-04-21 15:13:29 +04:00
|
|
|
|
|
|
|
int
|
2013-04-30 04:03:52 +04:00
|
|
|
rumpuser_kill(int64_t pid, int sig)
|
2010-04-21 15:13:29 +04:00
|
|
|
{
|
2013-05-01 21:17:54 +04:00
|
|
|
int rv;
|
2010-04-21 15:13:29 +04:00
|
|
|
|
2010-04-21 15:16:41 +04:00
|
|
|
#ifdef __NetBSD__
|
2013-05-01 21:17:54 +04:00
|
|
|
int error;
|
|
|
|
|
2010-04-21 15:13:29 +04:00
|
|
|
if (pid == RUMPUSER_PID_SELF) {
|
2013-04-30 04:03:52 +04:00
|
|
|
error = raise(sig);
|
2010-04-21 15:13:29 +04:00
|
|
|
} else {
|
2013-04-30 04:03:52 +04:00
|
|
|
error = kill((pid_t)pid, sig);
|
2010-04-21 15:13:29 +04:00
|
|
|
}
|
2013-04-30 04:03:52 +04:00
|
|
|
if (error == -1)
|
|
|
|
rv = errno;
|
|
|
|
else
|
|
|
|
rv = 0;
|
2010-04-21 15:16:41 +04:00
|
|
|
#else
|
|
|
|
/* XXXfixme: signal numbers may not match on non-NetBSD */
|
2013-04-30 04:03:52 +04:00
|
|
|
rv = EOPNOTSUPP;
|
2010-04-21 15:16:41 +04:00
|
|
|
#endif
|
2013-04-30 04:03:52 +04:00
|
|
|
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(rv);
|
2010-04-21 15:13:29 +04:00
|
|
|
}
|
2010-04-28 04:33:45 +04:00
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
int
|
|
|
|
rumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp)
|
2011-11-28 12:05:05 +04:00
|
|
|
{
|
2013-04-27 20:56:29 +04:00
|
|
|
size_t origlen = buflen;
|
|
|
|
uint32_t *p = buf;
|
|
|
|
uint32_t tmp;
|
|
|
|
int chunk;
|
2012-07-27 13:09:05 +04:00
|
|
|
|
2013-04-27 20:56:29 +04:00
|
|
|
do {
|
|
|
|
chunk = buflen < 4 ? buflen : 4; /* portable MIN ... */
|
|
|
|
tmp = RUMPUSER_RANDOM();
|
|
|
|
memcpy(p, &tmp, chunk);
|
|
|
|
p++;
|
|
|
|
buflen -= chunk;
|
|
|
|
} while (chunk);
|
|
|
|
|
2013-04-30 04:03:52 +04:00
|
|
|
*retp = origlen;
|
2013-04-30 16:39:20 +04:00
|
|
|
ET(0);
|
2011-11-28 12:05:05 +04:00
|
|
|
}
|