Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to figure out whether a Linux TTY is controlling a process group

Tags:

c

linux

process

tty

So I have a tty (let's say /dev/tty5) and want to know whether it currently is a controlling tty of a process group or session, or whether it is currently unowned. POSIX has two API functions which suggest themselves here: tcgetpgrp() and tcgetsid(), both of which only work however if the caller has the tty as controlling tty -- which in this case makes them mostly useless (and in fact I don't see the point of tcgetsid() at all).

Anybody has a suggestion how I can detect in a sane way, from C, whether a terminal is currently a controlling terminal of a process? I only care about Linux, so if Linux-specific APIs are necessary that is fine with me.

like image 683
user175104 Avatar asked Nov 14 '22 09:11

user175104


1 Answers

BSD: int ioctl(int tty, TIOCGETPGRP, int *foreground_group);

Linux: int tcgetpgrp(int tty, int *foreground_group);

Linux works only if you permissions to the non-owned terminal, i.e., you are root. This is an intentional security implemenation. BSD ioctl() allows any tty to take on any process group (or even nonexistant process groups) as its foreground tty. POSIX allows access only to process groups which have the tty as their controlling tty. This limitation disallows some ambiguous and security-undermining cases present in BSD ioctl.

What are you trying to do? You should only be worrying about process-controlling tty's if you are the kernel delivering signals.

Edit: I forgot /proc
From www.die.net: /proc/[number]/fd This is a subdirectory containing one entry for each file which the process has open, named by its file descriptor, and which is a symbolic link to the actual file. Thus, 0 is standard input, 1 standard output, 2 standard error, etc.

like image 143
jim mcnamara Avatar answered Feb 06 '23 04:02

jim mcnamara