Beispiel #1
0
int
uart_set_backend(struct uart_softc *sc, const char *opts)
{
	int retval;
#ifndef WITHOUT_CAPSICUM
	cap_rights_t rights;
	cap_ioctl_t cmds[] = { TIOCGETA, TIOCSETA, TIOCGWINSZ };
#endif

	retval = -1;

	if (opts == NULL)
		return (0);

	if (strcmp("stdio", opts) == 0) {
		if (!uart_stdio) {
			sc->tty.fd = STDIN_FILENO;
			sc->tty.opened = true;
			uart_stdio = true;
			retval = 0;
		}
	} else if (uart_tty_backend(sc, opts) == 0) {
		retval = 0;
	}

	/* Make the backend file descriptor non-blocking */
	if (retval == 0)
		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)
		uart_opentty(sc);

	return (retval);
}
Beispiel #2
0
int
main(int argc, char *argv[])
{
	LIST *p;
	int n, fd, rval, wval;
	char *bp;
	int append, ch, exitval;
	char *buf;
#define	BSIZE (8 * 1024)

	append = 0;
	while ((ch = getopt(argc, argv, "ai")) != -1)
		switch((char)ch) {
		case 'a':
			append = 1;
			break;
		case 'i':
			(void)signal(SIGINT, SIG_IGN);
			break;
		case '?':
		default:
			usage();
		}
	argv += optind;
	argc -= optind;

	if ((buf = malloc(BSIZE)) == NULL)
		err(1, "malloc");

	if (caph_limit_stdin() == -1 || caph_limit_stderr() == -1)
		err(EXIT_FAILURE, "unable to limit stdio");

	add(STDOUT_FILENO, "stdout");

	for (exitval = 0; *argv; ++argv)
		if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND :
		    O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) {
			warn("%s", *argv);
			exitval = 1;
		} else
			add(fd, *argv);

	if (caph_enter() < 0)
		err(EXIT_FAILURE, "unable to enter capability mode");
	while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0)
		for (p = head; p; p = p->next) {
			n = rval;
			bp = buf;
			do {
				if ((wval = write(p->fd, bp, n)) == -1) {
					warn("%s", p->name);
					exitval = 1;
					break;
				}
				bp += wval;
			} while (n -= wval);
		}
	if (rval < 0)
		err(1, "read");
	exit(exitval);
}