On Wed, 24 Mar 2004 00:52:28 -0800 (PST) dave giffin <[EMAIL PROTECTED]>
babbled:

</lurk>

much much much cleaner:
compile the attached 2 files into a .so shared library and use it as an
LD_PRELOAD when running ALL x applications (or make appropriate changes to Xlib
itself). this is a non invasive method that doesnt change the xserver or xlib -
it simply forces window creates (and reparents) to root to have that window
carry some extra properites - one of which is the process id (also machine name,
username etc. is done too).

if you dont know about LD_PRELOAD i'd highly suggest reading up about it - its a
great way to do the most evil of hacks :)

<lurk>

> I need to be able to tell which process a given window
> was created by. (Window ID=>Process ID)
> 
> Apparently, X servers don't know which process a
> client is, they just get a socket (b/c of X's network
> transparency).
> 
> It has been suggested that I could run a program like
> netstat on the client machine(which in my case is the
> same machine running the server) to get a list of
> which sockets belong to which processes.
> 
> Then, I need to get the ID of the foreign socket
> (socket ID on client end) from which a window was
> created.
> 
> What would work well is if the socket ID and the
> client/window ID could be written to STDOUT or STDIN
> when the window is created. Then I can have a script
> read the server's STDOUT or STDIN and compare it to
> the list of socket IDs and process IDs from netstat on
> the client machine (which for my purposes is on the
> same machine as the server).
> 
> MY PROBLEM: I don't know how to get XFree86 to write
> the socket ID and client ID when a window is created!
> 
> Today, I was looking at the XFree86(4.3.0) source code
> for the first time and trying some different hacks to
> get the info I wanted to print. 
> 
> In xc/programs/Xserver/dix/dispatch.c, I found
> ProcCreateWindow and the ClientPtr structure. I was
> able to get X to print client->index and
> client->OsPrivate->fd when a new window was created.
> 
> Which is what I want, except that the client index
> that is printed is always 2 and the socket ID is
> always 13. 13 is the ID of the socket that X listens
> on and I think 2 is the server client?
> 
> What am I doing wrong?
> 
> How can I print the Window ID and foreign/from socket
> ID of a client when a window is created?
> 
> 
> :) thanx
> 
> PS: I'm thinking of making a sourceforge project to
> publish the XFree86 modification and some associated
> scripts so that others will be able to see which
> process created which window. How does the project
> name XTracer sound?
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Finance Tax Center - File online. File on time.
> http://taxes.yahoo.com/filing.html
> _______________________________________________
> Devel mailing list
> [EMAIL PROTECTED]
> http://XFree86.Org/mailman/listinfo/devel


-- 
------------- Codito, ergo sum - "I code, therefore I am" --------------
The Rasterman (Carsten Haitzler)    [EMAIL PROTECTED]
熊耳 - 車君 (数田)                  [EMAIL PROTECTED]
Tokyo, Japan (東京 日本)
#include "config.h"
#include "e_hack.h"

/* prototypes */
static void __e_hack_set_properties(Display *display, Window window);

/* dlopened xlib so we can find the symbols in the real xlib to call them */
static void *lib_xlib = NULL;

/* the function that actually sets the properties on toplevel window */
static void
__e_hack_set_properties(Display *display, Window window)
{
   static Atom a_launch_id = 0;
   static Atom a_launch_path = 0;
   static Atom a_user_id = 0;
   static Atom a_process_id = 0;
   static Atom a_p_process_id = 0;
   static Atom a_machine_name = 0;
   static Atom a_user_name = 0;
   char *env = NULL;

   if (!a_launch_id)    a_launch_id    = XInternAtom(display, "_E_HACK_LAUNCH_ID", False);
   if (!a_launch_path)  a_launch_path  = XInternAtom(display, "_E_HACK_LAUNCH_PATH", False);
   if (!a_user_id)      a_user_id      = XInternAtom(display, "_E_HACK_USER_ID", False);
   if (!a_process_id)   a_process_id   = XInternAtom(display, "_E_HACK_PROCESS_ID", False);
   if (!a_p_process_id) a_p_process_id = XInternAtom(display, "_E_HACK_PARENT_PROCESS_ID", False);
   if (!a_machine_name) a_machine_name = XInternAtom(display, "_E_HACK_MACHINE_NAME", False);
   if (!a_user_name)    a_user_name    = XInternAtom(display, "_E_HACK_USER_NAME", False);

   if ((env = getenv("E_HACK_LAUNCH_ID")))
      XChangeProperty(display, window, a_launch_id, XA_STRING, 8, PropModeReplace, env, strlen(env));
   if ((env = getenv("E_HACK_LAUNCH_PATH")))
      XChangeProperty(display, window, a_launch_path, XA_STRING, 8, PropModeReplace, env, strlen(env));
     {
	uid_t uid;
	pid_t pid, ppid;
	struct utsname ubuf;
	char buf[4096];
	
	uid = getuid();
	pid = getpid();
	ppid = getppid();

	snprintf(buf, sizeof(buf), "%i", uid);
	XChangeProperty(display, window, a_user_id, XA_STRING, 8, PropModeReplace, buf, strlen(buf));
	snprintf(buf, sizeof(buf), "%i", pid);
	XChangeProperty(display, window, a_process_id, XA_STRING, 8, PropModeReplace, buf, strlen(buf));
	snprintf(buf, sizeof(buf), "%i", ppid);
	XChangeProperty(display, window, a_p_process_id, XA_STRING, 8, PropModeReplace, buf, strlen(buf));
	if (!uname(&ubuf))
	  {
	     snprintf(buf, sizeof(buf), "%s", ubuf.nodename);
	     XChangeProperty(display, window, a_machine_name, XA_STRING, 8, PropModeReplace, buf, strlen(buf));
	  }
	else
	   XChangeProperty(display, window, a_machine_name, XA_STRING, 8, PropModeReplace, " ", 1);
     }
   if ((env = getenv("USER")))
      XChangeProperty(display, window, a_user_name, XA_STRING, 8, PropModeReplace, env, strlen(env));
}

/* XCreateWindow intercept hack */
Window
XCreateWindow(
	      Display *display,
	      Window parent,
	      int x, int y,
	      unsigned int width, unsigned int height,
	      unsigned int border_width,
	      int depth,
	      unsigned int class,
	      Visual *visual,
	      unsigned long valuemask,
	      XSetWindowAttributes *attributes
	      )
{
   static Window (*func)
      (
       Display *display,
       Window parent,
       int x, int y,
       unsigned int width, unsigned int height,
       unsigned int border_width,
       int depth,
       unsigned int class,
       Visual *visual,
       unsigned long valuemask,
       XSetWindowAttributes *attributes
       ) = NULL;
   int i;

   /* find the real Xlib and the real X function */
   if (!lib_xlib) lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
   if (!func) func = dlsym (lib_xlib, "XCreateWindow");

   /* multihead screen handling loop */
   for (i = 0; i < ScreenCount(display); i++)
     {
	/* if the window is created as a toplevel window */
	if (parent == RootWindow(display, i))
	  {
	     Window window;
	     
	     /* create it */
	     window = (*func) (display, parent, x, y, width, height, 
				border_width, depth, class, visual, valuemask, 
				attributes);
	     /* set properties */
	     __e_hack_set_properties(display, window);
	     /* return it */
	     return window;
	  }
     }
   /* normal child window - create as usual */
   return (*func) (display, parent, x, y, width, height, border_width, depth,
		   class, visual, valuemask, attributes);
}

/* XCreateSimpleWindow intercept hack */
Window
XCreateSimpleWindow(
		    Display *display,
		    Window parent,
		    int x, int y,
		    unsigned int width, unsigned int height,
		    unsigned int border_width,
		    unsigned long border,
		    unsigned long background
		    )
{
   static Window (*func)
      (
       Display *display,
       Window parent,
       int x, int y,
       unsigned int width, unsigned int height,
       unsigned int border_width,
       unsigned long border,
       unsigned long background
       ) = NULL;
   int i;
   
   /* find the real Xlib and the real X function */
   if (!lib_xlib) lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
   if (!func) func = dlsym (lib_xlib, "XCreateSimpleWindow");
   
   /* multihead screen handling loop */
   for (i = 0; i < ScreenCount(display); i++)
     {
	/* if the window is created as a toplevel window */
	if (parent == RootWindow(display, i))
	  {
	     Window window;
	     
	     /* create it */
	     window = (*func) (display, parent, x, y, width, height, 
				border_width, border, background);
	     /* set properties */
	     __e_hack_set_properties(display, window);
	     /* return it */
	     return window;
	  }
     }
   /* normal child window - create as usual */
   return (*func) (display, parent, x, y, width, height, 
		   border_width, border, background);
}

/* XReparentWindow intercept hack */
int
XReparentWindow(
		Display *display,
		Window window,
		Window parent,
		int x, int y
		)
{
   static int (*func)
      (
       Display *display,
       Window window,
       Window parent,
       int x, int y
       ) = NULL;
   int i;
   
   /* find the real Xlib and the real X function */
   if (!lib_xlib) lib_xlib = dlopen("libX11.so", RTLD_GLOBAL | RTLD_LAZY);
   if (!func) func = dlsym (lib_xlib, "XReparentWindow");
   
   /* multihead screen handling loop */
   for (i = 0; i < ScreenCount(display); i++)
     {
	/* if the window is created as a toplevel window */
	if (parent == RootWindow(display, i))
	  {
	     /* set properties */
	     __e_hack_set_properties(display, window);
	     /* reparent it */
	     return (*func) (display, window, parent, x, y);
	  }
     }
   /* normal child window reparenting - reparent as usual */
   return (*func) (display, window, parent, x, y);
}
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <dlfcn.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/Xatom.h>

Reply via email to