Given an X11 window ID, is there a way to find the ID of the process that created it?
Of course this isn't always possible, for example if the window came over a TCP connection. For that case I'd like the IP and port associated with the remote end.
The question was asked before on Stack Overflow, and a proposed method was to use the _NET_WM_PID
property. But that's set by the application. Is there a way to do it if the application doesn't play nice?
Best Answer
Unless your X-server supports
XResQueryClientIds
from X-Resource v1.2 extension I know no easy way to reliably request process ID. There're other ways however.If you just have a window in front of you and don't know its ID yet — it's easy to find it out. Just open a terminal next to the window in question, run
xwininfo
there and click on that window.xwininfo
will show you the window-id.So let's assume you know a window-id, e.g. 0x1600045, and want to find, what's the process owning it.
The easiest way to check who that window belongs to is to run XKillClient for it i.e.:
and see which process just died. But only if you don't mind killing it of course!
Another easy but unreliable way is to check its
_NET_WM_PID
andWM_CLIENT_MACHINE
properties:That's what tools like
xlsclients
andxrestop
do.Unfortunately this information may be incorrect not only because the process was evil and changed those, but also because it was buggy. For example after some firefox crash/restart I've seen orphaned windows (from flash plugin, I guess) with
_NET_WM_PID
pointing to a process, that died long time ago.Alternative way is to run
and check properties of parents of the window in question. That may also give you some hints about window origins.
But! While you may not find what process have created that window, there's still a way to find where that process have connected to X-server from. And that way is for real hackers. :)
The window-id 0x1600045 that you know with lower bits zeroed (i.e. 0x1600000) is a "client base". And all resource IDs, allocated for that client are "based" on it (0x1600001, 0x1600002, 0x1600003, etc). X-server stores information about its clients in clients[] array, and for each client its "base" is stored in clients[i]->clientAsMask variable. To find X-socket, corresponding to that client, you need to attach to X-server with
gdb
, walk over clients[] array, find client with thatclientAsMask
and print its socket descriptor, stored in ((OsCommPtr)(clients[i]->osPrivate))->fd.There may be many X-clients connected, so in order to not check them all manually, let's use a gdb function:
When you find the socket, you can check, who's connected to it, and finally find the process.
WARNING: Do NOT attach gdb to X-server from INSIDE the X-server. gdb suspends the process it attaches to, so if you attach to it from inside X-session, you'll freeze your X-server and won't be able to interact with gdb. You must either switch to text terminal (
Ctrl+Alt+F2
) or connect to your machine over ssh.Example:
Find the PID of your X-server:
Window id is 0x1600045, so client base is 0x1600000. Attach to X-server and find client socket descriptor for that client base. You'll need debug information installed for X-server (-debuginfo package for rpm-distributions or -dbg package for deb's).
Now you know that client is connected to a server socket 31. Use
lsof
to find what that socket is:(here "X" is the process name, "1237" is its pid, "root" is the user it's running from, "31u" is a socket descriptor)
There you may see that the client is connected over TCP, then you can go to the machine it's connected from and check
netstat -nap
there to find the process. But most probably you'll see a unix socket there, as shown above, which means it's a local client.To find a pair for that unix socket you can use the MvG's technique (you'll also need debug information for your kernel installed):
Now that you know client socket, use
lsof
to find PID holding it:That's it. The process keeping that window is "firefox" with process-id 7725
2017 Edit: There are more options now as seen at Who's got the other end of this unix socketpair?. With Linux 3.3 or above and with
lsof
4.89 or above, you can replace points 3 to 5 above with:to find out who's at the other end of the socket on fd 31 of the X-server process with ID 1237.