Find controlling terminal with ttyname


You can identify the path the the controlling terminal device path for your current process in C with the ttyname() system call. This controlling terminal can be a virtual console (/dev/ttyn) or a pseudo-terminal (/dev/pts/n).

The ttyname system call takes the following format.

char *ttyname(int fd);

The stdin, stdout, stderr file descriptors unless redirected are by default mapped to the controlling terminal. So for example you can pass it the stdin file descriptor.

  • ausec

    if stdin/stdout/stderr are all re-directed to /dev/null, how to find which one is the process’ controlling tty?