Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser.c revision 1.60
      1 /*	$NetBSD: rumpuser.c,v 1.60 2014/07/09 23:41:40 justin 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.60 2014/07/09 23:41:40 justin Exp $");
     32 #endif /* !lint */
     33 
     34 #include <sys/stat.h>
     35 #include <sys/time.h>
     36 #include <sys/types.h>
     37 
     38 #include <assert.h>
     39 #include <errno.h>
     40 #include <fcntl.h>
     41 #include <signal.h>
     42 #include <stdarg.h>
     43 #include <stdint.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <time.h>
     48 #include <unistd.h>
     49 
     50 #include <rump/rumpuser.h>
     51 
     52 #include "rumpuser_int.h"
     53 
     54 struct rumpuser_hyperup rumpuser__hyp;
     55 
     56 int
     57 rumpuser_init(int version, const struct rumpuser_hyperup *hyp)
     58 {
     59 
     60 	if (version != RUMPUSER_VERSION) {
     61 		fprintf(stderr, "rumpuser mismatch, kern: %d, hypervisor %d\n",
     62 		    version, RUMPUSER_VERSION);
     63 		return 1;
     64 	}
     65 
     66 #ifdef RUMPUSER_USE_DEVRANDOM
     67 	uint32_t rv;
     68 	int fd;
     69 
     70 	if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
     71 		srandom(time(NULL));
     72 	} else {
     73 		if (read(fd, &rv, sizeof(rv)) != sizeof(rv))
     74 			srandom(time(NULL));
     75 		else
     76 			srandom(rv);
     77 		close(fd);
     78 	}
     79 #endif
     80 
     81 	rumpuser__thrinit();
     82 	rumpuser__hyp = *hyp;
     83 
     84 	return 0;
     85 }
     86 
     87 int
     88 rumpuser_clock_gettime(int enum_rumpclock, int64_t *sec, long *nsec)
     89 {
     90 	enum rumpclock rclk = enum_rumpclock;
     91 	struct timespec ts;
     92 	clockid_t clk;
     93 	int rv;
     94 
     95 	switch (rclk) {
     96 	case RUMPUSER_CLOCK_RELWALL:
     97 		clk = CLOCK_REALTIME;
     98 		break;
     99 	case RUMPUSER_CLOCK_ABSMONO:
    100 #ifdef HAVE_CLOCK_NANOSLEEP
    101 		clk = CLOCK_MONOTONIC;
    102 #else
    103 		clk = CLOCK_REALTIME;
    104 #endif
    105 		break;
    106 	default:
    107 		abort();
    108 	}
    109 
    110 	if (clock_gettime(clk, &ts) == -1) {
    111 		rv = errno;
    112 	} else {
    113 		*sec = ts.tv_sec;
    114 		*nsec = ts.tv_nsec;
    115 		rv = 0;
    116 	}
    117 
    118 	ET(rv);
    119 }
    120 
    121 int
    122 rumpuser_clock_sleep(int enum_rumpclock, int64_t sec, long nsec)
    123 {
    124 	enum rumpclock rclk = enum_rumpclock;
    125 	struct timespec rqt, rmt;
    126 	int nlocks;
    127 	int rv;
    128 
    129 	rumpkern_unsched(&nlocks, NULL);
    130 
    131 	/*LINTED*/
    132 	rqt.tv_sec = sec;
    133 	/*LINTED*/
    134 	rqt.tv_nsec = nsec;
    135 
    136 	switch (rclk) {
    137 	case RUMPUSER_CLOCK_RELWALL:
    138 		do {
    139 			rv = nanosleep(&rqt, &rmt);
    140 			rqt = rmt;
    141 		} while (rv == -1 && errno == EINTR);
    142 		if (rv == -1) {
    143 			rv = errno;
    144 		}
    145 		break;
    146 	case RUMPUSER_CLOCK_ABSMONO:
    147 		do {
    148 #ifdef HAVE_CLOCK_NANOSLEEP
    149 			rv = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
    150 			    &rqt, NULL);
    151 #else
    152 			/* le/la/der/die/das sigh. timevalspec tailspin */
    153 			struct timespec ts, tsr;
    154 			clock_gettime(CLOCK_REALTIME, &ts);
    155 			if (ts.tv_sec == rqt.tv_sec ?
    156 			    ts.tv_nsec > rqt.tv_nsec : ts.tv_sec > rqt.tv_sec) {
    157 				rv = 0;
    158 			} else {
    159 				tsr.tv_sec = rqt.tv_sec - ts.tv_sec;
    160 				tsr.tv_nsec = rqt.tv_nsec - ts.tv_nsec;
    161 				if (tsr.tv_nsec < 0) {
    162 					tsr.tv_sec--;
    163 					tsr.tv_nsec += 1000*1000*1000;
    164 				}
    165 				rv = nanosleep(&tsr, NULL);
    166 			}
    167 #endif
    168 		} while (rv == -1 && errno == EINTR);
    169 		if (rv == -1) {
    170 			rv = errno;
    171 		}
    172 		break;
    173 	default:
    174 		abort();
    175 	}
    176 
    177 	rumpkern_sched(nlocks, NULL);
    178 
    179 	ET(rv);
    180 }
    181 
    182 static int
    183 gethostncpu(void)
    184 {
    185 	int ncpu = 1; /* unknown, really */
    186 
    187 #ifdef _SC_NPROCESSORS_ONLN
    188 	ncpu = sysconf(_SC_NPROCESSORS_ONLN);
    189 #endif
    190 
    191 	return ncpu;
    192 }
    193 
    194 int
    195 rumpuser_getparam(const char *name, void *buf, size_t blen)
    196 {
    197 	int rv;
    198 
    199 	if (strcmp(name, RUMPUSER_PARAM_NCPU) == 0) {
    200 		int ncpu;
    201 
    202 		if (getenv_r("RUMP_NCPU", buf, blen) == -1) {
    203 			sprintf(buf, "2"); /* default */
    204 		} else if (strcmp(buf, "host") == 0) {
    205 			ncpu = gethostncpu();
    206 			snprintf(buf, blen, "%d", ncpu);
    207 		}
    208 		rv = 0;
    209 	} else if (strcmp(name, RUMPUSER_PARAM_HOSTNAME) == 0) {
    210 		char tmp[MAXHOSTNAMELEN];
    211 
    212 		if (gethostname(tmp, sizeof(tmp)) == -1) {
    213 			snprintf(buf, blen, "rump-%05d", (int)getpid());
    214 		} else {
    215 			snprintf(buf, blen, "rump-%05d.%s",
    216 			    (int)getpid(), tmp);
    217 		}
    218 		rv = 0;
    219 	} else if (*name == '_') {
    220 		rv = EINVAL;
    221 	} else {
    222 		if (getenv_r(name, buf, blen) == -1)
    223 			rv = errno;
    224 		else
    225 			rv = 0;
    226 	}
    227 
    228 	ET(rv);
    229 }
    230 
    231 void
    232 rumpuser_putchar(int c)
    233 {
    234 
    235 	putchar(c);
    236 }
    237 
    238 __dead void
    239 rumpuser_exit(int rv)
    240 {
    241 
    242 	if (rv == RUMPUSER_PANIC)
    243 		abort();
    244 	else
    245 		exit(rv);
    246 }
    247 
    248 void
    249 rumpuser_seterrno(int error)
    250 {
    251 
    252 	errno = error;
    253 }
    254 
    255 /*
    256  * This is meant for safe debugging prints from the kernel.
    257  */
    258 void
    259 rumpuser_dprintf(const char *format, ...)
    260 {
    261 	va_list ap;
    262 
    263 	va_start(ap, format);
    264 	vfprintf(stderr, format, ap);
    265 	va_end(ap);
    266 }
    267 
    268 int
    269 rumpuser_kill(int64_t pid, int rumpsig)
    270 {
    271 	int sig;
    272 
    273 	sig = rumpuser__sig_rump2host(rumpsig);
    274 	if (sig > 0)
    275 		raise(sig);
    276 	return 0;
    277 }
    278 
    279 int
    280 rumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp)
    281 {
    282 	size_t origlen = buflen;
    283 	uint32_t *p = buf;
    284 	uint32_t tmp;
    285 	int chunk;
    286 
    287 	do {
    288 		chunk = buflen < 4 ? buflen : 4; /* portable MIN ... */
    289 		tmp = RUMPUSER_RANDOM();
    290 		memcpy(p, &tmp, chunk);
    291 		p++;
    292 		buflen -= chunk;
    293 	} while (chunk);
    294 
    295 	*retp = origlen;
    296 	ET(0);
    297 }
    298