eudoxos eudoxos - 1 year ago 130
Linux Question

Restoring terminal settings (termios) in signal handler

In a code which changes terminal settings (it embeds python, which in turn uses some terminal manipulating modules), the changed state persists after I quit via Ctrl-\ sending SIGQUIT to the process.

Is it safe to save terminal settings at startup, and then register SIGQUIT handler which will restore those? If not safe, what can happen at the worst?

Answer Source

Signal handlers usually should be lean and not do much work. You shouldn't be calling non asynchronous-safe functions from signal-handler. Async-signal-safe functions. So, if your termios code contains non AS functions, than restoration of terminal settings from signal-handler is not recommended ( Though you will be handling SIGQUIT)

How about this:

fork() is listed as AS safe in the man page of signal. Why not fork a child process, execv another program which will restore the settings?