1 How can a process get the file descriptor of the terminal it is using?
2 It cannot. But it can cheat.
4 I assumed it was STDIN_FILENO, or STDOUT_FILENO, or STDERR_FILENO.
5 I assumed all three were equally good.
7 because stdin, stdout, and stderr can all be redirected,
8 and then they do not refer to the terminal anymore.
10 The current terminal has a filename: “/dev/tty”.
11 As any other file, it can be opened with open(),
12 which returns a new file descriptor, not in use before.
13 This can be used as the file descriptor of the terminal.
14 And then, when it is no longer needed, it can be closed with close().
16 I said “cheating” because this method does not get THE file descriptor,
17 but another file descriptor that refer to the same file.
19 Yesterday, minish, in script mode, terminated with SIGINT
20 when it received a SIGINT
21 and the process it was executing terminated with SIGINT.
22 bash does it this way, too.
23 Today, it ignores a received SIGINT
24 and terminates when the process it is executing terminates with SIGINT.
25 fish does it this way, too.
26 It is simpler and I cannot see any downsides.
27 When pressing Ctrl+C, the behavior is identical,
28 because the shell receives the SIGINT, too.
29 When sending SIGINT to a specific process that is part of a script,
30 for example with the command kill,
31 if the signal terminates the process,
32 the first strategy goes on with the script,
33 but the second aborts it.
34 But I do not really care: SIGINT is to be sent with Ctrl+C.
35 If you want to use kill or anything else, send SIGTERM instead.
40 https://gnu.org/software/libc/manual/html_node/Termination-Signals.html