help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Restoring tty line settings after a trap


From: Bob Proulx
Subject: Re: [Help-bash] Restoring tty line settings after a trap
Date: Thu, 19 Jun 2014 17:52:56 -0600
User-agent: Mutt/1.5.23 (2014-03-12)

Greg Wooledge wrote:
> Jesse Molina wrote:
> > I have an issue with a bash script where if the user calls SIGINT via a 
> > ctrl+c, my trap gets called and the tty line settings don't get restored 
> > like they should, because they had been changed before trap was called, 
> > but not restored.
> ...
> > Is there are a "better" way to do this, or is doing an stty save and 
> > restore my best bet?
> 
> Using stty to reset the terminal to a sane state is probably the best
> approach.

If this is your own script for your own purposes then that is fine.
You can do anything you want in your own house.  But if this is for
general consumption then please don't put stty settings into scripts
for other people to use.  I am sure nothing good would come of it.

Since Chet just posted a fix to bash for this problem then assuming
that resolves it I will encourage that fix to avoid the problem rather
than propagating stty settings in trap handlers in the userland
culture.  Because I would definitely be very annoyed if I ran across a
random script that upon Control-C reset my terminal settings!  That
would be bad.  I would want to get an angry mob out with dogs and
torches to find the culprit! :-)

It would be reasonable to save the stty settings when the user started
the script and then restore them.  That would be acceptable.  Because
then nothing would be changed for the user.

  savedtty=$(stty -g)
  cleanup() {
      stty "$savedtty"
  }
  trap 'cleanup' EXIT

Something like that should be fine.

> If you don't need to handle each signal specially, then you
> can just do:
> ...
> trap 'cleanup' EXIT

For bash that works for everything.  For non-bash shells such as dash
then dash needs additional userland code to handle things properly.

> However, if you explicitly catch SIGINT, then your script should
> kill itself with SIGINT, not just exit, because SIGINT is special.
> See http://www.cons.org/cracauer/sigint.html for details.

There isn't anything special about SIGINT.  All of the signals are
special.  :-)

> In that case, something like this may be required:

Since this is "help-bash" I would squeeze people to simply use bash at
this point and then stop with the above.  Done.  However if they want
to use the standard #!/bin/sh then that is possibly dash on some
systems and then it needs to kill itself properly.  I really wish dash
handled that nicer.  But it doesn't.  Sigh.

> cleanup() {
>     # remove temp files and so on
>     stty sane
> }
> trap 'cleanup; trap - INT; kill -INT $$' INT

Good.

> trap 'rc=$?; cleanup; exit $rc' HUP TERM

Unfortunately that drops the proper return code.  Instead it should
kill itself properly for those signals too.

  trap "cleanup; trap - HUP; kill -HUP $$" HUP TERM
  trap "cleanup; trap - TERM; kill -TERM $$" TERM

And again that is only needed for dash as far as I can see.  All of
the other shells handle "trap 'cleanup' EXIT" nicely for signals
including bash and the above isn't needed.  Did last time I tested
those other shells anyway.

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]