7

If I run a program in Bash that listens for SIGWINCH, and I resize the terminal that Bash is running in, then the program will receive SIGWINCH. I would like to know how this signal gets relayed to the program running under Bash.

Here is my understanding of what happens, using a sample catch_sig program that I list at the end of this post:

  • The terminal emulator will run Bash behind the receiving end of a PTY. Thus, the "STDIN" and "STDOUT" of Bash will be TTYs.
  • Bash runs catch_sig as a sub-process by forking. The catch_sig process inherits Bash's FDs for I/O, which are the TTYs mentioned above.
  • When the size of the terminal emulator changes it should call ioctl(pty_fd, TIOCSWINSZ, &size), where pty_fd is the sending end of the above PTY. This call to ioctl will update the size of the receiving TTY and will attempt to send SIGWINCH the process group for the TTY.
  • Bash and catch_sig are part of the above process group, so SIGWINCH gets sent to both of them individually.

One difficulty I'm having with the above however, is that if I attempt to send SIGWINCH to the process group for Bash manually with kill then catch_sig doesn't receive the signal. For example, if the PID (and process group) for Bash is 123 and I run catch_sig in it, and then I run kill -WINCH -123 in a separate pane, then catch_sig doesn't receive the signal. Why is this this the case?

The following is source code for a demonstrative catch_sig program, as mentioned above:

#include <signal.h>
#include <stdio.h>
#include <unistd.h>

static void sigwinch_handler(int sig) {
        printf("got signal: %d\n", sig);
}

int main() {
        signal(SIGWINCH, sigwinch_handler);
        printf("waiting for signal...\n");
        pause();
        return 0;
}

1 Answer 1

7

For example, if the PID (and process group) for Bash is 123 and I run catch_sig in it, and then I run kill -WINCH -123 in a separate pane, then catch_sig doesn't receive the signal. Why is this this the case?

The terminal sends SIGWINCH to the foreground (i.e controlling) process group only. When Bash is interactive, each job —foreground or background— is placed in a separate process group, and foreground jobs are temporarily given control of the terminal. For example:

Terminal #1:

$ tty
/dev/pts/0
$ echo $$
123
$ ./catch_sig
waiting for signal...

Terminal #2:

$ ps -t /dev/pts/0 -o pid,pgid,stat,comm
  PID  PGID STAT COMMAND
  123   123 S<s  bash
  124   124 S<+  catch_sig

The plus sign (+) in the STAT field means that the process is a member of the foreground process group, see ps(1).

So, when you resize the terminal window while catch_sig is running in the foreground (i.e. its process group was made the foreground process group by Bash, and it hasn't completed yet), SIGWINCH is sent to catch_sig's process group only. Then Bash reattains the control of the terminal as catch_sig terminates.

Which can be reproduced using kill as shown below.

Terminal #2:

$ kill -WINCH -124

Terminal #1:

got signal: 28

For further information, see how job control is implemented in POSIX shells.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.