Sync usage with man page.
[netbsd-mini2440.git] / sys / rump / librump / rumpuser / rumpuser.c
blob363e033fde11a5ae48e581807a20d65a2f16094a
1 /* $NetBSD: rumpuser.c,v 1.46 2009/11/19 13:42:29 pooka Exp $ */
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
6 * Development of this software was supported by Google Summer of Code
7 * and the Finnish Cultural Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 #if !defined(lint)
33 __RCSID("$NetBSD: rumpuser.c,v 1.46 2009/11/19 13:42:29 pooka Exp $");
34 #endif /* !lint */
36 /* thank the maker for this */
37 #ifdef __linux__
38 #define _XOPEN_SOURCE 500
39 #define _BSD_SOURCE
40 #define _FILE_OFFSET_BITS 64
41 #include <features.h>
42 #endif
44 #include <sys/param.h>
45 #include <sys/event.h>
46 #include <sys/ioctl.h>
47 #include <sys/mman.h>
48 #include <sys/uio.h>
50 #ifdef __NetBSD__
51 #include <sys/disklabel.h>
52 #endif
54 #include <assert.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <poll.h>
59 #include <stdarg.h>
60 #include <stdint.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <time.h>
65 #include <unistd.h>
67 #include <rump/rumpuser.h>
69 #include "rumpuser_int.h"
71 int
72 rumpuser_getfileinfo(const char *path, uint64_t *size, int *ft, int *error)
74 struct stat sb;
75 int needsdev = 0;
77 if (stat(path, &sb) == -1) {
78 *error = errno;
79 return -1;
82 switch (sb.st_mode & S_IFMT) {
83 case S_IFDIR:
84 *ft = RUMPUSER_FT_DIR;
85 break;
86 case S_IFREG:
87 *ft = RUMPUSER_FT_REG;
88 break;
89 case S_IFBLK:
90 *ft = RUMPUSER_FT_BLK;
91 needsdev = 1;
92 break;
93 case S_IFCHR:
94 *ft = RUMPUSER_FT_CHR;
95 needsdev = 1;
96 break;
97 default:
98 *ft = RUMPUSER_FT_OTHER;
99 break;
102 if (!needsdev) {
103 *size = sb.st_size;
104 } else {
106 * Welcome to the jungle. Of course querying the kernel
107 * for a device partition size is supposed to be far from
108 * trivial. On NetBSD we use ioctl. On $other platform
109 * we have a problem. We try "the lseek trick" and just
110 * fail if that fails. Platform specific code can later
111 * be written here if appropriate.
113 * On NetBSD we hope and pray that for block devices nobody
114 * else is holding them open, because otherwise the kernel
115 * will not permit us to open it. Thankfully, this is
116 * usually called only in bootstrap and then we can
117 * forget about it.
119 #ifndef __NetBSD__
120 off_t off;
121 int fd;
123 fd = open(path, O_RDONLY);
124 if (fd == -1) {
125 *error = errno;
126 return -1;
129 off = lseek(fd, 0, SEEK_END);
130 close(fd);
131 if (off != 0) {
132 *size = off;
133 return 0;
135 fprintf(stderr, "error: device size query not implemented on "
136 "this platform\n");
137 *error = EOPNOTSUPP;
138 return -1;
139 #else
140 struct disklabel lab;
141 struct partition *parta;
142 int fd;
144 fd = open(path, O_RDONLY);
145 if (fd == -1) {
146 *error = errno;
147 return -1;
150 if (ioctl(fd, DIOCGDINFO, &lab) == -1) {
151 *error = errno;
152 return -1;
154 close(fd);
156 parta = &lab.d_partitions[DISKPART(sb.st_rdev)];
157 *size = (uint64_t)lab.d_secsize * parta->p_size;
158 #endif /* __NetBSD__ */
161 return 0;
165 rumpuser_nanosleep(uint64_t *sec, uint64_t *nsec, int *error)
167 struct timespec rqt, rmt;
168 int rv;
170 /*LINTED*/
171 rqt.tv_sec = *sec;
172 /*LINTED*/
173 rqt.tv_nsec = *nsec;
175 KLOCK_WRAP(rv = nanosleep(&rqt, &rmt));
176 if (rv == -1)
177 *error = errno;
179 *sec = rmt.tv_sec;
180 *nsec = rmt.tv_nsec;
182 return rv;
185 void *
186 rumpuser__malloc(size_t howmuch, int canfail, const char *func, int line)
188 void *rv;
190 rv = malloc(howmuch);
191 if (rv == NULL && canfail == 0) {
192 warn("malloc failed %s (%d)", func, line);
193 abort();
196 return rv;
199 void *
200 rumpuser__realloc(void *ptr, size_t howmuch, int canfail,
201 const char *func, int line)
203 void *rv;
205 rv = realloc(ptr, howmuch);
206 if (rv == NULL && canfail == 0) {
207 warn("realloc failed %s (%d)", func, line);
208 abort();
211 return rv;
214 void
215 rumpuser_free(void *ptr)
218 free(ptr);
221 void *
222 rumpuser_anonmmap(size_t size, int alignbit, int exec, int *error)
224 void *rv;
225 int prot;
227 prot = PROT_READ|PROT_WRITE;
228 if (exec)
229 prot |= PROT_EXEC;
230 /* XXX: MAP_ALIGNED() is not portable */
231 rv = mmap(NULL, size, prot, MAP_ANON | MAP_ALIGNED(alignbit), -1, 0);
232 if (rv == MAP_FAILED) {
233 *error = errno;
234 return NULL;
236 return rv;
239 void
240 rumpuser_unmap(void *addr, size_t len)
242 int rv;
244 rv = munmap(addr, len);
245 assert(rv == 0);
248 void *
249 rumpuser_filemmap(int fd, off_t offset, size_t len, int flags, int *error)
251 void *rv;
252 int mmflags, prot;
254 if (flags & RUMPUSER_FILEMMAP_TRUNCATE)
255 ftruncate(fd, offset + len);
257 mmflags = MAP_FILE;
258 if (flags & RUMPUSER_FILEMMAP_SHARED)
259 mmflags |= MAP_SHARED;
260 else
261 mmflags |= MAP_PRIVATE;
263 prot = 0;
264 if (flags & RUMPUSER_FILEMMAP_READ)
265 prot |= PROT_READ;
266 if (flags & RUMPUSER_FILEMMAP_WRITE)
267 prot |= PROT_WRITE;
269 rv = mmap(NULL, len, PROT_READ|PROT_WRITE, mmflags, fd, offset);
270 if (rv == MAP_FAILED) {
271 *error = errno;
272 return NULL;
275 *error = 0;
276 return rv;
280 rumpuser_memsync(void *addr, size_t len, int *error)
283 DOCALL_KLOCK(int, (msync(addr, len, MS_SYNC)));
287 rumpuser_open(const char *path, int flags, int *error)
290 DOCALL(int, (open(path, flags, 0644)));
294 rumpuser_ioctl(int fd, u_long cmd, void *data, int *error)
297 DOCALL_KLOCK(int, (ioctl(fd, cmd, data)));
301 rumpuser_close(int fd, int *error)
304 DOCALL(int, close(fd));
308 rumpuser_fsync(int fd, int *error)
311 DOCALL_KLOCK(int, fsync(fd));
314 ssize_t
315 rumpuser_read(int fd, void *data, size_t size, int *error)
317 ssize_t rv;
319 KLOCK_WRAP(rv = read(fd, data, size));
320 if (rv == -1)
321 *error = errno;
323 return rv;
326 ssize_t
327 rumpuser_pread(int fd, void *data, size_t size, off_t offset, int *error)
329 ssize_t rv;
331 KLOCK_WRAP(rv = pread(fd, data, size, offset));
332 if (rv == -1)
333 *error = errno;
335 return rv;
338 void
339 rumpuser_read_bio(int fd, void *data, size_t size, off_t offset,
340 rump_biodone_fn biodone, void *biodonecookie)
342 ssize_t rv;
343 int error = 0;
345 rv = rumpuser_pread(fd, data, size, offset, &error);
346 /* check against <0 instead of ==-1 to get typing below right */
347 if (rv < 0)
348 rv = 0;
350 /* LINTED: see above */
351 biodone(biodonecookie, rv, error);
354 ssize_t
355 rumpuser_write(int fd, const void *data, size_t size, int *error)
357 ssize_t rv;
359 KLOCK_WRAP(rv = write(fd, data, size));
360 if (rv == -1)
361 *error = errno;
363 return rv;
366 ssize_t
367 rumpuser_pwrite(int fd, const void *data, size_t size, off_t offset, int *error)
369 ssize_t rv;
371 KLOCK_WRAP(rv = pwrite(fd, data, size, offset));
372 if (rv == -1)
373 *error = errno;
375 return rv;
378 void
379 rumpuser_write_bio(int fd, const void *data, size_t size, off_t offset,
380 rump_biodone_fn biodone, void *biodonecookie)
382 ssize_t rv;
383 int error = 0;
385 rv = rumpuser_pwrite(fd, data, size, offset, &error);
386 /* check against <0 instead of ==-1 to get typing below right */
387 if (rv < 0)
388 rv = 0;
390 /* LINTED: see above */
391 biodone(biodonecookie, rv, error);
394 ssize_t
395 rumpuser_readv(int fd, const struct rumpuser_iovec *riov, int iovcnt,
396 int *error)
398 struct iovec *iovp;
399 ssize_t rv;
400 int i;
402 iovp = malloc(iovcnt * sizeof(struct iovec));
403 if (iovp == NULL) {
404 *error = ENOMEM;
405 return -1;
407 for (i = 0; i < iovcnt; i++) {
408 iovp[i].iov_base = riov[i].iov_base;
409 /*LINTED*/
410 iovp[i].iov_len = riov[i].iov_len;
413 KLOCK_WRAP(rv = readv(fd, iovp, iovcnt));
414 if (rv == -1)
415 *error = errno;
416 free(iovp);
418 return rv;
421 ssize_t
422 rumpuser_writev(int fd, const struct rumpuser_iovec *riov, int iovcnt,
423 int *error)
425 struct iovec *iovp;
426 ssize_t rv;
427 int i;
429 iovp = malloc(iovcnt * sizeof(struct iovec));
430 if (iovp == NULL) {
431 *error = ENOMEM;
432 return -1;
434 for (i = 0; i < iovcnt; i++) {
435 iovp[i].iov_base = riov[i].iov_base;
436 /*LINTED*/
437 iovp[i].iov_len = riov[i].iov_len;
440 KLOCK_WRAP(rv = writev(fd, iovp, iovcnt));
441 if (rv == -1)
442 *error = errno;
443 free(iovp);
445 return rv;
449 rumpuser_gettime(uint64_t *sec, uint64_t *nsec, int *error)
451 struct timeval tv;
452 int rv;
454 rv = gettimeofday(&tv, NULL);
455 if (rv == -1) {
456 *error = errno;
457 return rv;
460 *sec = tv.tv_sec;
461 *nsec = tv.tv_usec * 1000;
463 return 0;
467 rumpuser_getenv(const char *name, char *buf, size_t blen, int *error)
470 DOCALL(int, getenv_r(name, buf, blen));
474 rumpuser_gethostname(char *name, size_t namelen, int *error)
477 DOCALL(int, (gethostname(name, namelen)));
481 rumpuser_poll(struct pollfd *fds, int nfds, int timeout, int *error)
484 DOCALL_KLOCK(int, (poll(fds, (nfds_t)nfds, timeout)));
488 rumpuser_putchar(int c, int *error)
491 DOCALL(int, (putchar(c)));
494 void
495 rumpuser_exit(int rv)
498 if (rv == RUMPUSER_PANIC)
499 abort();
500 else
501 exit(rv);
504 void
505 rumpuser_seterrno(int error)
508 errno = error;
512 rumpuser_writewatchfile_setup(int kq, int fd, intptr_t opaque, int *error)
514 struct kevent kev;
516 if (kq == -1) {
517 kq = kqueue();
518 if (kq == -1) {
519 *error = errno;
520 return -1;
524 EV_SET(&kev, fd, EVFILT_VNODE, EV_ADD|EV_ENABLE|EV_CLEAR,
525 NOTE_WRITE, 0, opaque);
526 if (kevent(kq, &kev, 1, NULL, 0, NULL) == -1) {
527 *error = errno;
528 return -1;
531 return kq;
535 rumpuser_writewatchfile_wait(int kq, intptr_t *opaque, int *error)
537 struct kevent kev;
538 int rv;
540 KLOCK_WRAP(rv = kevent(kq, NULL, 0, &kev, 1, NULL));
541 if (rv == -1) {
542 *error = errno;
543 return -1;
546 if (opaque)
547 *opaque = kev.udata;
548 return rv;
552 * This is meant for safe debugging prints from the kernel.
555 rumpuser_dprintf(const char *format, ...)
557 va_list ap;
558 int rv;
560 va_start(ap, format);
561 rv = vprintf(format, ap);
562 va_end(ap);
564 return rv;