Wed, Mar 24, 2004 at 12:52:28AM -0800, dave giffin escribió:

>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).

        If you are running a window manager setting the _NET_WM_PID
property correctly (metacity does it, dunno about others) you can do the
mapping easily. The next example get that prop from the Window ID, and
then converts the PID to the UID of user running the process (it uses
libgtop for doing that):

Salu2


#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <X11/Xfuncs.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <ctype.h>

#include <X11/Xatom.h>
#include <X11/Xmu/WinUtil.h>

#include <libgtop-2.0/glibtop.h>
#include <libgtop-2.0/glibtop/procuid.h>


int
main (int argc, char*argv[])
{
        Atom atom,actual_type;
        Display *dpy;
        int screen;
        char *atom_name;
        int actual_format;
        unsigned long nitems;
        unsigned long bytes_after;
        unsigned char *prop;
        int status;
        int pid;
        glibtop_proc_uid proc_uid;
                        
        Window w=0;

        //dpy = XOpenDisplay(":0");
        dpy = XOpenDisplay(getenv("DISPLAY"));
        screen = DefaultScreen(dpy);
        if (argc!=2) {
                printf("Usage %s window-id\n",argv[0]);
                exit(-1);
        }

        sscanf(argv[1],"0x%x",&w);

    
        atom = XInternAtom(dpy, "_NET_WM_PID", True);
        atom_name = XGetAtomName (dpy,atom);

                                                        
        status = XGetWindowProperty(dpy, w, atom, 0, 1024,
                                    False, AnyPropertyType,
                                    &actual_type,
                                    &actual_format, &nitems,
                                    &bytes_after,
                                    &prop);
        if (status!=0) {
                printf("Cannot get _NET_WM_PID");
                exit(-2);
        }

        pid = prop[1] * 256; 
        pid += prop[0];
        printf("pid of window 0x%x = %d\n",w,pid);
        glibtop_get_proc_uid (&proc_uid, pid);
        printf("uid = %d, euid = %d\n",proc_uid.uid, proc_uid.euid);
        return 0;
}


-- 
Fernando Herrera de las Heras
Onírica: análisis, diseño e implantación de soluciones informáticas
http://www.onirica.com
_______________________________________________
Devel mailing list
[EMAIL PROTECTED]
http://XFree86.Org/mailman/listinfo/devel

Reply via email to