#!/usr/bin/python # # Copyright (c) 2008 Red Hat, Inc. # Author: Adam Jackson # License: http://www.opensource.org/licenses/mit-license.php # # Try to get the list of X display connections. # # This tries harder than xlsclients, which only looks for windows with # the WM_COMMAND property set. The algorithm is fundamentally unreliable # though. Essentially we're looking through the list of sockets by inode # number, and assuming that the client inode is immediately less than # the server inode in numeric sort order. It's guaranteed to be strictly # less than (the connect() call happens before the accept() call), if you # ignore uint64_t wraparound, but some other process could have created a # socket between those two. # # If you're not running on Linux, this probably doesn't work at all. from subprocess import Popen, PIPE client_sockets = [] match = 0 ns = Popen(["netstat", "-an", "--unix"], stdout=PIPE) output = ns.communicate()[0] for line in output.split('\n'): if line.find("X11-unix") != -1: match = 1 elif match: match = 0 inode = line.split()[6] client_sockets.append(inode) lsof = Popen(["lsof", "-U", "+c0", "-w"], stdout=PIPE) output = lsof.communicate()[0] for line in output.split('\n'): try: inode = line.split()[7] if inode in client_sockets: print line except: pass