Search code examples
clinuxbashsignalsctrl

What is the behavior of each ctrl + key and its signals?


Given a list of signals (kill -l can give you), I want to know all the ctrl + key that can raise one of them. For example, the ctrl+c raises SIGINT when running on terminal a foreground process. Is it possible ? Where is the ctrl+key mapped on system ? If has a C function that shows me it is better.


Solution

  • The description of Linux terminal input handling is found in man tcsetattr, which is the function you would use to configure the mapping for cooked input. (Look for the c_cc array for the list of special characters.) The Linux terminal is based on the Posix standard but it has a number of extensions. (The list of special characters is in §11.1.9 of Posix, but you should also read the preceding sections for a complete understanding of the terminal programming model.)

    There are three signals which the terminal driver will send in response to control characters:

    • SIGINT (VINTR, default Ctrl-C)

    • SIGQUIT (VQUIT, default Ctrl-\)

    • SIGTSTP (VSUSP, default Ctrl-Z)

    If you set the terminal to raw mode (with the same function) then you will have to issue the signals yourself (if you want to) and you can use whatever mechanism you choose to decide what triggers each signal.

    The readline library (used by many shells) puts the terminal into raw mode while it is reading, then does its own character mapping [Note 2]. However, when bash hands the terminal over to a command application, the terminal is restored to default mode [Note 1], so the mapping is controlled by the terminal driver as explained in the tcsetattr documention.

    If you want to use readline, you will need to consult its documentation.

    Most applications using ncurses also start by putting the terminal into raw mode.


    Notes

    1. This is actually a bit imprecise, since commands can modify the terminal mode. (See man stty, for example.) If some command changes a control character, that becomes part of the mode which will be passed to the next command.

    2. readline doesn't turn off VINTR handling, so SIGINT can be sent even if readline is being used; this will obey the current terminal setting. But it does turn off the other control characters.