rumpuser.c revision 1.20 1 /* $NetBSD: rumpuser.c,v 1.20 2012/09/14 16:29:22 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.20 2012/09/14 16:29:22 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 #include <sys/sysctl.h>
45 #include <sys/event.h>
46 #endif
47
48 #include <assert.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <poll.h>
53 #include <signal.h>
54 #include <stdarg.h>
55 #include <stdint.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <time.h>
60 #include <unistd.h>
61
62 #include <rump/rumpuser.h>
63
64 #include "rumpuser_int.h"
65
66 int
67 rumpuser_getversion(void)
68 {
69
70 return RUMPUSER_VERSION;
71 }
72
73 int
74 rumpuser_getfileinfo(const char *path, uint64_t *sizep, int *ftp, int *error)
75 {
76 struct stat sb;
77 uint64_t size;
78 int needsdev = 0, rv = 0, ft;
79 int fd = -1;
80
81 if (stat(path, &sb) == -1) {
82 seterror(errno);
83 return -1;
84 }
85
86 switch (sb.st_mode & S_IFMT) {
87 case S_IFDIR:
88 ft = RUMPUSER_FT_DIR;
89 break;
90 case S_IFREG:
91 ft = RUMPUSER_FT_REG;
92 break;
93 case S_IFBLK:
94 ft = RUMPUSER_FT_BLK;
95 needsdev = 1;
96 break;
97 case S_IFCHR:
98 ft = RUMPUSER_FT_CHR;
99 needsdev = 1;
100 break;
101 default:
102 ft = RUMPUSER_FT_OTHER;
103 break;
104 }
105
106 if (!needsdev) {
107 size = sb.st_size;
108 } else if (sizep) {
109 /*
110 * Welcome to the jungle. Of course querying the kernel
111 * for a device partition size is supposed to be far from
112 * trivial. On NetBSD we use ioctl. On $other platform
113 * we have a problem. We try "the lseek trick" and just
114 * fail if that fails. Platform specific code can later
115 * be written here if appropriate.
116 *
117 * On NetBSD we hope and pray that for block devices nobody
118 * else is holding them open, because otherwise the kernel
119 * will not permit us to open it. Thankfully, this is
120 * usually called only in bootstrap and then we can
121 * forget about it.
122 */
123 #ifndef __NetBSD__
124 off_t off;
125
126 fd = open(path, O_RDONLY);
127 if (fd == -1) {
128 seterror(errno);
129 rv = -1;
130 goto out;
131 }
132
133 off = lseek(fd, 0, SEEK_END);
134 if (off != 0) {
135 size = off;
136 goto out;
137 }
138 fprintf(stderr, "error: device size query not implemented on "
139 "this platform\n");
140 seterror(EOPNOTSUPP);
141 rv = -1;
142 goto out;
143 #else
144 struct disklabel lab;
145 struct partition *parta;
146 struct dkwedge_info dkw;
147
148 fd = open(path, O_RDONLY);
149 if (fd == -1) {
150 seterror(errno);
151 rv = -1;
152 goto out;
153 }
154
155 if (ioctl(fd, DIOCGDINFO, &lab) == 0) {
156 parta = &lab.d_partitions[DISKPART(sb.st_rdev)];
157 size = (uint64_t)lab.d_secsize * parta->p_size;
158 goto out;
159 }
160
161 if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
162 /*
163 * XXX: should use DIOCGDISKINFO to query
164 * sector size, but that requires proplib,
165 * so just don't bother for now. it's nice
166 * that something as difficult as figuring out
167 * a partition's size has been made so easy.
168 */
169 size = dkw.dkw_size << DEV_BSHIFT;
170 goto out;
171 }
172
173 seterror(errno);
174 rv = -1;
175 #endif /* __NetBSD__ */
176 }
177
178 out:
179 if (rv == 0 && sizep)
180 *sizep = size;
181 if (rv == 0 && ftp)
182 *ftp = ft;
183 if (fd != -1)
184 close(fd);
185
186 return rv;
187 }
188
189 int
190 rumpuser_nanosleep(uint64_t *sec, uint64_t *nsec, int *error)
191 {
192 struct timespec rqt, rmt;
193 int rv;
194
195 /*LINTED*/
196 rqt.tv_sec = *sec;
197 /*LINTED*/
198 rqt.tv_nsec = *nsec;
199
200 KLOCK_WRAP(rv = nanosleep(&rqt, &rmt));
201 if (rv == -1)
202 seterror(errno);
203
204 *sec = rmt.tv_sec;
205 *nsec = rmt.tv_nsec;
206
207 return rv;
208 }
209
210 void *
211 rumpuser_malloc(size_t howmuch, int alignment)
212 {
213 void *mem;
214 int rv;
215
216 if (alignment == 0)
217 alignment = sizeof(void *);
218
219 rv = posix_memalign(&mem, (size_t)alignment, howmuch);
220 if (__predict_false(rv != 0)) {
221 if (rv == EINVAL) {
222 printf("rumpuser_malloc: invalid alignment %d\n",
223 alignment);
224 abort();
225 }
226 mem = NULL;
227 }
228
229 return mem;
230 }
231
232 void *
233 rumpuser_realloc(void *ptr, size_t howmuch)
234 {
235
236 return realloc(ptr, howmuch);
237 }
238
239 void
240 rumpuser_free(void *ptr)
241 {
242
243 free(ptr);
244 }
245
246 void *
247 rumpuser_anonmmap(void *prefaddr, size_t size, int alignbit,
248 int exec, int *error)
249 {
250 void *rv;
251 int prot;
252
253 #ifndef MAP_ALIGNED
254 #define MAP_ALIGNED(a) 0
255 if (alignbit)
256 fprintf(stderr, "rumpuser_anonmmap: warning, requested "
257 "alignment not supported by hypervisor\n");
258 #endif
259
260 prot = PROT_READ|PROT_WRITE;
261 if (exec)
262 prot |= PROT_EXEC;
263 rv = mmap(prefaddr, size, prot,
264 MAP_ANON | MAP_ALIGNED(alignbit), -1, 0);
265 if (rv == MAP_FAILED) {
266 seterror(errno);
267 return NULL;
268 }
269 return rv;
270 }
271
272 void
273 rumpuser_unmap(void *addr, size_t len)
274 {
275 int rv;
276
277 rv = munmap(addr, len);
278 assert(rv == 0);
279 }
280
281 void *
282 rumpuser_filemmap(int fd, off_t offset, size_t len, int flags, int *error)
283 {
284 void *rv;
285 int mmflags, prot;
286
287 if (flags & RUMPUSER_FILEMMAP_TRUNCATE)
288 ftruncate(fd, offset + len);
289
290 mmflags = MAP_FILE;
291 if (flags & RUMPUSER_FILEMMAP_SHARED)
292 mmflags |= MAP_SHARED;
293 else
294 mmflags |= MAP_PRIVATE;
295
296 prot = 0;
297 if (flags & RUMPUSER_FILEMMAP_READ)
298 prot |= PROT_READ;
299 if (flags & RUMPUSER_FILEMMAP_WRITE)
300 prot |= PROT_WRITE;
301
302 rv = mmap(NULL, len, PROT_READ|PROT_WRITE, mmflags, fd, offset);
303 if (rv == MAP_FAILED) {
304 seterror(errno);
305 return NULL;
306 }
307
308 seterror(0);
309 return rv;
310 }
311
312 int
313 rumpuser_memsync(void *addr, size_t len, int *error)
314 {
315
316 DOCALL_KLOCK(int, (msync(addr, len, MS_SYNC)));
317 }
318
319 int
320 rumpuser_open(const char *path, int ruflags, int *error)
321 {
322 int flags;
323
324 switch (ruflags & RUMPUSER_OPEN_ACCMODE) {
325 case RUMPUSER_OPEN_RDONLY:
326 flags = O_RDONLY;
327 break;
328 case RUMPUSER_OPEN_WRONLY:
329 flags = O_WRONLY;
330 break;
331 case RUMPUSER_OPEN_RDWR:
332 flags = O_RDWR;
333 break;
334 default:
335 *error = EINVAL;
336 return -1;
337 }
338
339 #define TESTSET(_ru_, _h_) if (ruflags & _ru_) flags |= _h_;
340 TESTSET(RUMPUSER_OPEN_CREATE, O_CREAT);
341 TESTSET(RUMPUSER_OPEN_EXCL, O_EXCL);
342 TESTSET(RUMPUSER_OPEN_DIRECT, O_DIRECT);
343 #undef TESTSET
344
345 DOCALL_KLOCK(int, (open(path, flags, 0644)));
346 }
347
348 int
349 rumpuser_ioctl(int fd, u_long cmd, void *data, int *error)
350 {
351
352 DOCALL_KLOCK(int, (ioctl(fd, cmd, data)));
353 }
354
355 int
356 rumpuser_close(int fd, int *error)
357 {
358
359 DOCALL(int, close(fd));
360 }
361
362 int
363 rumpuser_fsync(int fd, int *error)
364 {
365
366 DOCALL_KLOCK(int, fsync(fd));
367 }
368
369 ssize_t
370 rumpuser_read(int fd, void *data, size_t size, int *error)
371 {
372 ssize_t rv;
373
374 KLOCK_WRAP(rv = read(fd, data, size));
375 if (rv == -1)
376 seterror(errno);
377
378 return rv;
379 }
380
381 ssize_t
382 rumpuser_pread(int fd, void *data, size_t size, off_t offset, int *error)
383 {
384 ssize_t rv;
385
386 KLOCK_WRAP(rv = pread(fd, data, size, offset));
387 if (rv == -1)
388 seterror(errno);
389
390 return rv;
391 }
392
393 void
394 rumpuser_read_bio(int fd, void *data, size_t size, off_t offset,
395 rump_biodone_fn biodone, void *biodonecookie)
396 {
397 ssize_t rv;
398 int error = 0;
399
400 rv = rumpuser_pread(fd, data, size, offset, &error);
401 /* check against <0 instead of ==-1 to get typing below right */
402 if (rv < 0)
403 rv = 0;
404
405 /* LINTED: see above */
406 biodone(biodonecookie, rv, error);
407 }
408
409 ssize_t
410 rumpuser_write(int fd, const void *data, size_t size, int *error)
411 {
412 ssize_t rv;
413
414 KLOCK_WRAP(rv = write(fd, data, size));
415 if (rv == -1)
416 seterror(errno);
417
418 return rv;
419 }
420
421 ssize_t
422 rumpuser_pwrite(int fd, const void *data, size_t size, off_t offset, int *error)
423 {
424 ssize_t rv;
425
426 KLOCK_WRAP(rv = pwrite(fd, data, size, offset));
427 if (rv == -1)
428 seterror(errno);
429
430 return rv;
431 }
432
433 void
434 rumpuser_write_bio(int fd, const void *data, size_t size, off_t offset,
435 rump_biodone_fn biodone, void *biodonecookie)
436 {
437 ssize_t rv;
438 int error = 0;
439
440 rv = rumpuser_pwrite(fd, data, size, offset, &error);
441 /* check against <0 instead of ==-1 to get typing below right */
442 if (rv < 0)
443 rv = 0;
444
445 /* LINTED: see above */
446 biodone(biodonecookie, rv, error);
447 }
448
449 ssize_t
450 rumpuser_readv(int fd, const struct rumpuser_iovec *riov, int iovcnt,
451 int *error)
452 {
453 struct iovec *iovp;
454 ssize_t rv;
455 int i;
456
457 iovp = malloc(iovcnt * sizeof(struct iovec));
458 if (iovp == NULL) {
459 seterror(ENOMEM);
460 return -1;
461 }
462 for (i = 0; i < iovcnt; i++) {
463 iovp[i].iov_base = riov[i].iov_base;
464 /*LINTED*/
465 iovp[i].iov_len = riov[i].iov_len;
466 }
467
468 KLOCK_WRAP(rv = readv(fd, iovp, iovcnt));
469 if (rv == -1)
470 seterror(errno);
471 free(iovp);
472
473 return rv;
474 }
475
476 ssize_t
477 rumpuser_writev(int fd, const struct rumpuser_iovec *riov, int iovcnt,
478 int *error)
479 {
480 struct iovec *iovp;
481 ssize_t rv;
482 int i;
483
484 iovp = malloc(iovcnt * sizeof(struct iovec));
485 if (iovp == NULL) {
486 seterror(ENOMEM);
487 return -1;
488 }
489 for (i = 0; i < iovcnt; i++) {
490 iovp[i].iov_base = riov[i].iov_base;
491 /*LINTED*/
492 iovp[i].iov_len = riov[i].iov_len;
493 }
494
495 KLOCK_WRAP(rv = writev(fd, iovp, iovcnt));
496 if (rv == -1)
497 seterror(errno);
498 free(iovp);
499
500 return rv;
501 }
502
503 int
504 rumpuser_gettime(uint64_t *sec, uint64_t *nsec, int *error)
505 {
506 struct timeval tv;
507 int rv;
508
509 rv = gettimeofday(&tv, NULL);
510 if (rv == -1) {
511 seterror(errno);
512 return rv;
513 }
514
515 *sec = tv.tv_sec;
516 *nsec = tv.tv_usec * 1000;
517
518 return 0;
519 }
520
521 int
522 rumpuser_getenv(const char *name, char *buf, size_t blen, int *error)
523 {
524
525 DOCALL(int, getenv_r(name, buf, blen));
526 }
527
528 int
529 rumpuser_gethostname(char *name, size_t namelen, int *error)
530 {
531 char tmp[MAXHOSTNAMELEN];
532
533 if (gethostname(tmp, sizeof(tmp)) == -1) {
534 snprintf(name, namelen, "rump-%05d.rumpdomain", getpid());
535 } else {
536 snprintf(name, namelen, "rump-%05d.%s.rumpdomain",
537 getpid(), tmp);
538 }
539
540 *error = 0;
541 return 0;
542 }
543
544 int
545 rumpuser_poll(struct pollfd *fds, int nfds, int timeout, int *error)
546 {
547
548 DOCALL_KLOCK(int, (poll(fds, (nfds_t)nfds, timeout)));
549 }
550
551 int
552 rumpuser_putchar(int c, int *error)
553 {
554
555 DOCALL(int, (putchar(c)));
556 }
557
558 void
559 rumpuser_exit(int rv)
560 {
561
562 if (rv == RUMPUSER_PANIC)
563 abort();
564 else
565 exit(rv);
566 }
567
568 void
569 rumpuser_seterrno(int error)
570 {
571
572 errno = error;
573 }
574
575 #ifdef __NetBSD__
576 int
577 rumpuser_writewatchfile_setup(int kq, int fd, intptr_t opaque, int *error)
578 {
579 struct kevent kev;
580
581 if (kq == -1) {
582 kq = kqueue();
583 if (kq == -1) {
584 seterror(errno);
585 return -1;
586 }
587 }
588
589 EV_SET(&kev, fd, EVFILT_VNODE, EV_ADD|EV_ENABLE|EV_CLEAR,
590 NOTE_WRITE, 0, opaque);
591 if (kevent(kq, &kev, 1, NULL, 0, NULL) == -1) {
592 seterror(errno);
593 return -1;
594 }
595
596 return kq;
597 }
598
599 int
600 rumpuser_writewatchfile_wait(int kq, intptr_t *opaque, int *error)
601 {
602 struct kevent kev;
603 int rv;
604
605 again:
606 KLOCK_WRAP(rv = kevent(kq, NULL, 0, &kev, 1, NULL));
607 if (rv == -1) {
608 if (errno == EINTR)
609 goto again;
610 seterror(errno);
611 return -1;
612 }
613
614 if (opaque)
615 *opaque = kev.udata;
616 return rv;
617 }
618 #endif
619
620 /*
621 * This is meant for safe debugging prints from the kernel.
622 */
623 int
624 rumpuser_dprintf(const char *format, ...)
625 {
626 va_list ap;
627 int rv;
628
629 va_start(ap, format);
630 rv = vfprintf(stderr, format, ap);
631 va_end(ap);
632
633 return rv;
634 }
635
636 int
637 rumpuser_kill(int64_t pid, int sig, int *error)
638 {
639
640 #ifdef __NetBSD__
641 if (pid == RUMPUSER_PID_SELF) {
642 DOCALL(int, raise(sig));
643 } else {
644 DOCALL(int, kill((pid_t)pid, sig));
645 }
646 #else
647 /* XXXfixme: signal numbers may not match on non-NetBSD */
648 seterror(EOPNOTSUPP);
649 return -1;
650 #endif
651 }
652
653 int
654 rumpuser_getnhostcpu(void)
655 {
656 int ncpu = 1;
657
658 #ifdef __NetBSD__
659 size_t sz = sizeof(ncpu);
660
661 sysctlbyname("hw.ncpu", &ncpu, &sz, NULL, 0);
662 #elif __linux__
663 FILE *fp;
664 char *line = NULL;
665 size_t n = 0;
666
667 /* If anyone knows a better way, I'm all ears */
668 if ((fp = fopen("/proc/cpuinfo", "r")) != NULL) {
669 ncpu = 0;
670 while (getline(&line, &n, fp) != -1) {
671 if (strncmp(line,
672 "processor", sizeof("processor")-1) == 0)
673 ncpu++;
674 }
675 if (ncpu == 0)
676 ncpu = 1;
677 free(line);
678 fclose(fp);
679 }
680 #endif
681
682 return ncpu;
683 }
684
685 /* XXX: this hypercall needs a better name */
686 uint32_t
687 rumpuser_arc4random(void)
688 {
689
690 return arc4random();
691 }
692