Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In detail, what happens when you press Ctrl-C in a terminal?

In detail, what happens when you press Ctrl-C in a terminal? Yes, I know that it sends SIGINT, but what steps does it take to get there?

I have done some research so I think I understand most of the picture, but not all of it.

For the sake of pedagogy, I will assume we are running a terminal emulator, xterm, in an X session. The terminal is running the Bash shell, and the shell is currently running some long running pipeline consisting of multiple processes in the foreground.

  1. I press Ctrl-C in the keyboard.
  2. X sends the keyboard event to xterm.
  3. xterm translates the Ctrl-C keyboard event and sends it to the pseudo-tty master file descriptor it is holding? (Some magic happens)
  4. The kernel detects that some special SIGINT event happens on the pseudo-tty, and finds the session whose controlling terminal is this tty. It sends SIGINT to the current foreground process group of that session, which includes only the processes in our pipeline.

My question is, is my understanding so far correct, and how exactly does xterm tell the kernel to send SIGINT to the session with a given controlling terminal?

like image 266
darkfeline Avatar asked Sep 01 '17 04:09

darkfeline


People also ask

What does Ctrl-C do in a terminal?

While in a command line such as MS-DOS, Linux, and Unix, Ctrl + C is used to send a SIGINT signal, which cancels or terminates the currently-running program. For example, if a script or program is frozen or stuck in an infinite loop, pressing Ctrl + C cancels that command and returns you to the command line.

When you press Ctrl-C on Linux terminal which signal is produced?

When Ctrl+C is pressed, SIGINT signal is generated, we can catch this signal and run our defined signal handler. C standard defines following 6 signals in signal.

What is C in terminal mean?

The C-terminus (also known as the carboxyl-terminus, carboxy-terminus, C-terminal tail, C-terminal end, or COOH-terminus) is the end of an amino acid chain (protein or polypeptide), terminated by a free carboxyl group (-COOH).

Which signal is sent when you press Ctrl-C?

The SIGINT signal is sent to a process by its controlling terminal when a user wishes to interrupt the process. This is typically initiated by pressing Ctrl + C , but on some systems, the "delete" character or "break" key can be used. The SIGKILL signal is sent to a process to cause it to terminate immediately (kill).


1 Answers

tl;dr the kernel does it.

Each pty (pseudo tty) has two ends, a master and a slave. In the xterm example, xterm would be holding onto the master file descriptor. Any key presses are written directly into the master fd. The slave fd (pts, or pty slave) is owned by a session and passed to whatever the foreground process group is.

Whenever an ASCII ETX character (^C) is written to the master, the kernel translates that into sending SIGINT to the foreground process group with the corresponding controlling terminal. This is actually a pty setting. You can run stty -a and see that the default is intr = ^C;, meaning ^C or ETX is the "SIGINT" character. This can be changed to a different character or disabled entirely.

A more complex example would be how Ctrl-C works through an interactive SSH session. Interactive SSH sessions allocate a pty on the server side. The client side pty is set to raw mode, meaning that the client side kernel will not translate ETX into SIGINT. Instead, the client side kernel passes the ETX along to the slave. In this case, the ssh client process takes that ETX and passes it along to the server sshd process. If the server sshd pty is not in raw mode, then the server's kernel will translate that ETX into a SIGINT to its foreground process group. This is how Ctrl-C sends SIGINT to the process running on the server instead of killing your client side SSH and leaving you hanging.

like image 100
darkfeline Avatar answered Oct 06 '22 08:10

darkfeline