diff options
author | Reyk Floeter <reyk@cvs.openbsd.org> | 2015-11-23 13:04:50 +0000 |
---|---|---|
committer | Reyk Floeter <reyk@cvs.openbsd.org> | 2015-11-23 13:04:50 +0000 |
commit | 4e11e311495bdc25503c0a0c31d6b07af9bb1a4b (patch) | |
tree | 8d0899a73e36a82d1303913d2d10e016b1643577 /usr.sbin | |
parent | 57148cf533ab346dc20bec5a7b3593e6a7c2f3f7 (diff) |
Add support for logging to stderr or syslog, and to run vmd in
foreground with -d.
OK mlarkin@ jung@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/vmd/Makefile | 2 | ||||
-rw-r--r-- | usr.sbin/vmd/loadfile_elf.c | 11 | ||||
-rw-r--r-- | usr.sbin/vmd/log.c | 206 | ||||
-rw-r--r-- | usr.sbin/vmd/pci.c | 21 | ||||
-rw-r--r-- | usr.sbin/vmd/virtio.c | 171 | ||||
-rw-r--r-- | usr.sbin/vmd/vmd.8 | 18 | ||||
-rw-r--r-- | usr.sbin/vmd/vmd.c | 180 | ||||
-rw-r--r-- | usr.sbin/vmd/vmd.h | 27 |
8 files changed, 442 insertions, 194 deletions
diff --git a/usr.sbin/vmd/Makefile b/usr.sbin/vmd/Makefile index c02a006cf10..6f720bff811 100644 --- a/usr.sbin/vmd/Makefile +++ b/usr.sbin/vmd/Makefile @@ -2,7 +2,7 @@ .if ${MACHINE} == "amd64" PROG= vmd -SRCS= vmd.c loadfile_elf.c pci.c virtio.c +SRCS= vmd.c loadfile_elf.c pci.c virtio.c log.c CFLAGS+= -Wall -I${.CURDIR} CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes CFLAGS+= -Wmissing-declarations diff --git a/usr.sbin/vmd/loadfile_elf.c b/usr.sbin/vmd/loadfile_elf.c index df566c4f2db..29e88de556c 100644 --- a/usr.sbin/vmd/loadfile_elf.c +++ b/usr.sbin/vmd/loadfile_elf.c @@ -1,5 +1,5 @@ /* $NetBSD: loadfile.c,v 1.10 2000/12/03 02:53:04 tsutsui Exp $ */ -/* $OpenBSD: loadfile_elf.c,v 1.1 2015/11/22 20:20:32 mlarkin Exp $ */ +/* $OpenBSD: loadfile_elf.c,v 1.2 2015/11/23 13:04:49 reyk Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -382,8 +382,7 @@ mread(int fd, uint32_t addr, size_t sz) ct = sz; if (read(fd, buf, ct) != ct) { - fprintf(stderr, "%s: error %d in mread (%s)\n\r", - __progname, errno, strerror(errno)); + log_warn("%s: error %d in mread", __progname, errno); return (0); } rd += ct; @@ -407,8 +406,7 @@ mread(int fd, uint32_t addr, size_t sz) ct = PAGE_SIZE; if (read(fd, buf, ct) != ct) { - fprintf(stderr, "%s: error %d in mread (%s)\n\r", - __progname, errno, strerror(errno)); + log_warn("%s: error %d in mread", __progname, errno); return (0); } rd += ct; @@ -534,8 +532,7 @@ mbzero(uint32_t addr, int sz) static void mbcopy(char *src, char *dst, int sz) { - fprintf(stderr, "warning: bcopy during ELF kernel load not " - "supported\n\r"); + log_warnx("warning: bcopy during ELF kernel load not supported"); } /* diff --git a/usr.sbin/vmd/log.c b/usr.sbin/vmd/log.c new file mode 100644 index 00000000000..6991b963bd3 --- /dev/null +++ b/usr.sbin/vmd/log.c @@ -0,0 +1,206 @@ +/* $OpenBSD: log.c,v 1.1 2015/11/23 13:04:49 reyk Exp $ */ + +/* + * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <syslog.h> +#include <errno.h> +#include <time.h> + +int debug; +int verbose; +const char *log_procname; + +void log_init(int, int); +void log_procinit(const char *); +void log_verbose(int); +void log_warn(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_warnx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_info(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_debug(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void logit(int, const char *, ...) + __attribute__((__format__ (printf, 2, 3))); +void vlog(int, const char *, va_list) + __attribute__((__format__ (printf, 2, 0))); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); + +void +log_init(int n_debug, int facility) +{ + extern char *__progname; + + debug = n_debug; + verbose = n_debug; + log_procinit(__progname); + + if (!debug) + openlog(__progname, LOG_PID | LOG_NDELAY, facility); + + tzset(); +} + +void +log_procinit(const char *procname) +{ + if (procname != NULL) + log_procname = procname; +} + +void +log_verbose(int v) +{ + verbose = v; +} + +void +logit(int pri, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlog(pri, fmt, ap); + va_end(ap); +} + +void +vlog(int pri, const char *fmt, va_list ap) +{ + char *nfmt; + + if (debug) { + /* best effort in out of mem situations */ + if (asprintf(&nfmt, "%s\n", fmt) == -1) { + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + } else { + vfprintf(stderr, nfmt, ap); + free(nfmt); + } + fflush(stderr); + } else + vsyslog(pri, fmt, ap); +} + + +void +log_warn(const char *emsg, ...) +{ + char *nfmt; + va_list ap; + + /* best effort to even work in out of memory situations */ + if (emsg == NULL) + logit(LOG_CRIT, "%s", strerror(errno)); + else { + va_start(ap, emsg); + + if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { + /* we tried it... */ + vlog(LOG_CRIT, emsg, ap); + logit(LOG_CRIT, "%s", strerror(errno)); + } else { + vlog(LOG_CRIT, nfmt, ap); + free(nfmt); + } + va_end(ap); + } +} + +void +log_warnx(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vlog(LOG_CRIT, emsg, ap); + va_end(ap); +} + +void +log_info(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vlog(LOG_INFO, emsg, ap); + va_end(ap); +} + +void +log_debug(const char *emsg, ...) +{ + va_list ap; + + if (verbose > 1) { + va_start(ap, emsg); + vlog(LOG_DEBUG, emsg, ap); + va_end(ap); + } +} + +static void +vfatal(const char *emsg, va_list ap) +{ + static char s[BUFSIZ]; + const char *sep; + + if (emsg != NULL) { + (void)vsnprintf(s, sizeof(s), emsg, ap); + sep = ": "; + } else { + s[0] = '\0'; + sep = ""; + } + if (errno) + logit(LOG_CRIT, "fatal in %s: %s%s%s", + log_procname, s, sep, strerror(errno)); + else + logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s); +} + +void +fatal(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfatal(emsg, ap); + va_end(ap); + exit(1); +} + +void +fatalx(const char *emsg, ...) +{ + va_list ap; + + errno = 0; + va_start(ap, emsg); + vfatal(emsg, ap); + va_end(ap); + exit(1); +} diff --git a/usr.sbin/vmd/pci.c b/usr.sbin/vmd/pci.c index e0fd19384a5..c8ff945a8b4 100644 --- a/usr.sbin/vmd/pci.c +++ b/usr.sbin/vmd/pci.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pci.c,v 1.2 2015/11/22 21:51:32 reyk Exp $ */ +/* $OpenBSD: pci.c,v 1.3 2015/11/23 13:04:49 reyk Exp $ */ /* * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org> @@ -89,7 +89,7 @@ pci_add_bar(uint8_t id, uint32_t type, void *barfn, void *cookie) pci.pci_next_io_bar += VMM_PCI_IO_BAR_SIZE; pci.pci_devices[id].pd_barfunc[bar_ct] = barfn; pci.pci_devices[id].pd_bar_cookie[bar_ct] = cookie; - dprintf("%s: adding pci bar cookie for dev %d bar %d = %p\n\r", + dprintf("%s: adding pci bar cookie for dev %d bar %d = %p", __progname, id, bar_ct, cookie); pci.pci_devices[id].pd_bartype[bar_ct] = PCI_BAR_TYPE_IO; pci.pci_devices[id].pd_barsize[bar_ct] = VMM_PCI_IO_BAR_SIZE; @@ -150,7 +150,7 @@ pci_add_device(uint8_t *id, uint16_t vid, uint16_t pid, uint8_t class, pci_pic_irqs[pci.pci_next_pic_irq]; pci.pci_devices[*id].pd_int = 1; pci.pci_next_pic_irq++; - dprintf("assigned irq %d to pci dev %d\n\r", + dprintf("assigned irq %d to pci dev %d", pci.pci_devices[*id].pd_irq, *id); } @@ -177,8 +177,7 @@ pci_init(void) if (pci_add_device(&id, PCI_VENDOR_OPENBSD, PCI_PRODUCT_OPENBSD_PCHB, PCI_CLASS_BRIDGE, PCI_SUBCLASS_BRIDGE_HOST, PCI_VENDOR_OPENBSD, 0, 0, NULL)) { - fprintf(stderr, "%s: can't add PCI host bridge\n", - __progname); + log_warnx("%s: can't add PCI host bridge", __progname); return; } } @@ -238,11 +237,11 @@ pci_handle_io(struct vm_run_params *vrp) PCI_MAPREG_IO_ADDR(pci.pci_devices[l].pd_bar[k]), &vei->vei.vei_data, &intr, pci.pci_devices[l].pd_bar_cookie[k])) { - fprintf(stderr, "%s: pci i/o access function failed\n", + log_warnx("%s: pci i/o access function failed", __progname); } } else { - fprintf(stderr, "%s: no pci i/o function for reg 0x%llx\n", + log_warnx("%s: no pci i/o function for reg 0x%llx", __progname, (uint64_t)reg); } @@ -266,8 +265,8 @@ pci_handle_data_reg(struct vm_run_params *vrp) /* if read, return FFs */ if (vei->vei.vei_dir == 1) vei->vei.vei_data = 0xffffffff; - fprintf(stderr, "invalid address register during pci read: " - "0x%llx\r\n", (uint64_t)pci.pci_addr_reg); + log_warnx("invalid address register during pci read: " + "0x%llx", (uint64_t)pci.pci_addr_reg); return; } @@ -280,8 +279,8 @@ pci_handle_data_reg(struct vm_run_params *vrp) if (csfunc != NULL) { ret = csfunc(vei->vei.vei_dir, (o / 4), &vei->vei.vei_data); if (ret) - fprintf(stderr, "cfg space access function failed for " - "pci device %d\n", d); + log_warnx("cfg space access function failed for " + "pci device %d", d); return; } diff --git a/usr.sbin/vmd/virtio.c b/usr.sbin/vmd/virtio.c index 5a5381c0afa..1dfdbf52670 100644 --- a/usr.sbin/vmd/virtio.c +++ b/usr.sbin/vmd/virtio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: virtio.c,v 1.2 2015/11/22 21:51:32 reyk Exp $ */ +/* $OpenBSD: virtio.c,v 1.3 2015/11/23 13:04:49 reyk Exp $ */ /* * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org> @@ -65,10 +65,10 @@ vioblk_cmd_name(uint32_t type) static void dump_descriptor_chain(struct vring_desc *desc, int16_t dxx) { - fprintf(stderr, "descriptor chain @ %d\n\r", dxx); + log_debug("descriptor chain @ %d", dxx); do { - fprintf(stderr, "desc @%d addr/len/flags/next = 0x%llx / 0x%x " - "/ 0x%x / 0x%x\n\r", + log_debug("desc @%d addr/len/flags/next = 0x%llx / 0x%x " + "/ 0x%x / 0x%x", dxx, desc[dxx].addr, desc[dxx].len, @@ -77,8 +77,8 @@ dump_descriptor_chain(struct vring_desc *desc, int16_t dxx) dxx = desc[dxx].next; } while (desc[dxx].flags & VRING_DESC_F_NEXT); - fprintf(stderr, "desc @%d addr/len/flags/next = 0x%llx / 0x%x / 0x%x " - "/ 0x%x\n\r", + log_debug("desc @%d addr/len/flags/next = 0x%llx / 0x%x / 0x%x " + "/ 0x%x", dxx, desc[dxx].addr, desc[dxx].len, @@ -181,7 +181,7 @@ viornd_notifyq(void) buf = malloc(vr_sz); if (buf == NULL) { - fprintf(stderr, "malloc error getting viornd ring\n\r"); + log_warn("malloc error getting viornd ring"); return (0); } @@ -207,8 +207,8 @@ viornd_notifyq(void) arc4random_buf(rnd_data, desc[avail->ring[avail->idx]].len); if (write_page((uint32_t)(desc[avail->ring[avail->idx]].addr), rnd_data, desc[avail->ring[avail->idx]].len, 0)) { - fprintf(stderr, "viornd: can't write random data @ " - "0x%llx\n\r", + log_warnx("viornd: can't write random data @ " + "0x%llx", desc[avail->ring[avail->idx]].addr); } else { /* ret == 1 -> interrupt needed */ @@ -221,8 +221,7 @@ viornd_notifyq(void) used->idx++; if (push_virtio_ring(buf, q_gpa, vr_sz)) { - fprintf(stderr, "viornd: error writing vio " - "ring\n\r"); + log_warnx("viornd: error writing vio ring"); } } @@ -245,7 +244,7 @@ virtio_rnd_io(int dir, uint16_t reg, uint32_t *data, uint8_t *intr, case VIRTIO_CONFIG_DEVICE_FEATURES: case VIRTIO_CONFIG_QUEUE_SIZE: case VIRTIO_CONFIG_ISR_STATUS: - fprintf(stderr, "%s: illegal write %x to %s\n\r", + log_warnx("%s: illegal write %x to %s", __progname, *data, virtio_reg_name(reg)); break; case VIRTIO_CONFIG_GUEST_FEATURES: @@ -327,23 +326,20 @@ vioblk_do_read(struct vioblk_dev *dev, off_t sector, ssize_t sz) char *buf; buf = malloc(sz); - if (buf == NULL) { - fprintf(stderr, "malloc errror vioblk read\n\r"); + log_warn("malloc errror vioblk read"); return (NULL); } if (lseek(dev->fd, sector * VIRTIO_BLK_SECTOR_SIZE, SEEK_SET) == -1) { - fprintf(stderr, "seek error in vioblk read: %d\n\r", - errno); + log_warn("seek error in vioblk read"); free(buf); return (NULL); } if (read(dev->fd, buf, sz) != sz) { - fprintf(stderr, "vioblk read error: %d\n\r", - errno); + log_warn("vioblk read error"); free(buf); return (NULL); } @@ -356,14 +352,12 @@ vioblk_do_write(struct vioblk_dev *dev, off_t sector, char *buf, ssize_t sz) { if (lseek(dev->fd, sector * VIRTIO_BLK_SECTOR_SIZE, SEEK_SET) == -1) { - fprintf(stderr, "seek error in vioblk write: %d\n\r", - errno); + log_warn("seek error in vioblk write"); return (1); } if (write(dev->fd, buf, sz) != sz) { - fprintf(stderr, "vioblk write error: %d\n\r", - errno); + log_warn("vioblk write error"); free(buf); return (1); } @@ -403,7 +397,7 @@ vioblk_notifyq(struct vioblk_dev *dev) vr = malloc(vr_sz); if (vr == NULL) { - fprintf(stderr, "malloc error getting vioblk ring\n\r"); + log_warn("malloc error getting vioblk ring"); return (0); } @@ -411,7 +405,7 @@ vioblk_notifyq(struct vioblk_dev *dev) for (i = 0; i < vr_sz; i += VIRTIO_PAGE_SIZE) { if (read_page((uint32_t)q_gpa + i, vr + i, PAGE_SIZE, 0)) { - fprintf(stderr, "error reading gpa 0x%x\n\r", + log_warnx("error reading gpa 0x%x", (uint32_t)q_gpa + i); free(vr); return (0); @@ -429,7 +423,7 @@ vioblk_notifyq(struct vioblk_dev *dev) idx = dev->vq[dev->cfg.queue_notify].last_avail & VIOBLK_QUEUE_MASK; if ((avail->idx & VIOBLK_QUEUE_MASK) == idx) { - fprintf(stderr, "vioblk queue notify - nothing to do?\n\r"); + log_warnx("vioblk queue notify - nothing to do?"); free(vr); return (0); } @@ -438,15 +432,15 @@ vioblk_notifyq(struct vioblk_dev *dev) cmd_desc = &desc[cmd_desc_idx]; if ((cmd_desc->flags & VRING_DESC_F_NEXT) == 0) { - fprintf(stderr, "unchained vioblk cmd descriptor received " - "(idx %d)\n\r", cmd_desc_idx); + log_warnx("unchained vioblk cmd descriptor received " + "(idx %d)", cmd_desc_idx); free(vr); return (0); } /* Read command from descriptor ring */ if (read_page((uint32_t)cmd_desc->addr, &cmd, cmd_desc->len, 0)) { - fprintf(stderr, "vioblk: command read_page error @ 0x%llx\n\r", + log_warnx("vioblk: command read_page error @ 0x%llx", cmd_desc->addr); free(vr); return (0); @@ -459,8 +453,8 @@ vioblk_notifyq(struct vioblk_dev *dev) secdata_desc = &desc[secdata_desc_idx]; if ((secdata_desc->flags & VRING_DESC_F_NEXT) == 0) { - fprintf(stderr, "unchained vioblk data descriptor " - "received (idx %d)\n\r", cmd_desc_idx); + log_warnx("unchained vioblk data descriptor " + "received (idx %d)", cmd_desc_idx); free(vr); return (0); } @@ -475,8 +469,8 @@ vioblk_notifyq(struct vioblk_dev *dev) secdata = vioblk_do_read(dev, cmd.sector + secbias, (ssize_t)secdata_desc->len); if (secdata == NULL) { - fprintf(stderr, "vioblk: block read error, " - "sector %lld\n\r", cmd.sector); + log_warnx("vioblk: block read error, " + "sector %lld", cmd.sector); free(vr); return (0); } @@ -490,8 +484,8 @@ vioblk_notifyq(struct vioblk_dev *dev) if (write_page((uint32_t)secdata_desc->addr + j, secdata + j, len, 0)) { - fprintf(stderr, "can't write sector " - "data to gpa @ 0x%llx\n\r", + log_warnx("can't write sector " + "data to gpa @ 0x%llx", secdata_desc->addr); dump_descriptor_chain(desc, cmd_desc_idx); free(vr); @@ -516,8 +510,8 @@ vioblk_notifyq(struct vioblk_dev *dev) ds = VIRTIO_BLK_S_OK; if (write_page((uint32_t)ds_desc->addr, &ds, ds_desc->len, 0)) { - fprintf(stderr, "can't write device status data @ " - "0x%llx\n\r", ds_desc->addr); + log_warnx("can't write device status data @ " + "0x%llx", ds_desc->addr); dump_descriptor_chain(desc, cmd_desc_idx); free(vr); return (0); @@ -534,7 +528,7 @@ vioblk_notifyq(struct vioblk_dev *dev) VIOBLK_QUEUE_MASK; if (push_virtio_ring(vr, q_gpa, vr_sz)) { - fprintf(stderr, "vioblk: error writing vio ring\n\r"); + log_warnx("vioblk: error writing vio ring"); } break; case VIRTIO_BLK_T_OUT: @@ -542,15 +536,15 @@ vioblk_notifyq(struct vioblk_dev *dev) secdata_desc = &desc[secdata_desc_idx]; if ((secdata_desc->flags & VRING_DESC_F_NEXT) == 0) { - fprintf(stderr, "wr vioblk: unchained vioblk data " - "descriptor received (idx %d)\n\r", cmd_desc_idx); + log_warnx("wr vioblk: unchained vioblk data " + "descriptor received (idx %d)", cmd_desc_idx); free(vr); return (0); } secdata = malloc(MAXPHYS); if (secdata == NULL) { - fprintf(stderr, "wr vioblk: malloc error, len %d\n\r", + log_warn("wr vioblk: malloc error, len %d", secdata_desc->len); free(vr); return (0); @@ -566,8 +560,8 @@ vioblk_notifyq(struct vioblk_dev *dev) len = secdata_desc->len - j; if (read_page((uint32_t)secdata_desc->addr + j, secdata + j, len, 0)) { - fprintf(stderr, "wr vioblk: can't read " - "sector data @ 0x%llx\n\r", + log_warnx("wr vioblk: can't read " + "sector data @ 0x%llx", secdata_desc->addr); dump_descriptor_chain(desc, cmd_desc_idx); @@ -581,8 +575,7 @@ vioblk_notifyq(struct vioblk_dev *dev) if (vioblk_do_write(dev, cmd.sector + secbias, secdata, (ssize_t)secdata_desc->len)) { - fprintf(stderr, "wr vioblk: disk write " - "error\n\r"); + log_warnx("wr vioblk: disk write error"); free(vr); free(secdata); return (0); @@ -603,8 +596,8 @@ vioblk_notifyq(struct vioblk_dev *dev) ds = VIRTIO_BLK_S_OK; if (write_page((uint32_t)ds_desc->addr, &ds, ds_desc->len, 0)) { - fprintf(stderr, "wr vioblk: can't write device status " - "data @ 0x%llx\n\r", ds_desc->addr); + log_warnx("wr vioblk: can't write device status " + "data @ 0x%llx", ds_desc->addr); dump_descriptor_chain(desc, cmd_desc_idx); free(vr); return (0); @@ -619,7 +612,7 @@ vioblk_notifyq(struct vioblk_dev *dev) dev->vq[dev->cfg.queue_notify].last_avail = avail->idx & VIOBLK_QUEUE_MASK; if (push_virtio_ring(vr, q_gpa, vr_sz)) - fprintf(stderr, "wr vioblk: error writing vio ring\n\r"); + log_warnx("wr vioblk: error writing vio ring"); break; case VIRTIO_BLK_T_FLUSH: case VIRTIO_BLK_T_FLUSH_OUT: @@ -629,8 +622,8 @@ vioblk_notifyq(struct vioblk_dev *dev) ds = VIRTIO_BLK_S_OK; if (write_page((uint32_t)ds_desc->addr, &ds, ds_desc->len, 0)) { - fprintf(stderr, "fl vioblk: can't write device status " - "data @ 0x%llx\n\r", ds_desc->addr); + log_warnx("fl vioblk: can't write device status " + "data @ 0x%llx", ds_desc->addr); dump_descriptor_chain(desc, cmd_desc_idx); free(vr); return (0); @@ -645,7 +638,7 @@ vioblk_notifyq(struct vioblk_dev *dev) dev->vq[dev->cfg.queue_notify].last_avail = avail->idx & VIOBLK_QUEUE_MASK; if (push_virtio_ring(vr, q_gpa, vr_sz)) { - fprintf(stderr, "fl vioblk: error writing vio ring\n\r"); + log_warnx("fl vioblk: error writing vio ring"); } break; } @@ -668,7 +661,7 @@ virtio_blk_io(int dir, uint16_t reg, uint32_t *data, uint8_t *intr, case VIRTIO_CONFIG_DEVICE_FEATURES: case VIRTIO_CONFIG_QUEUE_SIZE: case VIRTIO_CONFIG_ISR_STATUS: - fprintf(stderr, "%s: illegal write %x to %s\n\r", + log_warnx("%s: illegal write %x to %s", __progname, *data, virtio_reg_name(reg)); break; case VIRTIO_CONFIG_GUEST_FEATURES: @@ -743,7 +736,7 @@ virtio_net_io(int dir, uint16_t reg, uint32_t *data, uint8_t *intr, case VIRTIO_CONFIG_DEVICE_FEATURES: case VIRTIO_CONFIG_QUEUE_SIZE: case VIRTIO_CONFIG_ISR_STATUS: - fprintf(stderr, "%s: illegal write %x to %s\n\r", + log_warnx("%s: illegal write %x to %s", __progname, *data, virtio_reg_name(reg)); break; case VIRTIO_CONFIG_GUEST_FEATURES: @@ -850,7 +843,7 @@ vionet_enq_rx(struct vionet_dev *dev, char *pkt, ssize_t sz, int *spc) vr = malloc(vr_sz); if (vr == NULL) { - fprintf(stderr, "rx enq :malloc error getting vionet ring\n\r"); + log_warn("rx enq: malloc error getting vionet ring"); return (0); } @@ -858,7 +851,7 @@ vionet_enq_rx(struct vionet_dev *dev, char *pkt, ssize_t sz, int *spc) for (i = 0; i < vr_sz; i += VIRTIO_PAGE_SIZE) { if (read_page((uint32_t)q_gpa + i, vr + i, PAGE_SIZE, 0)) { - fprintf(stderr, "rx enq: error reading gpa 0x%x\n\r", + log_warnx("rx enq: error reading gpa 0x%x", (uint32_t)q_gpa + i); free(vr); return (0); @@ -875,8 +868,7 @@ vionet_enq_rx(struct vionet_dev *dev, char *pkt, ssize_t sz, int *spc) idx = dev->vq[0].last_avail & VIONET_QUEUE_MASK; if ((avail->idx & VIONET_QUEUE_MASK) == idx) { - fprintf(stderr, "vionet queue notify - no space, dropping " - "packet\n\r"); + log_warnx("vionet queue notify - no space, dropping packet"); free(vr); return (0); } @@ -889,7 +881,7 @@ vionet_enq_rx(struct vionet_dev *dev, char *pkt, ssize_t sz, int *spc) /* must be not readable */ if ((pkt_desc->flags & VRING_DESC_F_WRITE) == 0) { - fprintf(stderr, "unexpected readable rx descriptor %d\n\r", + log_warnx("unexpected readable rx descriptor %d", pkt_desc_idx); free(vr); return (0); @@ -897,8 +889,8 @@ vionet_enq_rx(struct vionet_dev *dev, char *pkt, ssize_t sz, int *spc) /* Write packet to descriptor ring */ if (write_page((uint32_t)pkt_desc->addr, pkt, sz, 0)) { - fprintf(stderr, "vionet: rx enq packet write_page error @ " - "0x%llx\n\r", pkt_desc->addr); + log_warnx("vionet: rx enq packet write_page error @ " + "0x%llx", pkt_desc->addr); free(vr); return (0); } @@ -911,7 +903,7 @@ vionet_enq_rx(struct vionet_dev *dev, char *pkt, ssize_t sz, int *spc) dev->vq[0].last_avail = (dev->vq[0].last_avail + 1); *spc = avail->idx - dev->vq[0].last_avail; if (push_virtio_ring(vr, q_gpa, vr_sz)) { - fprintf(stderr, "vionet: error writing vio ring\n\r"); + log_warnx("vionet: error writing vio ring"); } free(vr); @@ -947,8 +939,7 @@ vionet_process_rx(void) num_enq += vionet_enq_rx(&vionet[i], buf, sz, &spc); } else if (sz == 0) { - fprintf(stderr, "process_rx: no data" - "\n\r"); + log_debug("process_rx: no data"); hasdata = 0; } } @@ -981,7 +972,7 @@ vionet_notify_rx(struct vionet_dev *dev) vr = malloc(vr_sz); if (vr == NULL) { - fprintf(stderr, "malloc error getting vionet ring\n\r"); + log_warn("malloc error getting vionet ring"); return; } @@ -989,7 +980,7 @@ vionet_notify_rx(struct vionet_dev *dev) for (i = 0; i < vr_sz; i += VIRTIO_PAGE_SIZE) { if (read_page((uint32_t)q_gpa + i, vr + i, PAGE_SIZE, 0)) { - fprintf(stderr, "error reading gpa 0x%x\n\r", + log_warnx("error reading gpa 0x%x", (uint32_t)q_gpa + i); free(vr); return; @@ -1045,7 +1036,7 @@ vionet_notifyq(struct vionet_dev *dev) vr = malloc(vr_sz); if (vr == NULL) { - fprintf(stderr, "malloc error getting vionet ring\n\r"); + log_warn("malloc error getting vionet ring"); return (0); } @@ -1053,7 +1044,7 @@ vionet_notifyq(struct vionet_dev *dev) for (i = 0; i < vr_sz; i += VIRTIO_PAGE_SIZE) { if (read_page((uint32_t)q_gpa + i, vr + i, PAGE_SIZE, 0)) { - fprintf(stderr, "error reading gpa 0x%x\n\r", + log_warnx("error reading gpa 0x%x", (uint32_t)q_gpa + i); free(vr); return (0); @@ -1072,7 +1063,7 @@ vionet_notifyq(struct vionet_dev *dev) idx = dev->vq[dev->cfg.queue_notify].last_avail & VIONET_QUEUE_MASK; if ((avail->idx & VIONET_QUEUE_MASK) == idx) { - fprintf(stderr, "vionet tx queue notify - nothing to do?\n\r"); + log_warnx("vionet tx queue notify - nothing to do?"); free(vr); return (0); } @@ -1101,7 +1092,7 @@ vionet_notifyq(struct vionet_dev *dev) */ pkt = malloc(pktsz); if (pkt == NULL) { - fprintf(stderr, "malloc error alloc packet buf\n\r"); + log_warn("malloc error alloc packet buf"); free(vr); return (0); } @@ -1113,8 +1104,8 @@ vionet_notifyq(struct vionet_dev *dev) while (pkt_desc->flags & VRING_DESC_F_NEXT) { /* must be not writable */ if (pkt_desc->flags & VRING_DESC_F_WRITE) { - fprintf(stderr, "unexpceted writable tx desc " - "%d\n\r", pkt_desc_idx); + log_warnx("unexpceted writable tx desc " + "%d", pkt_desc_idx); free(vr); return (0); } @@ -1122,8 +1113,8 @@ vionet_notifyq(struct vionet_dev *dev) /* Read packet from descriptor ring */ if (read_page((uint32_t)pkt_desc->addr, pkt + ofs, pkt_desc->len, 0)) { - fprintf(stderr, "vionet: packet read_page error " - "@ 0x%llx\n\r", pkt_desc->addr); + log_warnx("vionet: packet read_page error " + "@ 0x%llx", pkt_desc->addr); free(pkt); free(vr); return (0); @@ -1136,8 +1127,8 @@ vionet_notifyq(struct vionet_dev *dev) /* Now handle tail descriptor - must be not writable */ if (pkt_desc->flags & VRING_DESC_F_WRITE) { - fprintf(stderr, "unexpceted writable tx descriptor %d" - "\n\r", pkt_desc_idx); + log_warnx("unexpceted writable tx descriptor %d", + pkt_desc_idx); free(vr); return (0); } @@ -1145,8 +1136,8 @@ vionet_notifyq(struct vionet_dev *dev) /* Read packet from descriptor ring */ if (read_page((uint32_t)pkt_desc->addr, pkt + ofs, pkt_desc->len, 0)) { - fprintf(stderr, "vionet: packet read_page error @ " - "0x%llx\n\r", pkt_desc->addr); + log_warnx("vionet: packet read_page error @ " + "0x%llx", pkt_desc->addr); free(pkt); free(vr); return (0); @@ -1154,8 +1145,8 @@ vionet_notifyq(struct vionet_dev *dev) /* XXX signed vs unsigned here, funky cast */ if (write(dev->fd, pkt, pktsz) != (int)pktsz) { - fprintf(stderr, "vionet: tx failed writing to tap: " - "%d\n\r", errno); + log_warnx("vionet: tx failed writing to tap: " + "%d", errno); free(pkt); free(vr); return (0); @@ -1176,7 +1167,7 @@ vionet_notifyq(struct vionet_dev *dev) } if (push_virtio_ring(vr, q_gpa, vr_sz)) { - fprintf(stderr, "vionet: tx error writing vio ring\n\r"); + log_warnx("vionet: tx error writing vio ring"); } free(vr); @@ -1198,13 +1189,13 @@ virtio_init(struct vm_create_params *vcp, int *child_disks, int *child_taps) PCI_SUBCLASS_SYSTEM_MISC, PCI_VENDOR_OPENBSD, PCI_PRODUCT_VIRTIO_ENTROPY, 1, NULL)) { - fprintf(stderr, "%s: can't add PCI virtio rng device\n", + log_warnx("%s: can't add PCI virtio rng device", __progname); return; } if (pci_add_bar(id, PCI_MAPREG_TYPE_IO, virtio_rnd_io, NULL)) { - fprintf(stderr, "%s: can't add bar for virtio rng device\n", + log_warnx("%s: can't add bar for virtio rng device", __progname); return; } @@ -1220,7 +1211,7 @@ virtio_init(struct vm_create_params *vcp, int *child_disks, int *child_taps) vioblk = malloc(sizeof(struct vioblk_dev) * vcp->vcp_ndisks); if (vioblk == NULL) { - fprintf(stderr, "%s: malloc failure allocating vioblks\n", + log_warn("%s: malloc failure allocating vioblks", __progname); return; } @@ -1237,14 +1228,14 @@ virtio_init(struct vm_create_params *vcp, int *child_disks, int *child_taps) PCI_SUBCLASS_MASS_STORAGE_SCSI, PCI_VENDOR_OPENBSD, PCI_PRODUCT_VIRTIO_BLOCK, 1, NULL)) { - fprintf(stderr, "%s: can't add PCI virtio block " - "device\n", __progname); + log_warnx("%s: can't add PCI virtio block " + "device", __progname); return; } if (pci_add_bar(id, PCI_MAPREG_TYPE_IO, virtio_blk_io, &vioblk[i])) { - fprintf(stderr, "%s: can't add bar for virtio block " - "device\n", __progname); + log_warnx("%s: can't add bar for virtio block " + "device", __progname); return; } vioblk[i].vq[0].qs = VIOBLK_QUEUE_SIZE; @@ -1260,7 +1251,7 @@ virtio_init(struct vm_create_params *vcp, int *child_disks, int *child_taps) vionet = malloc(sizeof(struct vionet_dev) * vcp->vcp_nnics); if (vionet == NULL) { - fprintf(stderr, "%s: malloc failure allocating vionets\n", + log_warn("%s: malloc failure allocating vionets", __progname); return; } @@ -1275,15 +1266,15 @@ virtio_init(struct vm_create_params *vcp, int *child_disks, int *child_taps) PCI_SUBCLASS_SYSTEM_MISC, PCI_VENDOR_OPENBSD, PCI_PRODUCT_VIRTIO_NETWORK, 1, NULL)) { - fprintf(stderr, "%s: can't add PCI virtio net device\n", + log_warnx("%s: can't add PCI virtio net device", __progname); return; } if (pci_add_bar(id, PCI_MAPREG_TYPE_IO, virtio_net_io, &vionet[i])) { - fprintf(stderr, "%s: can't add bar for virtio net " - "device\n", __progname); + log_warnx("%s: can't add bar for virtio net " + "device", __progname); return; } diff --git a/usr.sbin/vmd/vmd.8 b/usr.sbin/vmd/vmd.8 index bf838516d60..367a12734a1 100644 --- a/usr.sbin/vmd/vmd.8 +++ b/usr.sbin/vmd/vmd.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: vmd.8,v 1.2 2015/11/22 21:24:48 jmc Exp $ +.\" $OpenBSD: vmd.8,v 1.3 2015/11/23 13:04:49 reyk Exp $ .\" .\" Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org> .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: November 22 2015 $ +.Dd $Mdocdate: November 23 2015 $ .Dt VMD 8 .Os .Sh NAME @@ -22,7 +22,7 @@ .Nd virtual machine daemon .Sh SYNOPSIS .Nm vmd -.Op Fl f Ar file +.Op Fl dv .Sh DESCRIPTION .Nm is a daemon responsible for the execution of virtual machines (VMs) on a @@ -55,6 +55,18 @@ is also responsible for proxying various other commands/requests from such as stopping VMs, and retrieving information from .Xr vmm 4 about running VMs. +.Pp +The options are as follows: +.Bl -tag -width Dssmacro=value +.It Fl d +Do not daemonize and log to +.Em stderr . +.It Fl v +Verbose mode. +Multiple +.Fl v +options increases the verbosity. +.El .Sh SEE ALSO .Xr vmm 4 , .Xr rc.conf 8 , diff --git a/usr.sbin/vmd/vmd.c b/usr.sbin/vmd/vmd.c index b51eafb1795..e6c773d270e 100644 --- a/usr.sbin/vmd/vmd.c +++ b/usr.sbin/vmd/vmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vmd.c,v 1.3 2015/11/22 22:29:48 deraadt Exp $ */ +/* $OpenBSD: vmd.c,v 1.4 2015/11/23 13:04:49 reyk Exp $ */ /* * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org> @@ -39,7 +39,6 @@ #include <machine/param.h> #include <machine/vmmvar.h> -#include <err.h> #include <errno.h> #include <fcntl.h> #include <imsg.h> @@ -51,6 +50,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <syslog.h> #include <termios.h> #include <unistd.h> #include <util.h> @@ -112,6 +112,8 @@ struct ns8250_regs { struct i8253_counter i8253_counter[3]; struct ns8250_regs com1_regs; +__dead void usage(void); + void sighdlr(int); int main(int, char **); int control_run(void); @@ -171,15 +173,39 @@ sighdlr(int sig) } } +__dead void +usage(void) +{ + extern char *__progname; + fprintf(stderr, "usage: %s [-dv]", __progname); + exit(1); +} + int main(int argc, char **argv) { - int res; + int debug = 0, verbose = 0, c, res; + + while ((c = getopt(argc, argv, "dv")) != -1) { + switch (c) { + case 'd': + debug = 2; + break; + case 'v': + verbose++; + break; + default: + usage(); + } + } + + /* log to stderr until daemonized */ + log_init(debug ? debug : 1, LOG_DAEMON); /* Open /dev/vmm */ vmm_fd = open(VMM_NODE, O_RDONLY); if (vmm_fd == -1) - errx(1, "can't open vmm device node %s", VMM_NODE); + fatal("can't open vmm device node %s", VMM_NODE); setproctitle("control"); @@ -189,13 +215,17 @@ main(int argc, char **argv) signal(SIGINT, sighdlr); signal(SIGCHLD, sighdlr); - if (daemon(0, 1) == -1) - errx(1, "can't daemonize\n"); + log_init(debug, LOG_DAEMON); + log_verbose(verbose); + log_procinit("control"); + + if (!debug && daemon(1, 0) == -1) + fatal("can't daemonize"); res = control_run(); if (res == -1) - errx(1, "control socket error\n"); + fatalx("control socket error"); return (0); } @@ -225,7 +255,7 @@ control_run(void) /* Establish and start listening on control socket */ socketpath = SOCKET_NAME; if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) { - fprintf(stderr, "%s: socket error\n", __progname); + log_warn("%s: socket error", __progname); return (-1); } @@ -233,14 +263,14 @@ control_run(void) sun.sun_family = AF_UNIX; if (strlcpy(sun.sun_path, socketpath, sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) { - fprintf(stderr, "%s: socket name too long\n", __progname); + log_warnx("%s: socket name too long", __progname); close(fd); return (-1); } if (unlink(socketpath) == -1) if (errno != ENOENT) { - fprintf(stderr, "%s: unlink of %s failed\n", + log_warn("%s: unlink of %s failed", __progname, socketpath); close(fd); return (-1); @@ -250,7 +280,7 @@ control_run(void) mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP; if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) { - fprintf(stderr, "%s: control_init: bind of %s failed\n", + log_warn("%s: control_init: bind of %s failed", __progname, socketpath); close(fd); umask(old_umask); @@ -260,7 +290,7 @@ control_run(void) umask(old_umask); if (chmod(socketpath, mode) == -1) { - fprintf(stderr, "%s: control_init: chmod of %s failed\n", + log_warn("%s: control_init: chmod of %s failed", __progname, socketpath); close(fd); unlink(socketpath); @@ -268,14 +298,14 @@ control_run(void) } if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL) { - fprintf(stderr, "%s: out of memory\n", __progname); + log_warn("%s: out of memory", __progname); close(fd); unlink(socketpath); return (-1); } if (listen(fd, NR_BACKLOG) == -1) { - fprintf(stderr, "%s: listen failed\n", __progname); + log_warn("%s: listen failed", __progname); close(fd); unlink(socketpath); return (-1); @@ -284,7 +314,7 @@ control_run(void) while (!quit) { if ((connfd = accept4(fd, (struct sockaddr *)&c_sun, &len, SOCK_CLOEXEC)) == -1) { - fprintf(stderr, "%s: accept4 error\n", __progname); + log_warn("%s: accept4 error", __progname); close(fd); unlink(socketpath); return (-1); @@ -292,7 +322,7 @@ control_run(void) imsg_init(ibuf, connfd); if ((n = imsg_read(ibuf)) == -1 || n == 0) { - fprintf(stderr, "%s: imsg_read error, n=%d\n", + log_warnx("%s: imsg_read error, n=%d", __progname, n); continue; } @@ -341,9 +371,8 @@ control_run(void) while (ibuf->w.queued) if (msgbuf_write(&ibuf->w) <= 0 && errno != EAGAIN) { - fprintf(stderr, "%s: msgbuf_write " - "error %d\n", __progname, - errno); + log_warn("%s: msgbuf_write error", + __progname); close(fd); close(connfd); unlink(socketpath); @@ -505,8 +534,8 @@ start_vm(struct imsg *imsg) child_disks[i] = open(vcp->vcp_disks[i], O_RDWR); if (child_disks[i] == -1) { ret = errno; - fprintf(stderr, "%s: can't open %s (%d)\n", __progname, - vcp->vcp_disks[i], errno); + log_warn("%s: can't open %s", __progname, + vcp->vcp_disks[i]); goto err; } } @@ -514,8 +543,8 @@ start_vm(struct imsg *imsg) bzero(&sb, sizeof(sb)); if (stat(vcp->vcp_kernel, &sb) == -1) { ret = errno; - fprintf(stderr, "%s: can't stat kernel image %s (%d)\n", - __progname, vcp->vcp_kernel, errno); + log_warn("%s: can't stat kernel image %s", + __progname, vcp->vcp_kernel); goto err; } @@ -525,22 +554,20 @@ start_vm(struct imsg *imsg) kernel_fd = open(vcp->vcp_kernel, O_RDONLY); if (kernel_fd == -1) { ret = errno; - fprintf(stderr, "%s: can't open kernel image %s (%d)\n", - __progname, vcp->vcp_kernel, errno); + log_warn("%s: can't open kernel image %s", + __progname, vcp->vcp_kernel); goto err; } if (openpty(&ttym_fd, &ttys_fd, ptyn, NULL, NULL) == -1) { ret = errno; - fprintf(stderr, "%s: openpty failed: %d\n", - __progname, errno); + log_warn("%s: openpty failed", __progname); goto err; } if (close(ttys_fd)) { ret = errno; - fprintf(stderr, "%s: close tty failed: %d\n", - __progname, errno); + log_warn("%s: close tty failed", __progname); goto err; } @@ -549,8 +576,8 @@ start_vm(struct imsg *imsg) child_taps[i] = opentap(); if (child_taps[i] == -1) { ret = errno; - fprintf(stderr, "%s: can't open tap for nic %zd (%d)\n", - __progname, i, errno); + log_warn("%s: can't open tap for nic %zd", + __progname, i); goto err; } } @@ -579,31 +606,28 @@ start_vm(struct imsg *imsg) } else { /* Child */ - fprintf(stderr, "%s: vm console: %s\n", __progname, ptyn); - ret = vmm_create_vm(vcp); setproctitle(vcp->vcp_name); + log_procinit(vcp->vcp_name); + + log_info("%s: vm console: %s", __progname, ptyn); + ret = vmm_create_vm(vcp); if (ret) { - fprintf(stderr, "%s: create vmm ioctl failed - " - "exiting (%d)\n", __progname, ret); - _exit(1); + errno = ret; + fatal("create vmm ioctl failed - exiting"); } /* Load kernel image */ ret = loadelf_main(kernel_fd, vcp->vcp_id, vcp->vcp_memory_size); if (ret) { - fprintf(stderr, "%s: failed to load kernel - " - "exiting (%d)\n", __progname, ret); - _exit(1); + errno = ret; + fatal("failed to load kernel - exiting"); } close(kernel_fd); con_fd = ttym_fd; - if (fcntl(con_fd, F_SETFL, O_NONBLOCK) == -1) { - fprintf(stderr, "%s: failed to set nonblocking mode " - "on console\n", __progname); - _exit(1); - } + if (fcntl(con_fd, F_SETFL, O_NONBLOCK) == -1) + fatal("failed to set nonblocking mode on console"); /* Execute the vcpu run loop(s) for this VM */ ret = run_vm(child_disks, child_taps, vcp); @@ -721,7 +745,7 @@ start_client_vmd(void) pw = getpwnam(VMD_USER); if (pw == NULL) { - fprintf(stderr, "%s: no such user %s\n", __progname, VMD_USER); + log_warnx("%s: no such user %s", __progname, VMD_USER); return (-1); } @@ -732,16 +756,16 @@ start_client_vmd(void) if (!child_pid) { /* Child */ if (chroot(pw->pw_dir) != 0) - err(1, "unable to chroot"); + fatal("unable to chroot"); if (chdir("/") != 0) - err(1, "unable to chdir"); + fatal("unable to chdir"); if (setgroups(1, &pw->pw_gid) == -1) - err(1, "setgroups() failed"); + fatal("setgroups() failed"); if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1) - err(1, "setresgid() failed"); + fatal("setresgid() failed"); if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1) - err(1, "setresuid() failed"); + fatal("setresuid() failed"); return (0); } @@ -846,7 +870,7 @@ run_vm(int *child_disks, int *child_taps, struct vm_create_params *vcp) tid = malloc(sizeof(pthread_t) * vcp->vcp_ncpus); vrp = malloc(sizeof(struct vm_run_params *) * vcp->vcp_ncpus); if (tid == NULL || vrp == NULL) { - fprintf(stderr, "%s: memory allocation error - exiting.\n", + log_warn("%s: memory allocation error - exiting.", __progname); return (ENOMEM); } @@ -862,15 +886,15 @@ run_vm(int *child_disks, int *child_taps, struct vm_create_params *vcp) for (i = 0 ; i < vcp->vcp_ncpus; i++) { vrp[i] = malloc(sizeof(struct vm_run_params)); if (vrp[i] == NULL) { - fprintf(stderr, "%s: memory allocation error - " - "exiting.\n", __progname); + log_warn("%s: memory allocation error - " + "exiting.", __progname); /* caller will exit, so skip free'ing */ return (ENOMEM); } vrp[i]->vrp_exit = malloc(sizeof(union vm_exit)); if (vrp[i]->vrp_exit == NULL) { - fprintf(stderr, "%s: memory allocation error - " - "exiting.\n", __progname); + log_warn("%s: memory allocation error - " + "exiting.", __progname); /* caller will exit, so skip free'ing */ return (ENOMEM); } @@ -888,14 +912,14 @@ run_vm(int *child_disks, int *child_taps, struct vm_create_params *vcp) /* Wait for all the threads to exit */ for (i = 0; i < vcp->vcp_ncpus; i++) { if (pthread_join(tid[i], &exit_status)) { - fprintf(stderr, "%s: failed to join thread %zd - " - "exiting\n", __progname, i); + log_warn("%s: failed to join thread %zd - " + "exiting", __progname, i); return (EIO); } if (exit_status != NULL) { - fprintf(stderr, "%s: vm %d vcpu run thread %zd exited " - "abnormally\n", __progname, vcp->vcp_id, i); + log_warnx("%s: vm %d vcpu run thread %zd exited " + "abnormally", __progname, vcp->vcp_id, i); ret = EIO; } } @@ -975,8 +999,8 @@ vcpu_exit_i8253(union vm_exit *vei) (TIMER_SEL0 | TIMER_SEL1 | TIMER_SEL2); sel = sel >> 6; if (sel > 2) { - fprintf(stderr, "%s: i8253 PIT: invalid " - "timer selected (%d)\n", + log_warnx("%s: i8253 PIT: invalid " + "timer selected (%d)", __progname, sel); return; } @@ -990,8 +1014,8 @@ vcpu_exit_i8253(union vm_exit *vei) * XXX this seems to be used on occasion, needs * to be implemented */ - fprintf(stderr, "%s: i8253 PIT: 16 bit " - "counter I/O not supported\n", + log_warnx("%s: i8253 PIT: 16 bit " + "counter I/O not supported", __progname); return; } @@ -1028,13 +1052,13 @@ vcpu_exit_i8253(union vm_exit *vei) return; } - fprintf(stderr, "%s: i8253 PIT: unsupported rw mode " - "%d\n", __progname, rw); + log_warnx("%s: i8253 PIT: unsupported rw mode " + "%d", __progname, rw); return; } else { /* XXX should this return 0xff? */ - fprintf(stderr, "%s: i8253 PIT: read from control " - "port unsupported\n", __progname); + log_warnx("%s: i8253 PIT: read from control " + "port unsupported", __progname); } } else { sel = vei->vei.vei_port - (TIMER_CNTR0 + TIMER_BASE); @@ -1108,7 +1132,7 @@ vcpu_process_com_data(union vm_exit *vei) } else { /* XXX should this be com1_regs.data or 0xff? */ vei->vei.vei_data = com1_regs.data; - fprintf(stderr, "guest reading com1 when not ready\n"); + log_warnx("guest reading com1 when not ready"); } /* Reading the data register always clears RXRDY from IIR */ @@ -1242,7 +1266,7 @@ vcpu_process_com_lsr(union vm_exit *vei) * continue. */ if (vei->vei.vei_dir == 0) { - fprintf(stderr, "%s: LSR UART write 0x%x unsupported\n", + log_warnx("%s: LSR UART write 0x%x unsupported", __progname, vei->vei.vei_data); } else { /* @@ -1274,7 +1298,7 @@ vcpu_process_com_msr(union vm_exit *vei) * continue. */ if (vei->vei.vei_dir == 0) { - fprintf(stderr, "%s: MSR UART write 0x%x unsupported\n", + log_warnx("%s: MSR UART write 0x%x unsupported", __progname, vei->vei.vei_data); } else { /* @@ -1425,7 +1449,7 @@ vcpu_exit_pci(struct vm_run_params *vrp) intr = pci_handle_io(vrp); break; default: - fprintf(stderr, "%s: unknown PCI register 0x%llx\n", + log_warnx("%s: unknown PCI register 0x%llx", __progname, (uint64_t)vei->vei.vei_port); break; } @@ -1512,7 +1536,7 @@ vcpu_exit(struct vm_run_params *vrp) */ break; default: - fprintf(stderr, "%s: unknown exit reason %d\n", + log_warnx("%s: unknown exit reason %d", __progname, vrp->vrp_exit_reason); return (1); } @@ -1581,7 +1605,6 @@ vcpu_exit(struct vm_run_params *vrp) int write_page(uint32_t dst, void *buf, uint32_t len, int do_mask) { - int ret; struct vm_writepage_params vwp; /* @@ -1596,9 +1619,8 @@ write_page(uint32_t dst, void *buf, uint32_t len, int do_mask) vwp.vwp_vm_id = vm_id; vwp.vwp_len = len; if (ioctl(vmm_fd, VMM_IOC_WRITEPAGE, &vwp) < 0) { - ret = errno; - fprintf(stderr, "writepage ioctl failed: %d\n", ret); - return (ret); + log_warn("writepage ioctl failed"); + return (errno); } return (0); } @@ -1624,7 +1646,6 @@ write_page(uint32_t dst, void *buf, uint32_t len, int do_mask) int read_page(uint32_t src, void *buf, uint32_t len, int do_mask) { - int ret; struct vm_readpage_params vrp; /* @@ -1639,9 +1660,8 @@ read_page(uint32_t src, void *buf, uint32_t len, int do_mask) vrp.vrp_vm_id = vm_id; vrp.vrp_len = len; if (ioctl(vmm_fd, VMM_IOC_READPAGE, &vrp) < 0) { - ret = errno; - fprintf(stderr, "readpage ioctl failed: %d\n", ret); - return (ret); + log_warn("readpage ioctl failed"); + return (errno); } return (0); } diff --git a/usr.sbin/vmd/vmd.h b/usr.sbin/vmd/vmd.h index 768fcb2e612..c960dcfa735 100644 --- a/usr.sbin/vmd/vmd.h +++ b/usr.sbin/vmd/vmd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: vmd.h,v 1.2 2015/11/22 21:51:32 reyk Exp $ */ +/* $OpenBSD: vmd.h,v 1.3 2015/11/23 13:04:49 reyk Exp $ */ /* * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org> @@ -16,6 +16,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <stdarg.h> + #ifndef __VMD_H__ #define __VMD_H__ @@ -27,7 +29,7 @@ /* #define VMD_DEBUG */ #ifdef VMD_DEBUG -#define dprintf(x...) do { fprintf(stderr, x); } while(0) +#define dprintf(x...) do { log_debug(x); } while(0) #else #define dprintf(x...) #endif /* VMM_DEBUG */ @@ -51,4 +53,25 @@ enum imsg_type { int write_page(uint32_t dst, void *buf, uint32_t, int); int read_page(uint32_t dst, void *buf, uint32_t, int); +/* log.c */ +void log_init(int, int); +void log_procinit(const char *); +void log_verbose(int); +void log_warn(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_warnx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_info(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void log_debug(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void logit(int, const char *, ...) + __attribute__((__format__ (printf, 2, 3))); +void vlog(int, const char *, va_list) + __attribute__((__format__ (printf, 2, 0))); +__dead void fatal(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +__dead void fatalx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); + #endif /* __VMD_H__ */ |