Determine the owner of the session of a process

aixprocessptysessiontty

I am trying to implement a way to determine which user is owner of some process's session, in a way that's as cross-platform as possible.

In Linux I can trace the process's pty by following the link /proc/PID/fd/0 -> /dev/pts/31 and looking up who is the owner of this terminal in the utmp file.
How do I do this in, say, AIX 6.1? /proc/PID/fd contains char files and not symlinks as Linux does… Also what is the exact structure of a single entry in utmp file on AIX 6.1? Tried to read it using 7.1 [http://www-01.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.files/utmp.h.htm] utmp structures but it does not really fit the pattern.

Best Answer

Maybe I am over simplifying, but, can you just do this?

ps -p <pid> -F tty

Here is an example:

$ ps -p 6947010
      PID    TTY  TIME CMD
  6947010  pts/0  0:00 ksh

$ ps -p 6947010 -F tty=
 pts/0

Here is how you could determine to allow or deny access to a particular process:

You first determine who owns the process and which pts device started it by using:

$ ps -p <PID> -F tty=,user=
 pts/X  <username>

Then you check the owner of the pts/X device, like this:

$ ls -l /dev/pts/X
crw--w--w-    1 <username>  <group>     21,  0 Apr 18 13:27 /dev/pts/0

If the owner of /dev/pts/X is the same as the process owner then then the process was started by the login user and you will grant access, if the owner of the /dev/pts/X device is not the same as the owner of the process then you deny access.