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