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.