2014-07-25 18:00:31 +04:00
|
|
|
/* $NetBSD: rumpuser.c,v 1.63 2014/07/25 14:00:31 justin 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)
|
2014-07-25 18:00:31 +04:00
|
|
|
__RCSID("$NetBSD: rumpuser.c,v 1.63 2014/07/25 14:00:31 justin Exp $");
|
2008-12-18 03:21:52 +03:00
|
|
|
#endif /* !lint */
|
|
|
|
|
2012-07-27 13:09:05 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
2014-07-10 03:41:40 +04:00
|
|
|
#include <sys/types.h>
|
2012-12-14 14:48:48 +04:00
|
|
|
|
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>
|
2014-07-10 12:17:43 +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
|
|
|
{
|
2014-07-25 18:00:31 +04:00
|
|
|
int rv;
|
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);
|
2014-07-25 18:00:31 +04:00
|
|
|
abort();
|
2013-04-27 18:59:08 +04:00
|
|
|
}
|
|
|
|
|
2014-07-25 18:00:31 +04:00
|
|
|
rv = rumpuser__random_init();
|
|
|
|
if (rv != 0) {
|
|
|
|
ET(rv);
|
2013-04-27 18:59:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
{
|
2014-04-02 17:54:42 +04:00
|
|
|
int ncpu = 1; /* unknown, really */
|
2013-04-29 21:31:05 +04:00
|
|
|
|
2014-04-02 17:54:42 +04:00
|
|
|
#ifdef _SC_NPROCESSORS_ONLN
|
2013-04-29 21:31:05 +04:00
|
|
|
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
|
2014-02-20 04:42:27 +04:00
|
|
|
rumpuser_kill(int64_t pid, int rumpsig)
|
2010-04-21 15:13:29 +04:00
|
|
|
{
|
2014-02-20 04:44:20 +04:00
|
|
|
int sig;
|
2013-05-01 21:17:54 +04:00
|
|
|
|
2014-02-20 04:42:27 +04:00
|
|
|
sig = rumpuser__sig_rump2host(rumpsig);
|
2014-02-20 04:44:20 +04:00
|
|
|
if (sig > 0)
|
|
|
|
raise(sig);
|
|
|
|
return 0;
|
2010-04-21 15:13:29 +04:00
|
|
|
}
|