获取前台控制台/查找活动X服务器

时间:2021-07-19 20:47:04

I'd like to programmatically find the X server that is attached to the console, meaning currently controlled by mouse/keyboard/screen. I assumed there is a clean way to get the current vt (using /dev/console?). The fgconsole code (fgconsole.c, getfd.c) made me doubt a bit. While

我想以编程方式找到连接到控制台的X服务器,这意味着当前由鼠标/键盘/屏幕控制。我假设有一种干净的方式来获取当前的vt(使用/ dev / console?)。 fgconsole代码(fgconsole.c,getfd.c)让我有点怀疑。而

struct vt_stat vtInfo;
ioctl(fdConsole, VT_GETSTATE, &vtInfo);

seems to be what i want, the code to retrieve a valid console fd seems somewhat unreliable - although 5 files are tested (rw/w/r each), it still fails if called inside a terminal emulation (xterm). I can probably live with that, but it doesn't really feel good... (Note: of course a xterm cannot be the console - we are talking about querying the fgconsole inside an xterm).

似乎是我想要的,检索有效的控制台fd的代码似乎有点不可靠 - 虽然测试了5个文件(每个rw / w / r),但如果在终端仿真(xterm)中调用它仍然会失败。我可能会忍受这一点,但它并不是真的感觉良好...(注意:当然xterm不能成为控制台 - 我们正在讨论查询xterm中的fgconsole)。

Next, I would have to map the vt (eg. vt7) to an X display. However, I'd rather not rely on

接下来,我必须将vt(例如vt7)映射到X显示器。但是,我宁愿不依赖

ps aux | grep X

to accomplish that... Is there a more reliable way? Could I maybe connect to all the X servers listed in /tmp/.X11-unix/ and ask them about their vt? Or even directly get their attached-to-console ('active') state? I could not find an obvious way to do that with Xlib, probably because the X server API is agnostic to vts, but maybe there is an extension for this?

要做到这一点......有更可靠的方法吗?我可以连接到/tmp/.X11-unix/中列出的所有X服务器并询问他们的vt吗?甚至直接获得他们附加到控制台('活跃')的状态?我找不到一个明显的方法来使用Xlib,可能是因为X服务器API与vts无关,但是可能有一个扩展名吗?

Thanks for any help!

谢谢你的帮助!

1 个解决方案

#1


0  

I like the /proc fs :) It seems to provide everything i need. I have not yet cast this into C code, but this should work pretty good:

我喜欢/ proc fs :)它似乎提供了我需要的一切。我还没有将它转换为C代码,但这应该工作得很好:

  • look for open X displays (and their Names, eg. :0) in /tmp/.X11-unix/
  • 在/tmp/.X11-unix/中查找打开的X显示(及其名称,例如:0)

  • look for these sockets in /proc/net/unix, get their inodes
  • 在/ proc / net / unix中查找这些套接字,获取它们的inode

  • look for processes in /proc/[PID]/ that have a vt open: ls -la /proc/*/fd/ | grep /dev/tty - if they also have one of the socket inodes in their open file descriptors, they should be an X server, else a tty. Of course, /proc/[PID]/exe helps, but may be less reliable.
  • 查找/ proc / [PID] /中有vt打开的进程:ls -la / proc / * / fd / | grep / dev / tty - 如果它们在打开的文件描述符中也有一个套接字inode,它们应该是X服务器,否则是tty。当然,/ proc / [PID] / exe有帮助,但可能不太可靠。

The output of cat /proc/net/unix | grep -a '/tmp/.X11-unix/X' suggests, that there is always a socket of the form /tmp/.X11-unix/X0 and many of the form @/tmp/.X11-unix/X0 (note the @). I wonder if it is a save assumption that there is always exactly one process (the X server) listening on the former.

cat / proc / net / unix |的输出grep -a'/ tmp / .X11-unix / X'建议,总有一个形式为/tmp/.X11-unix/X0的套接字和许多形式的@ / tmp / .X11-unix / X0(注意@)。我想知道这是否是一个保存假设,总是只有一个进程(X服务器)监听前者。

#1


0  

I like the /proc fs :) It seems to provide everything i need. I have not yet cast this into C code, but this should work pretty good:

我喜欢/ proc fs :)它似乎提供了我需要的一切。我还没有将它转换为C代码,但这应该工作得很好:

  • look for open X displays (and their Names, eg. :0) in /tmp/.X11-unix/
  • 在/tmp/.X11-unix/中查找打开的X显示(及其名称,例如:0)

  • look for these sockets in /proc/net/unix, get their inodes
  • 在/ proc / net / unix中查找这些套接字,获取它们的inode

  • look for processes in /proc/[PID]/ that have a vt open: ls -la /proc/*/fd/ | grep /dev/tty - if they also have one of the socket inodes in their open file descriptors, they should be an X server, else a tty. Of course, /proc/[PID]/exe helps, but may be less reliable.
  • 查找/ proc / [PID] /中有vt打开的进程:ls -la / proc / * / fd / | grep / dev / tty - 如果它们在打开的文件描述符中也有一个套接字inode,它们应该是X服务器,否则是tty。当然,/ proc / [PID] / exe有帮助,但可能不太可靠。

The output of cat /proc/net/unix | grep -a '/tmp/.X11-unix/X' suggests, that there is always a socket of the form /tmp/.X11-unix/X0 and many of the form @/tmp/.X11-unix/X0 (note the @). I wonder if it is a save assumption that there is always exactly one process (the X server) listening on the former.

cat / proc / net / unix |的输出grep -a'/ tmp / .X11-unix / X'建议,总有一个形式为/tmp/.X11-unix/X0的套接字和许多形式的@ / tmp / .X11-unix / X0(注意@)。我想知道这是否是一个保存假设,总是只有一个进程(X服务器)监听前者。