user175104
user175104

Reputation: 3696

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

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.

Upvotes: 6

Views: 4717

Answers (4)

agupta666
agupta666

Reputation: 269

you can use the proc file system to query the controlling tty of a process, if you know the PID of the process.

/proc//fd/0 is a symbolic link to the tty (say /dev/pts/4).

So all you need to do is create a proc path with the PID (eg: /proc/7834/fd/0, where 7834 is the PID) and then call the readlink system call to get the tty

See the C code snippet below

sprintf(procPath, "/proc/%s/fd/0", pid);
int ret = readlink(procPath, buffer, MAX_LEN);
buffer[ret] = '\0';

Upvotes: 0

Vanni Totaro
Vanni Totaro

Reputation: 5471

Not sure if this exactly catches your need, anyway here it is:

#include <stdlib.h>
#include <stdio.h>

int main()
{
  int status = system("fuser /dev/tty1 >/dev/null 2>/dev/null") >> 8;
  printf("%s",
         status ?
           "tty not in use as a text terminal.\n" :
           "tty in use as a text terminal.\n");
  return 0;
}

Upvotes: 0

duck
duck

Reputation: 171

Execute this as a system call "ps au >tempfile.txt", and parse the file.

Upvotes: 0

jim mcnamara
jim mcnamara

Reputation: 16379

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.

Upvotes: 2

Related Questions