Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit fixes #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions linux-serial-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <linux/serial.h>
#include <errno.h>
#include <sys/file.h>
#include <signal.h>

/*
* glibc for MIPS has its own bits/termios.h which does not define
Expand Down Expand Up @@ -83,8 +84,22 @@ long long int _write_count = 0;
long long int _read_count = 0;
long long int _error_count = 0;

volatile sig_atomic_t sigint_received = 0;
void sigint_handler(int s)
{
sigint_received += 1;
if (sigint_received > 3) {
exit(-1);
}
}

static void exit_handler(void)
{
//FIXME This part is NEVER reached when stopping an endless run (i.e. no -r, no -o set) with SIGINT ?
printf("Exit handler: Cleaning up ...\n");
// set_modem_lines(_fd, 0, TIOCM_LOOP); // TODO if do it here, move def!!
tcflush(_fd, TCIOFLUSH);

if (_fd >= 0) {
flock(_fd, LOCK_UN);
close(_fd);
Expand Down Expand Up @@ -665,7 +680,6 @@ static void setup_serial_port(int baud)
} else {
if (rs485.flags & SER_RS485_ENABLED) {
printf("RS485 already enabled on port, ignoring delays if set\n");
//FIXME when running with --rs485 it does not send?!
} else {
if (_cl_rs485_after_delay >= 0) {
/* enable RS485 */
Expand Down Expand Up @@ -723,7 +737,9 @@ int main(int argc, char * argv[])
{
printf("Linux serial test app\n");

atexit(&exit_handler);
signal(SIGINT, sigint_handler);
signal(SIGTERM, sigint_handler);
atexit(&exit_handler); //FIXME seems not to work for SIGINT without further additions like the previous signal handler

process_options(argc, argv);

Expand Down Expand Up @@ -822,7 +838,7 @@ int main(int argc, char * argv[])
if (_cl_tx_wait)
serial_poll.events &= ~POLLOUT;

while (!(_cl_no_rx && _cl_no_tx)) {
while (!(_cl_no_rx && _cl_no_tx) && !sigint_received ) {
struct timespec current;
int retval = poll(&serial_poll, 1, 1000);

Expand Down Expand Up @@ -942,10 +958,12 @@ int main(int argc, char * argv[])
}
}

//FIXME rs485 RTS not reset on SIGINT as this part is also NEVER reached (like exit_handler)?!
printf("Terminating ...\n");
tcdrain(_fd);
dump_serial_port_stats();
set_modem_lines(_fd, 0, TIOCM_LOOP);
tcflush(_fd, TCIOFLUSH);
set_modem_lines(_fd, 0, TIOCM_LOOP); //TODO undecided
// tcflush(_fd, TCIOFLUSH); //move to exit_handler

return compute_error_count();
}