diff --git a/src/common/utils.c b/src/common/utils.c index a280e2a61..7076820e7 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -28,6 +28,10 @@ #include #include +#ifdef HAVE_SYS_IOCTL_H +#include +#endif + #ifdef DEBUG extern int debug; #endif @@ -335,3 +339,21 @@ int tcpr_random(uint32_t *seed) return result; } + +/** + * #416 - Ensure STDIN is not left in non-blocking mode after closing + * a program. BSD and Unix derivatives should utilize `FIONBIO` due to known + * issues with reading from tty with a 0 byte read returning -1 opposed to 0. + */ +void restore_stdin(void) +{ +#ifdef FIONBIO + int nb = 0; + + ioctl(0, FIONBIO, &nb); +#else + fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK); +#endif /* FIONBIO */ + + +} diff --git a/src/common/utils.h b/src/common/utils.h index e9c6828bf..db066c34b 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -48,6 +48,7 @@ int read_hexstring(const char *l2string, u_char *hex, const int hexlen); void packet_stats(const tcpreplay_stats_t *stats); int format_date_time(struct timeval *when, char *buf, size_t len); int tcpr_random(uint32_t *seed); +void restore_stdin(void); /* our "safe" implimentations of functions which allocate memory */ #define safe_malloc(x) _our_safe_malloc(x, __FUNCTION__, __LINE__, __FILE__) diff --git a/src/tcpbridge.c b/src/tcpbridge.c index 7d1de8129..41ada589e 100644 --- a/src/tcpbridge.c +++ b/src/tcpbridge.c @@ -114,6 +114,7 @@ main(int argc, char *argv[]) tcpdump_close(options.tcpdump); #endif + restore_stdin(); return 0; } diff --git a/src/tcpcapinfo.c b/src/tcpcapinfo.c index 8cb08a2a9..86ba4d56e 100644 --- a/src/tcpcapinfo.c +++ b/src/tcpcapinfo.c @@ -332,13 +332,11 @@ main(int argc, char *argv[]) } else if (backwards && caplentoobig) { printf("BAD_TS|TOOBIG"); } - } - } - exit(0); - + restore_stdin(); + return 0; } /** diff --git a/src/tcpliveplay.c b/src/tcpliveplay.c index 4763f885e..c84dc15cb 100644 --- a/src/tcpliveplay.c +++ b/src/tcpliveplay.c @@ -383,6 +383,7 @@ main(int argc, char **argv) printf("----------------------------------------------------------\n\n"); free(sched); + restore_stdin(); return 0; } /*end of main() function*/ diff --git a/src/tcpprep.c b/src/tcpprep.c index 922775e1a..d42b9c604 100644 --- a/src/tcpprep.c +++ b/src/tcpprep.c @@ -193,8 +193,9 @@ main(int argc, char *argv[]) /* close cache file */ close(out_file); - return 0; + restore_stdin(); + return 0; } diff --git a/src/tcpreplay.c b/src/tcpreplay.c index 159ee80cc..a954526e2 100644 --- a/src/tcpreplay.c +++ b/src/tcpreplay.c @@ -157,6 +157,7 @@ main(int argc, char *argv[]) } } tcpreplay_close(ctx); + restore_stdin(); return 0; } /* main() */ diff --git a/src/tcprewrite.c b/src/tcprewrite.c index d18a6c85f..1494d6522 100644 --- a/src/tcprewrite.c +++ b/src/tcprewrite.c @@ -35,7 +35,6 @@ #include #include #include -#include #include #include @@ -67,11 +66,9 @@ main(int argc, char *argv[]) { int optct, rcode; pcap_t *dlt_pcap; - int nb = 0; #ifdef ENABLE_FRAGROUTE char ebuf[FRAGROUTE_ERRBUF_LEN]; #endif - tcprewrite_init(); /* call autoopts to process arguments */ @@ -153,9 +150,7 @@ main(int argc, char *argv[]) dmalloc_shutdown(); #endif - /* avoid making STDIN non-blocking */ - ioctl(0, FIONBIO, &nb); - + restore_stdin(); return 0; }