3

In Linux, each terminal is associated with only one session (a session has one or more process groups, and a process group has one or more processes).

Is there some function (or a command) that takes a tty device file (for example: /dev/tty1 or /dev/pts/0) and returns the session id associated with this tty/terminal?

alk
  • 69,737
  • 10
  • 105
  • 255
user8437463
  • 357
  • 3
  • 6
  • I found out that ioctl TIOCGSID can find it for *current controlling terminal*. Not for any other terminals. Search continues... – Antti Haapala -- Слава Україні Nov 10 '17 at 17:17
  • You could retrieve the PID having /dev/tty1 open by scanning `/proc` and then call `getsid(pid)` to get the session. – alk Nov 10 '17 at 17:27
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Nov 10 '17 at 17:56
  • @jww: Well ... "*Is there some function ...*" smells pretty strong like programming. – alk Nov 10 '17 at 18:03
  • I hate mentioning this because of the potential side effects, but you can fork a child process which blocks all signals, and temporarily changes its controlling terminal to that terminal, uses the `TIOCGSID` ioctl to get the session ID, then detaches from the terminal, passes the session ID to its parent, and exits. I mention this only because it might be needed if the terminal is associated with nefarious processes that might be exploiting certain backslash-escaping bugs in `/proc/PID/` files (making it hard/impossible to reliably process them correctly). – Nominal Animal Nov 20 '17 at 11:12

2 Answers2

3

Perform the following steps:

  1. stat the TTY you want to check. In particular, find out the major/minor device id it is using. Combine them to a single number using the formula major*256+minor (or just take the raw number from stat)

  2. Open /proc/ and scan all directories whose name is only digits. The directory's name is the pid of a process in the system.

  3. For each such directory, open /proc/pid/stat, and parse the file into fields separated by a space (except the second field, which has brackets around it). The 7th field will be the TTY device's major/minor. Scan until you find one that matches the TTY for which you are looking.

  4. The 6th field in that file is the sid for the process (the number you're looking for). The 8th field is the TTY's pgrp.

File structure detailed here.

Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57
0

By Mohit BATRA = Each time you open a terminal a new session gets started and a unique session Id is assigned to it if We need to find the session id associated with each terminal then it can be possible by = ps command , and By = tty command we can find the terminal type or number and to get the session I'd associated with it we need to open the terminal two more time and check its terminal number with = tty then run

ps -exo sess,tty | grep pts/1

Here pts/1 is my terminal number which I have get by running the command = tty on the terminal and this is different in your case just note your and paste after grep and you will get the session id of that session Good luck.

U.Shaba
  • 542
  • 5
  • 11