[Techtalk] Ctrl-C handling and stty

Miriam English mim at miriam-english.org
Mon Aug 25 20:45:43 UTC 2014


Really interesting problem. I looked on the net and found this at 
stackexchange, which might lead to it:


Processes can choose to:

    - ignore the SIGINT signal usually sent upon pressing Ctrl-C (like 
with trap '' INT in a shell) or have their own handler for it that 
decides not to terminate (or fails to terminate in a timely fashion).

    - tell the terminal device that the character that causes a SIGINT 
to be sent to the foreground job is something else (like with stty int 
'^K' in a shell)

    - tell the terminal device not to send any signal (like with stty 
-isig in a shell).

Or, they can be uninterruptible, like when in a middle of a system call 
that can't be interrupted.

On Linux (with a relatively recent kernel), you can tell if a process is 
ignoring and/or handling SIGINT by looking at the output of

   $ kill -l INT
   2
   $ grep Sig "/proc/$pid/status"
   SigQ:   0/63858
   SigPnd: 0000000000000000
   SigBlk: 0000000000000000
   SigIgn: 0000000000000002
   SigCgt: 0000000000000000

SIGINT is 2. The second bit of SigIgn above is 1, which mean SIGINT is 
ignored.

You can automate that with:

   $ SIG=$(kill -l INT) perl -lane 'print $1 if $F[0] =~ /^Sig(...):/ &&
       $F[1] & (1<<($ENV{SIG}-1))' < "/proc/$pid/status"
   Ign

To check what the current intr character is or if isig is enabled for a 
given terminal:

   $ stty -a < /dev/pts/0
   [...] intr = ^C [...] isig

(above the intr character is ^C (the character usually sent by your 
terminal (emulator) when pressing CTRL-C and input signals are not disabled.

   $ stty -a < /dev/pts/1
   [...] intr = ^K [...] -isig

(intr character is ^K and isig is disabled for /dev/pts/1).


On 26/08/14 03:14, Akkana Peck wrote:
> I have a weird problem I'm having trouble tracking down:
> something is messing with my terminal settings, specifically Ctrl-C.
> But I'm not sure what it is, because by the time I notice it
> (because I need to interrupt a program by hitting Ctrl-C, and
> it doesn't work so I have to open another window and use pkill),
> it's too late to figure out what caused it.
>
> Typing reset fixes the problem, but I want to find out why it's
> happening so I can prevent it.
>
> When it happens, it's always in a terminal where I've been doing
> a lot of three things: git, meld (which I use for git difftool),
> and ncftp (because I'm helping out with a website on a host that
> only supports ftp, not ssh).
>
> Being an old unix fogey, I assumed the difference was stty.
> stty used to have modes like raw, cooked and cbreak that affected
> whether characters like ^C worked.
> So I decided I'd set a precmd() in zsh to check my stty settings
> after every command, and alert me when they change, so I know what
> command is causing the problem.
>
> Only it looks like stty isn't actually the problem.
> Here's the complete stty -a from a terminal where ^C no longer works:
>
> speed 38400 baud; rows 32; columns 80; line = 0;
> intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
> eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
> werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
> -parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
> ignbrk -brkint ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -ixon -ixoff
> -iuclc -ixany -imaxbel iutf8
> opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
> -isig icanon -iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
> echoctl echoke
>
> If I compare that to a stty -a from a freshly opened xterm, the only
> differences are that the bad one has ignbrk ignpar -iexten -ixon, while
> the normal one has -ignbrk -ignpar iexten ixon. intr = ^C in both cases.
>
> So if I run stty -ignbrk -ignpar iexten ixon, the terminal should go
> back to normal, right? Nope: if I type that, ^C still doesn't work.
> And modes like raw, cooked and cbreak aren't there any more.
>
> So there's something else causing the terminal to ignore ^C, having
> nothing to do with stty, and I can't figure out what it is. Anyone
> know where I should be looking?
>
> (I'm going to set up that precmd to look at stty anyway -- because
> stty IS getting changed, even if it seems to make no difference --
> but it bugs me not to know what the real problem is.)
>
>          ...Akkana
> _______________________________________________
> Techtalk mailing list
> Techtalk at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/techtalk
>

-- 
If you don't have any failures then you're not trying hard enough.
  - Dr. Charles Elachi, director of NASA's Jet Propulsion Laboratory
-----
Website: http://miriam-english.org
Blogs:   http://miriam-e.dreamwidth.org
          http://miriam-e.livejournal.com




More information about the Techtalk mailing list