Skip to content

Commit

Permalink
#416 apply STDIN restore to all programs
Browse files Browse the repository at this point in the history
  • Loading branch information
fklassen committed Jan 22, 2018
1 parent d0b0e3f commit cc7ae9f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 11 deletions.
22 changes: 22 additions & 0 deletions src/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include <ctype.h>
#include <unistd.h>

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif

#ifdef DEBUG
extern int debug;
#endif
Expand Down Expand Up @@ -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 */


}
1 change: 1 addition & 0 deletions src/common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
1 change: 1 addition & 0 deletions src/tcpbridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ main(int argc, char *argv[])
tcpdump_close(options.tcpdump);
#endif

restore_stdin();
return 0;
}

Expand Down
6 changes: 2 additions & 4 deletions src/tcpcapinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,11 @@ main(int argc, char *argv[])
} else if (backwards && caplentoobig) {
printf("BAD_TS|TOOBIG");
}

}

}

exit(0);

restore_stdin();
return 0;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/tcpliveplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ main(int argc, char **argv)
printf("----------------------------------------------------------\n\n");

free(sched);
restore_stdin();
return 0;
}
/*end of main() function*/
Expand Down
3 changes: 2 additions & 1 deletion src/tcpprep.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ main(int argc, char *argv[])

/* close cache file */
close(out_file);
return 0;

restore_stdin();
return 0;
}


Expand Down
1 change: 1 addition & 0 deletions src/tcpreplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ main(int argc, char *argv[])
}
}
tcpreplay_close(ctx);
restore_stdin();
return 0;
} /* main() */

Expand Down
7 changes: 1 addition & 6 deletions src/tcprewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit cc7ae9f

Please sign in to comment.