What are the ways to find the session leader or the controlling TTY of a process group in Linux?

huangapple go评论87阅读模式
英文:

What are the ways to find the session leader or the controlling TTY of a process group in Linux?

问题

这不是一个特定语言的问题,尽管我目前正在使用golang。

我正在编写一个命令行程序,我想要找到程序的真实UID(通过真实UID,我指的是如果用户使用sudo,有效的UID会发生变化,但真实的UID将与用户相同)。

  • 我读到过一种方法是通过找到控制终端的所有者来实现,而在Linux上,我们可以使用"tty"命令来返回与STDINPUT连接的终端的文件名,检查其所有权是一种方法。

  • 另一种方法是找到会话领导进程以及其所有者。

我尝试了第一种方法,使用:

cmdOut []byte
cmdOut, _ = exec.Command("tty").Output()

但是当我从我的shell运行程序时,它返回输出"not a tty"。有可能是因为它在一个与tty分离的分离的shell中执行(这只是一个猜测)。

我尝试了第二种方法,使用os.Getppid()来获取父进程ID,但实际上,在运行sudo时,它会再次fork,它给出了sudo进程的父进程ID(在下面的情况中为16031),而我想要获取的是3393。(从pstree输出中粘贴进程层次结构)
/usr/bin/termin(3383)-+-bash(3393)---sudo(16031)---Myprogram(16032),所以实际上我无法获取会话领导进程,只能获取父进程ID。

有人可以指导我如何使用这两种方法中的任何一种来实现这个功能吗?

英文:

This is not a language specific question, although I am using golang at the moment.

I am writing a command line program, and I wanted to find the real UID of the program.(By realUID, I meant, if the user did a sudo, the effective uid changes, but the real uid would be the same as the user's.)

  • I've read that finding the owner of the controlling tty is one way to find this, and on linux, we can use "tty" command, that will return the filename of the terminal connected to STDINPUT. Checking its ownership is one way.

  • Another way is to find the session leader process, and who owns it.

I tried the first way, using

cmdOut []byte
cmdOut, _ = exec.Command("tty").Output()

but it returns the output not a tty, when I run the program from my shell. Chances are that this might be getting executed in a separate forked shell that is detached from a tty (again, just a wild guess).

I tried the second way using os.Getppid() to get the parent pid, but in reality, when running sudo, it forks again, and it is giving the parent pid of the sudo process(16031 in the below case, whereas I am looking to grab 3393 instead.). (Pasting the process hierarchy from pstree output)
/usr/bin/termin(3383)-+-bash(3393)---sudo(16031)---Myprogram(16032), so effectively I am not able to get the session leader process, but just the parent pid.

Can someone guide me on how do I implement this functionality using either of this method?

答案1

得分: 2

编辑:
sudo设置$SUDO_USER环境变量,但它只对一个sudo命令有帮助,即如果有类似sudo sudo -u nobody your-program的命令,$SUDO_USER将被设置为"root"。还有$SUDO_UID

旧答案:exec.Command("who am i").Output()怎么样?(不起作用,仍然需要一个tty)。

英文:

Edit:
sudo set's $SUDO_USER environment variable, but it will help just with one sudo, i.e. if there was something like sudo sudo -u nobody your-program, $SUDO_USER will be set to "root". And there is $SUDO_UID too.

Old answer: How about exec.Command("who am i").Output() ? (won't work, still needs a tty).

huangapple
  • 本文由 发表于 2016年1月6日 17:56:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/34630191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定