From 668574b26474d7f4774e9c8924e6a3d5a4f5cec1 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 5 Oct 2015 23:15:31 +0000 Subject: [PATCH] Set the line file descriptor nonblocking and make it blocking again for xmodem and child processes, makes xmodem work with -d. Reported by Kim Zeitler via guenther@, tested by Jiri B. ok (and a small change) guenther --- usr.bin/cu/command.c | 6 +++++- usr.bin/cu/cu.c | 20 ++++++++++++++++++-- usr.bin/cu/cu.h | 3 ++- usr.bin/cu/xmodem.c | 6 ++++-- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/usr.bin/cu/command.c b/usr.bin/cu/command.c index cb4352ee57d..9ae8e9adf9c 100644 --- a/usr.bin/cu/command.c +++ b/usr.bin/cu/command.c @@ -1,4 +1,4 @@ -/* $OpenBSD: command.c,v 1.14 2015/10/05 17:53:56 nicm Exp $ */ +/* $OpenBSD: command.c,v 1.15 2015/10/05 23:15:31 nicm Exp $ */ /* * Copyright (c) 2012 Nicholas Marriott @@ -51,6 +51,7 @@ pipe_command(void) return; restore_termios(); + set_blocking(line_fd, 1); switch (pid = fork()) { case -1: @@ -81,6 +82,7 @@ pipe_command(void) break; } + set_blocking(line_fd, 0); set_termios(); } @@ -102,6 +104,7 @@ connect_command(void) return; restore_termios(); + set_blocking(line_fd, 1); switch (pid = fork()) { case -1: @@ -129,6 +132,7 @@ connect_command(void) break; } + set_blocking(line_fd, 0); set_termios(); } diff --git a/usr.bin/cu/cu.c b/usr.bin/cu/cu.c index ac8ddae2d7a..8bb812a169a 100644 --- a/usr.bin/cu/cu.c +++ b/usr.bin/cu/cu.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cu.c,v 1.22 2015/05/18 09:35:05 nicm Exp $ */ +/* $OpenBSD: cu.c,v 1.23 2015/10/05 23:15:31 nicm Exp $ */ /* * Copyright (c) 2012 Nicholas Marriott @@ -186,6 +186,7 @@ main(int argc, char **argv) NULL); bufferevent_enable(output_ev, EV_WRITE); + set_blocking(line_fd, 0); line_ev = bufferevent_new(line_fd, line_read, NULL, line_error, NULL); bufferevent_enable(line_ev, EV_READ|EV_WRITE); @@ -208,6 +209,21 @@ signal_event(int fd, short events, void *data) exit(0); } +void +set_blocking(int fd, int state) +{ + int mode; + + state = state ? 0 : O_NONBLOCK; + if ((mode = fcntl(fd, F_GETFL)) == -1) + cu_err(1, "fcntl"); + if ((mode & O_NONBLOCK) != state) { + mode = (mode & ~O_NONBLOCK) | state; + if (fcntl(fd, F_SETFL, mode) == -1) + cu_err(1, "fcntl"); + } +} + void set_termios(void) { @@ -342,7 +358,7 @@ try_remote(const char *host, const char *path, const char *entry) if (entry != NULL && cgetset(entry) != 0) cu_errx(1, "cgetset failed"); - error = cgetent(&cp, (char**)paths, (char*)host); + error = cgetent(&cp, (char **)paths, (char *)host); if (error < 0) { switch (error) { case -1: diff --git a/usr.bin/cu/cu.h b/usr.bin/cu/cu.h index 70510d61c42..10745bc037a 100644 --- a/usr.bin/cu/cu.h +++ b/usr.bin/cu/cu.h @@ -1,4 +1,4 @@ -/* $OpenBSD: cu.h,v 1.6 2012/07/10 12:47:23 nicm Exp $ */ +/* $OpenBSD: cu.h,v 1.7 2015/10/05 23:15:31 nicm Exp $ */ /* * Copyright (c) 2012 Nicholas Marriott @@ -27,6 +27,7 @@ extern FILE *record_file; extern struct termios saved_tio; extern int line_fd; extern struct bufferevent *line_ev; +void set_blocking(int, int); int set_line(int); void set_termios(void); void restore_termios(void); diff --git a/usr.bin/cu/xmodem.c b/usr.bin/cu/xmodem.c index a542e6f457b..09f3304c5da 100644 --- a/usr.bin/cu/xmodem.c +++ b/usr.bin/cu/xmodem.c @@ -1,4 +1,4 @@ -/* $OpenBSD: xmodem.c,v 1.7 2014/09/21 05:29:47 daniel Exp $ */ +/* $OpenBSD: xmodem.c,v 1.8 2015/10/05 23:15:31 nicm Exp $ */ /* * Copyright (c) 2012 Nicholas Marriott @@ -137,8 +137,9 @@ xmodem_send(const char *file) if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio) != 0) cu_err(1, "tcsetattr"); } - + set_blocking(line_fd, 1); tcflush(line_fd, TCIFLUSH); + if (xmodem_read(&c) != 0) goto fail; if (c == XMODEM_C) @@ -214,6 +215,7 @@ fail: cu_warn("%s", file); out: + set_blocking(line_fd, 0); set_termios(); sigaction(SIGINT, &oact, NULL); -- 2.20.1