-
Notifications
You must be signed in to change notification settings - Fork 24
microcom.c: Call cleanup routines only once #56
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,12 @@ struct ios_ops *ios; | |
| int debug = 0; | ||
| int quiet = 0; | ||
|
|
||
| int opt_force = 0; | ||
| unsigned long current_speed = DEFAULT_BAUDRATE; | ||
| int current_flow = FLOW_NONE; | ||
| int listenonly = 0; | ||
| char escape_char = DEFAULT_ESCAPE_CHAR; | ||
|
|
||
| void init_terminal(void) | ||
| { | ||
| struct termios sts; | ||
|
|
@@ -47,17 +53,27 @@ void restore_terminal(void) | |
| } | ||
|
|
||
|
|
||
| void microcom_exit(int signal) | ||
| static void microcom_exit(int signal) | ||
| { | ||
| write(1, "exiting\n", 8); | ||
| static const char exit_msg[] = "exiting\n"; | ||
|
|
||
| if (write(STDOUT_FILENO, exit_msg, sizeof(exit_msg) - 1) < 0) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this to avoid a warning about ignoring the return value? or why is this here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, for compiler and reader to see that error checking is omitted purposely. I thought that it looked better this way but I'll change it given you find that more idiomatic. |
||
| /* couldn't write message but already exiting */ | ||
| } | ||
|
|
||
| ios->exit(ios); | ||
| tcsetattr(STDIN_FILENO, TCSANOW, &sots); | ||
| if (!listenonly) | ||
| tcsetattr(STDIN_FILENO, TCSANOW, &sots); | ||
|
|
||
| if (signal) | ||
| _Exit(0); | ||
| } | ||
|
|
||
| static void atexit_cleanup(void) | ||
| { | ||
| microcom_exit(0); | ||
| } | ||
|
|
||
| /* | ||
| * Main functions | ||
| ******************************************************************** | ||
|
|
@@ -98,12 +114,6 @@ void main_usage(int exitcode, char *str, char *dev) | |
| exit(exitcode); | ||
| } | ||
|
|
||
| int opt_force = 0; | ||
| unsigned long current_speed = DEFAULT_BAUDRATE; | ||
| int current_flow = FLOW_NONE; | ||
| int listenonly = 0; | ||
| char escape_char = DEFAULT_ESCAPE_CHAR; | ||
|
|
||
| int main(int argc, char *argv[]) | ||
| { | ||
| struct sigaction sact = { 0 }; /* used to initialize the signal handler */ | ||
|
|
@@ -216,7 +226,7 @@ int main(int argc, char *argv[]) | |
|
|
||
| ret = ios->set_speed(ios, current_speed); | ||
| if (ret) | ||
| goto cleanup_ios; | ||
| exit(1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is before |
||
|
|
||
| current_flow = FLOW_NONE; | ||
| ios->set_flow(ios, current_flow); | ||
|
|
@@ -226,27 +236,23 @@ int main(int argc, char *argv[]) | |
| msg_printf("Type the escape character to get to the prompt.\n"); | ||
|
|
||
| /* Now deal with the local terminal side */ | ||
| /* microcom_exit will restore the old termios handler */ | ||
| tcgetattr(STDIN_FILENO, &sots); | ||
| init_terminal(); | ||
|
|
||
| /* set the signal handler to restore the old | ||
| * termios handler */ | ||
| sact.sa_handler = µcom_exit; | ||
| sigaction(SIGHUP, &sact, NULL); | ||
| sigaction(SIGINT, &sact, NULL); | ||
| sigaction(SIGPIPE, &sact, NULL); | ||
| sigaction(SIGTERM, &sact, NULL); | ||
| sigaction(SIGQUIT, &sact, NULL); | ||
| } | ||
|
|
||
| /* run the main program loop */ | ||
| ret = mux_loop(ios); | ||
| sact.sa_handler = µcom_exit; | ||
|
|
||
| if (!listenonly) | ||
| tcsetattr(STDIN_FILENO, TCSANOW, &sots); | ||
| sigaction(SIGHUP, &sact, NULL); | ||
| sigaction(SIGINT, &sact, NULL); | ||
| sigaction(SIGPIPE, &sact, NULL); | ||
| sigaction(SIGTERM, &sact, NULL); | ||
| sigaction(SIGQUIT, &sact, NULL); | ||
|
|
||
| cleanup_ios: | ||
| ios->exit(ios); | ||
| atexit(atexit_cleanup); | ||
|
|
||
| /* run the main program loop */ | ||
| ret = mux_loop(ios); | ||
|
|
||
| exit(ret ? 1 : 0); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.