diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | ingest.c | 52 | ||||
-rw-r--r-- | ptee.c | 49 | ||||
-rw-r--r-- | ssh-command.sh | 2 | ||||
-rw-r--r-- | view.c | 41 |
6 files changed, 42 insertions, 114 deletions
diff --git a/.gitignore b/.gitignore index b1b280d..db06a66 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -*.o chroot.tar ingest ptee diff --git a/Makefile b/Makefile index 7f55f7b..58e7cc9 100644 --- a/Makefile +++ b/Makefile @@ -6,15 +6,6 @@ LDLIBS = -lutil all: tags $(BINS) -ingest: ingest.o winch.o - $(CC) $(LDFLAGS) ingest.o winch.o $(LDLIBS) -o $@ - -ptee: ptee.o winch.o - $(CC) $(LDFLAGS) ptee.o winch.o $(LDLIBS) -o $@ - -view: view.o winch.o - $(CC) $(LDFLAGS) view.o winch.o $(LDLIBS) -o $@ - tags: *.c ctags -w *.c @@ -42,6 +33,6 @@ chroot.tar: $(BINS) tar -c -f chroot.tar -C root bin home lib libexec usr clean: - rm -f tags *.o $(BINS) chroot.tar + rm -f tags $(BINS) chroot.tar .PHONY: all clean diff --git a/ingest.c b/ingest.c index 52a50af..dfc480c 100644 --- a/ingest.c +++ b/ingest.c @@ -15,34 +15,27 @@ */ #include <err.h> -#include <errno.h> #include <fcntl.h> -#include <poll.h> -#include <sys/ioctl.h> #include <sys/ioctl.h> #include <sysexits.h> #include <unistd.h> -extern int winch(void); - static struct { - int winch; int local; int remote; -} fd = { -1, -1, STDIN_FILENO }; +} fd = { -1, STDIN_FILENO }; int main(int argc, char *argv[]) { if (argc < 2) errx(EX_USAGE, "missing private id"); - if (!isatty(STDERR_FILENO)) errx(EX_USAGE, "no terminal (use ssh -t)"); const char *path = argv[1]; fd.local = open(path, O_WRONLY); if (fd.local < 0) err(EX_NOINPUT, "%s", path); struct winsize window; - int error = ioctl(STDERR_FILENO, TIOCGWINSZ, &window); - if (error) err(EX_IOERR, "TIOCGWINSZ"); - fd.winch = winch(); + ssize_t size = read(fd.remote, &window, sizeof(window)); + if (size < 0) err(EX_IOERR, "read(%d)", fd.remote); + if ((size_t)size < sizeof(window)) errx(EX_IOERR, "short read(%d)", fd.remote); for (;;) { int error = ftruncate(fd.local, 0); @@ -58,39 +51,16 @@ int main(int argc, char *argv[]) { } char buf[4096]; - struct pollfd fds[2] = { - { .fd = fd.remote, .events = POLLIN }, - { .fd = fd.winch, .events = POLLIN }, - }; for (ssize_t totalSize = 0; totalSize < 1024 * 1024;) { - int nfds = poll(fds, 2, -1); - if (nfds < 0) { - if (errno == EINTR) continue; - err(EX_IOERR, "poll"); - } - - if (fds[0].revents & POLLIN) { - ssize_t readSize = read(fd.remote, buf, sizeof(buf)); - if (readSize < 0) err(EX_IOERR, "read(%d)", fd.remote); - if (!readSize) return EX_OK; - - ssize_t writeSize = write(fd.local, buf, readSize); - if (writeSize < 0) err(EX_IOERR, "write(%d)", fd.local); - if (writeSize < readSize) errx(EX_IOERR, "short write(%d)", fd.local); - - totalSize += readSize; - } + ssize_t readSize = read(fd.remote, buf, sizeof(buf)); + if (readSize < 0) err(EX_IOERR, "read(%d)", fd.remote); + if (!readSize) return EX_OK; - if (fds[0].revents & POLLHUP) return EX_OK; + ssize_t writeSize = write(fd.local, buf, readSize); + if (writeSize < 0) err(EX_IOERR, "write(%d)", fd.local); + if (writeSize < readSize) errx(EX_IOERR, "short write(%d)", fd.local); - if (fds[1].revents & POLLIN) { - ssize_t readSize = read(fd.winch, &window, sizeof(window)); - if (readSize < 0) err(EX_IOERR, "read(%d)", fd.winch); - if ((size_t)readSize < sizeof(window)) { - errx(EX_IOERR, "short read(%d)", fd.winch); - } - break; - } + totalSize += readSize; } } } diff --git a/ptee.c b/ptee.c index 7dec8c1..785b25a 100644 --- a/ptee.c +++ b/ptee.c @@ -15,7 +15,6 @@ */ #include <err.h> -#include <errno.h> #include <poll.h> #include <pwd.h> #include <stdlib.h> @@ -33,15 +32,12 @@ #include <util.h> #endif -extern int winch(void); - static struct { int pty; - int winch; int input; int local; int remote; -} fd = { -1, -1, STDIN_FILENO, STDERR_FILENO, STDOUT_FILENO }; +} fd = { -1, STDIN_FILENO, STDERR_FILENO, STDOUT_FILENO }; static struct termios saveTerm; static void restoreTerm(void) { @@ -76,7 +72,6 @@ int main(int argc, char *argv[]) { struct winsize window; error = ioctl(fd.local, TIOCGWINSZ, &window); if (error) err(EX_IOERR, "TIOCGWINSZ"); - fd.winch = winch(); pid_t pid = forkpty(&fd.pty, NULL, NULL, &window); if (pid < 0) err(EX_OSERR, "forkpty"); @@ -86,21 +81,18 @@ int main(int argc, char *argv[]) { err(EX_NOINPUT, "%s", *argv); } + ssize_t size = write(fd.remote, &window, sizeof(window)); + if (size < 0) err(EX_IOERR, "write(%d)", fd.remote); + if ((size_t)size < sizeof(window)) errx(EX_IOERR, "short write(%d)", fd.remote); + char buf[4096]; ssize_t totalSize = 0; - struct pollfd fds[3] = { + struct pollfd fds[2] = { { .fd = fd.input, .events = POLLIN }, { .fd = fd.pty, .events = POLLIN }, - { .fd = fd.winch, .events = POLLIN }, }; - for (;;) { - int nfds = poll(fds, 3, -1); - if (nfds < 0) { - if (errno == EINTR) continue; - err(EX_IOERR, "poll"); - } - - if (fds[0].revents == POLLIN) { + while (0 < poll(fds, 2, -1)) { + if (fds[0].revents & POLLIN) { ssize_t readSize = read(fd.input, buf, sizeof(buf)); if (readSize < 0) err(EX_IOERR, "read(%d)", fd.input); @@ -109,7 +101,7 @@ int main(int argc, char *argv[]) { if (writeSize < readSize) errx(EX_IOERR, "short write(%d)", fd.pty); } - if (fds[1].revents == POLLIN) { + if (fds[1].revents & POLLIN) { ssize_t readSize = read(fd.pty, buf, sizeof(buf)); if (readSize < 0) err(EX_IOERR, "read(%d)", fd.pty); @@ -122,35 +114,24 @@ int main(int argc, char *argv[]) { if (writeSize < readSize) err(EX_IOERR, "short write(%d)", fd.remote); if ((totalSize += readSize) >= 1024 * 1024) { - struct winsize original = window; - window.ws_row = 1; - window.ws_col = 1; + struct winsize redraw = window; + redraw.ws_row = 1; + redraw.ws_col = 1; - int error = ioctl(fd.pty, TIOCSWINSZ, &window); + error = ioctl(fd.pty, TIOCSWINSZ, &redraw); if (error) err(EX_IOERR, "TIOCSWINSZ"); - window = original; error = ioctl(fd.pty, TIOCSWINSZ, &window); - if (error) err(EX_IOERR, "TIOCWINSZ"); + if (error) err(EX_IOERR, "TIOCSWINSZ"); totalSize = 0; } } - if (fds[2].revents == POLLIN) { - ssize_t readSize = read(fd.winch, &window, sizeof(window)); - if (readSize < 0) err(EX_IOERR, "read(%d)", fd.winch); - if ((size_t)readSize < sizeof(window)) { - errx(EX_IOERR, "short read(%d)", fd.winch); - } - - int error = ioctl(fd.pty, TIOCSWINSZ, &window); - if (error) err(EX_IOERR, "TIOCSWINSZ"); - } - int status; pid_t dead = waitpid(pid, &status, WNOHANG); if (dead < 0) err(EX_OSERR, "waitpid(%d)", pid); if (dead) return WIFEXITED(status) ? WEXITSTATUS(status) : EX_SOFTWARE; } + err(EX_IOERR, "poll"); } diff --git a/ssh-command.sh b/ssh-command.sh index 84bc350..4433717 100644 --- a/ssh-command.sh +++ b/ssh-command.sh @@ -33,7 +33,7 @@ start() { echo " git clone $GIT_URL" echo " cd stream" echo " make ptee" - echo " ./ptee | ssh -t $SSH_URL ingest $id" + echo " ./ptee | ssh $SSH_URL ingest $id" echo } diff --git a/view.c b/view.c index 47c6c5b..b8f8793 100644 --- a/view.c +++ b/view.c @@ -19,6 +19,7 @@ #include <err.h> #include <errno.h> #include <fcntl.h> +#include <signal.h> #include <stdlib.h> #include <sys/event.h> #include <sys/ioctl.h> @@ -26,14 +27,13 @@ #include <termios.h> #include <unistd.h> -extern int winch(void); +static void nop() { } static struct { - int winch; int remote; int local; int input; -} fd = { -1, -1, STDOUT_FILENO, STDIN_FILENO }; +} fd = { -1, STDOUT_FILENO, STDIN_FILENO }; static struct termios saveTerm; static void restoreTerm(void) { @@ -59,26 +59,26 @@ int main(int argc, char *argv[]) { error = tcsetattr(fd.input, TCSADRAIN, &term); if (error) err(EX_IOERR, "tcsetattr"); - struct winsize localWindow; - error = ioctl(fd.local, TIOCGWINSZ, &localWindow); - if (error) err(EX_IOERR, "TIOCGWINSZ"); - fd.winch = winch(); + signal(SIGWINCH, nop); int kq = kqueue(); if (kq < 0) err(EX_OSERR, "kqueue"); - struct kevent events[3] = { + struct kevent events[2] = { { .ident = fd.input, .filter = EVFILT_READ, .flags = EV_ADD }, { .ident = fd.remote, .filter = EVFILT_READ, .flags = EV_ADD }, - { .ident = fd.winch, .filter = EVFILT_READ, .flags = EV_ADD }, }; - int nevents = kevent(kq, events, 3, NULL, 0, NULL); + int nevents = kevent(kq, events, 2, NULL, 0, NULL); if (nevents < 0) err(EX_OSERR, "kevent"); for (;;) { off_t off = lseek(fd.remote, 0, SEEK_SET); if (off < 0) err(EX_IOERR, "%s", path); + struct winsize localWindow; + error = ioctl(fd.local, TIOCGWINSZ, &localWindow); + if (error) err(EX_IOERR, "TIOCGWINSZ"); + // FIXME: Hack spin waiting for remote window. struct winsize remoteWindow; ssize_t size; @@ -100,12 +100,9 @@ int main(int argc, char *argv[]) { localWindow.ws_col, localWindow.ws_row, remoteWindow.ws_col, remoteWindow.ws_row ); - - ssize_t size = read(fd.winch, &localWindow, sizeof(localWindow)); - if (size < 0) err(EX_IOERR, "read(%d)", fd.winch); - if ((size_t)size < sizeof(localWindow)) { - errx(EX_IOERR, "short read(%d)", fd.winch); - } + sigset_t mask; + sigemptyset(&mask); + sigsuspend(&mask); continue; } @@ -114,10 +111,9 @@ int main(int argc, char *argv[]) { struct kevent event; int nevents = kevent(kq, NULL, 0, &event, 1, NULL); if (nevents < 0) { - if (errno == EINTR) continue; + if (errno == EINTR) break; err(EX_OSERR, "kevent"); } - if (!nevents) continue; if (event.ident == (uintptr_t)fd.input) { ssize_t size = read(fd.input, buf, sizeof(buf)); @@ -135,15 +131,6 @@ int main(int argc, char *argv[]) { if (writeSize < 0) err(EX_IOERR, "write(%d)", fd.local); if (writeSize < readSize) errx(EX_IOERR, "short write(%d)", fd.local); } - - if (event.ident == (uintptr_t)fd.winch) { - ssize_t size = read(fd.winch, &localWindow, sizeof(localWindow)); - if (size < 0) err(EX_IOERR, "read(%d)", fd.winch); - if ((size_t)size < sizeof(localWindow)) { - errx(EX_IOERR, "short read(%d)", fd.winch); - } - break; - } } } } |