rumpuser.c revision 1.45 1 /* $NetBSD: rumpuser.c,v 1.45 2013/04/30 00:03:52 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.45 2013/04/30 00:03:52 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)
102 {
103 struct stat sb;
104 uint64_t size = 0;
105 int needsdev = 0, rv = 0, ft = 0;
106 int fd = -1;
107
108 if (stat(path, &sb) == -1) {
109 rv = errno;
110 goto out;
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 rv = errno;
156 goto out;
157 }
158
159 off = lseek(fd, 0, SEEK_END);
160 if (off != 0) {
161 size = off;
162 goto out;
163 }
164 fprintf(stderr, "error: device size query not implemented on "
165 "this platform\n");
166 rv = EOPNOTSUPP;
167 goto out;
168 #else
169 struct disklabel lab;
170 struct partition *parta;
171 struct dkwedge_info dkw;
172
173 fd = open(path, O_RDONLY);
174 if (fd == -1) {
175 rv = errno;
176 goto out;
177 }
178
179 if (ioctl(fd, DIOCGDINFO, &lab) == 0) {
180 parta = &lab.d_partitions[DISKPART(sb.st_rdev)];
181 size = (uint64_t)lab.d_secsize * parta->p_size;
182 goto out;
183 }
184
185 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
186 /*
187 * XXX: should use DIOCGDISKINFO to query
188 * sector size, but that requires proplib,
189 * so just don't bother for now. it's nice
190 * that something as difficult as figuring out
191 * a partition's size has been made so easy.
192 */
193 size = dkw.dkw_size << DEV_BSHIFT;
194 goto out;
195 }
196
197 rv = errno;
198 #endif /* __NetBSD__ */
199 }
200
201 out:
202 if (rv == 0 && sizep)
203 *sizep = size;
204 if (rv == 0 && ftp)
205 *ftp = ft;
206 if (fd != -1)
207 close(fd);
208
209 return rv;
210 }
211
212 int
213 rumpuser_malloc(size_t howmuch, int alignment, void **memp)
214 {
215 void *mem;
216 int rv;
217
218 if (alignment == 0)
219 alignment = sizeof(void *);
220
221 rv = posix_memalign(&mem, (size_t)alignment, howmuch);
222 if (__predict_false(rv != 0)) {
223 if (rv == EINVAL) {
224 printf("rumpuser_malloc: invalid alignment %d\n",
225 alignment);
226 abort();
227 }
228 }
229
230 *memp = mem;
231 return rv;
232 }
233
234 /*ARGSUSED1*/
235 void
236 rumpuser_free(void *ptr, size_t size)
237 {
238
239 free(ptr);
240 }
241
242 int
243 rumpuser_anonmmap(void *prefaddr, size_t size, int alignbit,
244 int exec, void **memp)
245 {
246 void *mem;
247 int prot, rv;
248
249 #ifndef MAP_ALIGNED
250 #define MAP_ALIGNED(a) 0
251 if (alignbit)
252 fprintf(stderr, "rumpuser_anonmmap: warning, requested "
253 "alignment not supported by hypervisor\n");
254 #endif
255
256 prot = PROT_READ|PROT_WRITE;
257 if (exec)
258 prot |= PROT_EXEC;
259 mem = mmap(prefaddr, size, prot,
260 MAP_PRIVATE | MAP_ANON | MAP_ALIGNED(alignbit), -1, 0);
261 if (mem == MAP_FAILED) {
262 rv = errno;
263 } else {
264 *memp = mem;
265 rv = 0;
266 }
267
268 return rv;
269 }
270
271 void
272 rumpuser_unmap(void *addr, size_t len)
273 {
274 int rv;
275
276 rv = munmap(addr, len);
277 assert(rv == 0);
278 }
279
280 int
281 rumpuser_open(const char *path, int ruflags, int *fdp)
282 {
283 int fd, flags, rv;
284
285 switch (ruflags & RUMPUSER_OPEN_ACCMODE) {
286 case RUMPUSER_OPEN_RDONLY:
287 flags = O_RDONLY;
288 break;
289 case RUMPUSER_OPEN_WRONLY:
290 flags = O_WRONLY;
291 break;
292 case RUMPUSER_OPEN_RDWR:
293 flags = O_RDWR;
294 break;
295 default:
296 rv = EINVAL;
297 goto out;
298 }
299
300 #define TESTSET(_ru_, _h_) if (ruflags & _ru_) flags |= _h_;
301 TESTSET(RUMPUSER_OPEN_CREATE, O_CREAT);
302 TESTSET(RUMPUSER_OPEN_EXCL, O_EXCL);
303 #undef TESTSET
304
305 KLOCK_WRAP(fd = open(path, flags, 0644));
306 if (fd == -1) {
307 rv = errno;
308 } else {
309 *fdp = fd;
310 rv = 0;
311 }
312
313 out:
314 return rv;
315 }
316
317 int
318 rumpuser_close(int fd)
319 {
320 int nlocks;
321
322 rumpkern_unsched(&nlocks, NULL);
323 fsync(fd);
324 close(fd);
325 rumpkern_sched(nlocks, NULL);
326
327 return 0;
328 }
329
330 /*
331 * Assume "struct rumpuser_iovec" and "struct iovec" are the same.
332 * If you encounter POSIX platforms where they aren't, add some
333 * translation for iovlen > 1.
334 */
335 int
336 rumpuser_iovread(int fd, struct rumpuser_iovec *ruiov, size_t iovlen,
337 off_t off, size_t *retp)
338 {
339 struct iovec *iov = (struct iovec *)ruiov;
340 ssize_t nn;
341 int rv;
342
343 if (off == RUMPUSER_IOV_NOSEEK)
344 KLOCK_WRAP(nn = readv(fd, iov, iovlen));
345 else
346 KLOCK_WRAP(nn = preadv(fd, iov, iovlen, off));
347
348 if (nn == -1) {
349 rv = errno;
350 } else {
351 *retp = (size_t)nn;
352 rv = 0;
353 }
354
355 return rv;
356 }
357
358 int
359 rumpuser_iovwrite(int fd, const struct rumpuser_iovec *ruiov, size_t iovlen,
360 off_t off, size_t *retp)
361 {
362 const struct iovec *iov = (const struct iovec *)ruiov;
363 ssize_t nn;
364 int rv;
365
366 if (off == RUMPUSER_IOV_NOSEEK)
367 KLOCK_WRAP(nn = writev(fd, iov, iovlen));
368 else
369 KLOCK_WRAP(nn = pwritev(fd, iov, iovlen, off));
370
371 if (nn == -1) {
372 rv = errno;
373 } else {
374 *retp = (size_t)nn;
375 rv = 0;
376 }
377
378 return rv;
379 }
380
381 int
382 rumpuser_clock_gettime(uint64_t *sec, uint64_t *nsec, enum rumpclock rclk)
383 {
384 struct timespec ts;
385 clockid_t clk;
386 int rv;
387
388 switch (rclk) {
389 case RUMPUSER_CLOCK_RELWALL:
390 clk = CLOCK_REALTIME;
391 break;
392 case RUMPUSER_CLOCK_ABSMONO:
393 #ifdef HAVE_CLOCK_NANOSLEEP
394 clk = CLOCK_MONOTONIC;
395 #else
396 clk = CLOCK_REALTIME;
397 #endif
398 break;
399 default:
400 abort();
401 }
402
403 rv = clock_gettime(clk, &ts);
404 if (rv == -1) {
405 rv = errno;
406 } else {
407 *sec = ts.tv_sec;
408 *nsec = ts.tv_nsec;
409 rv = 0;
410 }
411
412 return rv;
413 }
414
415 int
416 rumpuser_clock_sleep(uint64_t sec, uint64_t nsec, enum rumpclock clk)
417 {
418 struct timespec rqt, rmt;
419 int nlocks;
420 int rv;
421
422 rumpkern_unsched(&nlocks, NULL);
423
424 /*LINTED*/
425 rqt.tv_sec = sec;
426 /*LINTED*/
427 rqt.tv_nsec = nsec;
428
429 switch (clk) {
430 case RUMPUSER_CLOCK_RELWALL:
431 do {
432 rv = nanosleep(&rqt, &rmt);
433 rqt = rmt;
434 } while (rv == -1 && errno == EINTR);
435 if (rv == -1) {
436 rv = errno;
437 }
438 break;
439 case RUMPUSER_CLOCK_ABSMONO:
440 do {
441 #ifdef HAVE_CLOCK_NANOSLEEP
442 rv = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
443 &rqt, NULL);
444 #else
445 /* le/la/der/die/das sigh. timevalspec tailspin */
446 struct timespec ts, tsr;
447 clock_gettime(CLOCK_REALTIME, &ts);
448 if (ts.tv_sec == rqt.tv_sec ?
449 ts.tv_nsec > rqt.tv_nsec : ts.tv_sec > rqt.tv_sec) {
450 rv = 0;
451 } else {
452 tsr.tv_sec = rqt.tv_sec - ts.tv_sec;
453 tsr.tv_nsec = rqt.tv_nsec - ts.tv_nsec;
454 if (tsr.tv_nsec < 0) {
455 tsr.tv_sec--;
456 tsr.tv_nsec += 1000*1000*1000;
457 }
458 rv = nanosleep(&tsr, NULL);
459 }
460 #endif
461 } while (rv == -1 && errno == EINTR);
462 if (rv == -1) {
463 rv = errno;
464 }
465 break;
466 default:
467 abort();
468 }
469
470 rumpkern_sched(nlocks, NULL);
471 return rv;
472 }
473
474 static int
475 gethostncpu(void)
476 {
477 int ncpu = 1;
478
479 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
480 size_t sz = sizeof(ncpu);
481
482 sysctlbyname("hw.ncpu", &ncpu, &sz, NULL, 0);
483 #elif defined(__linux__) || defined(__CYGWIN__)
484 FILE *fp;
485 char *line = NULL;
486 size_t n = 0;
487
488 /* If anyone knows a better way, I'm all ears */
489 if ((fp = fopen("/proc/cpuinfo", "r")) != NULL) {
490 ncpu = 0;
491 while (getline(&line, &n, fp) != -1) {
492 if (strncmp(line,
493 "processor", sizeof("processor")-1) == 0)
494 ncpu++;
495 }
496 if (ncpu == 0)
497 ncpu = 1;
498 free(line);
499 fclose(fp);
500 }
501 #elif __sun__
502 /* XXX: this is just a rough estimate ... */
503 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
504 #endif
505
506 return ncpu;
507 }
508
509 int
510 rumpuser_getparam(const char *name, void *buf, size_t blen)
511 {
512 int rv;
513
514 if (strcmp(name, RUMPUSER_PARAM_NCPU) == 0) {
515 int ncpu;
516
517 if (getenv_r("RUMP_NCPU", buf, blen) == -1) {
518 ncpu = gethostncpu();
519 snprintf(buf, blen, "%d", ncpu);
520 }
521 rv = 0;
522 } else if (strcmp(name, RUMPUSER_PARAM_HOSTNAME) == 0) {
523 char tmp[MAXHOSTNAMELEN];
524
525 if (gethostname(tmp, sizeof(tmp)) == -1) {
526 snprintf(buf, blen, "rump-%05d", (int)getpid());
527 } else {
528 snprintf(buf, blen, "rump-%05d.%s",
529 (int)getpid(), tmp);
530 }
531 rv = 0;
532 } else if (*name == '_') {
533 rv = EINVAL;
534 } else {
535 if (getenv_r(name, buf, blen) == -1)
536 rv = errno;
537 else
538 rv = 0;
539 }
540
541 return rv;
542 }
543
544 void
545 rumpuser_putchar(int c)
546 {
547
548 putchar(c);
549 }
550
551 void
552 rumpuser_exit(int rv)
553 {
554
555 if (rv == RUMPUSER_PANIC)
556 abort();
557 else
558 exit(rv);
559 }
560
561 void
562 rumpuser_seterrno(int error)
563 {
564
565 errno = error;
566 }
567
568 /*
569 * This is meant for safe debugging prints from the kernel.
570 */
571 void
572 rumpuser_dprintf(const char *format, ...)
573 {
574 va_list ap;
575
576 va_start(ap, format);
577 vfprintf(stderr, format, ap);
578 va_end(ap);
579 }
580
581 int
582 rumpuser_kill(int64_t pid, int sig)
583 {
584 int rv, error;
585
586 #ifdef __NetBSD__
587 if (pid == RUMPUSER_PID_SELF) {
588 error = raise(sig);
589 } else {
590 error = kill((pid_t)pid, sig);
591 }
592 if (error == -1)
593 rv = errno;
594 else
595 rv = 0;
596 #else
597 /* XXXfixme: signal numbers may not match on non-NetBSD */
598 rv = EOPNOTSUPP;
599 #endif
600
601 return rv;
602 }
603
604 int
605 rumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp)
606 {
607 size_t origlen = buflen;
608 uint32_t *p = buf;
609 uint32_t tmp;
610 int chunk;
611
612 do {
613 chunk = buflen < 4 ? buflen : 4; /* portable MIN ... */
614 tmp = RUMPUSER_RANDOM();
615 memcpy(p, &tmp, chunk);
616 p++;
617 buflen -= chunk;
618 } while (chunk);
619
620 *retp = origlen;
621 return 0;
622 }
623