Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/bhyve/uart_emul.c
Show All 26 Lines | |||||
* $FreeBSD$ | * $FreeBSD$ | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <dev/ic/ns16550.h> | #include <dev/ic/ns16550.h> | ||||
#ifndef WITHOUT_CAPSICUM | |||||
#include <sys/capsicum.h> | |||||
#include <capsicum_helpers.h> | |||||
#endif | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <assert.h> | #include <assert.h> | ||||
#include <err.h> | |||||
#include <errno.h> | |||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <termios.h> | #include <termios.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <stdbool.h> | #include <stdbool.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <pthread.h> | #include <pthread.h> | ||||
#include <sysexits.h> | |||||
#include "mevent.h" | #include "mevent.h" | ||||
#include "uart_emul.h" | #include "uart_emul.h" | ||||
#define COM1_BASE 0x3F8 | #define COM1_BASE 0x3F8 | ||||
#define COM1_IRQ 4 | #define COM1_IRQ 4 | ||||
#define COM2_BASE 0x2F8 | #define COM2_BASE 0x2F8 | ||||
#define COM2_IRQ 3 | #define COM2_IRQ 3 | ||||
▲ Show 20 Lines • Show All 580 Lines • ▼ Show 20 Lines | uart_tty_backend(struct uart_softc *sc, const char *opts) | ||||
retval = -1; | retval = -1; | ||||
fd = open(opts, O_RDWR | O_NONBLOCK); | fd = open(opts, O_RDWR | O_NONBLOCK); | ||||
if (fd > 0 && isatty(fd)) { | if (fd > 0 && isatty(fd)) { | ||||
sc->tty.fd = fd; | sc->tty.fd = fd; | ||||
sc->tty.opened = true; | sc->tty.opened = true; | ||||
retval = 0; | retval = 0; | ||||
} | } | ||||
return (retval); | return (retval); | ||||
} | } | ||||
int | int | ||||
uart_set_backend(struct uart_softc *sc, const char *opts) | uart_set_backend(struct uart_softc *sc, const char *opts) | ||||
{ | { | ||||
int retval; | int retval; | ||||
#ifndef WITHOUT_CAPSICUM | |||||
cap_rights_t rights; | |||||
cap_ioctl_t cmds[] = { TIOCGETA, TIOCSETA, TIOCGWINSZ }; | |||||
#endif | |||||
retval = -1; | retval = -1; | ||||
if (opts == NULL) | if (opts == NULL) | ||||
return (0); | return (0); | ||||
if (strcmp("stdio", opts) == 0) { | if (strcmp("stdio", opts) == 0) { | ||||
if (!uart_stdio) { | if (!uart_stdio) { | ||||
sc->tty.fd = STDIN_FILENO; | sc->tty.fd = STDIN_FILENO; | ||||
sc->tty.opened = true; | sc->tty.opened = true; | ||||
uart_stdio = true; | uart_stdio = true; | ||||
retval = 0; | retval = 0; | ||||
} | } | ||||
} else if (uart_tty_backend(sc, opts) == 0) { | } else if (uart_tty_backend(sc, opts) == 0) { | ||||
retval = 0; | retval = 0; | ||||
} | } | ||||
/* Make the backend file descriptor non-blocking */ | /* Make the backend file descriptor non-blocking */ | ||||
if (retval == 0) | if (retval == 0) | ||||
retval = fcntl(sc->tty.fd, F_SETFL, O_NONBLOCK); | retval = fcntl(sc->tty.fd, F_SETFL, O_NONBLOCK); | ||||
#ifndef WITHOUT_CAPSICUM | |||||
cap_rights_init(&rights, CAP_EVENT, CAP_IOCTL, CAP_READ, CAP_WRITE); | |||||
if (cap_rights_limit(sc->tty.fd, &rights) == -1 && errno != ENOSYS) | |||||
errx(EX_OSERR, "Unable to apply rights for sandbox"); | |||||
if (cap_ioctls_limit(sc->tty.fd, cmds, nitems(cmds)) == -1 && errno != ENOSYS) | |||||
errx(EX_OSERR, "Unable to apply rights for sandbox"); | |||||
if (!uart_stdio) { | |||||
if (caph_limit_stdin() == -1 && errno != ENOSYS) | |||||
errx(EX_OSERR, "Unable to apply rights for sandbox"); | |||||
} | |||||
#endif | |||||
if (retval == 0) | if (retval == 0) | ||||
uart_opentty(sc); | uart_opentty(sc); | ||||
return (retval); | return (retval); | ||||
} | } |