From 282c47eb102f839cbf3bee646ba2841362284760 Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Mon, 20 Jul 2009 16:37:45 -0400 Subject: Get VT from X display if no controlling tty In some cases a controlling tty won't be able available to peek at to find the VT of the X display. In those cases, we fall back to looking at the XFree86_VT property. We can do this because we know the display is a local display since we've already gotten peer credentials from its socket. --- tools/ck-get-x11-display-device.c | 72 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tools/ck-get-x11-display-device.c b/tools/ck-get-x11-display-device.c index b349cc7..eb2fec9 100644 --- a/tools/ck-get-x11-display-device.c +++ b/tools/ck-get-x11-display-device.c @@ -27,6 +27,7 @@ #include #include +#include #include #include "ck-sysdeps.h" @@ -78,6 +79,72 @@ display_init (const char *display_name) return xdisplay; } +static char * +get_tty_for_display (Display *xdisplay) +{ + Window root_window; + Atom xfree86_vt_atom; + Atom return_type_atom; + int return_format; + gulong return_count; + gulong bytes_left; + guchar *return_value; + glong vt; + char *display; + + display = NULL; + + xfree86_vt_atom = XInternAtom (xdisplay, "XFree86_VT", True); + + if (xfree86_vt_atom == None) { + return NULL; + } + + root_window = DefaultRootWindow (xdisplay); + + g_assert (root_window != None); + + return_value = NULL; + if (XGetWindowProperty(xdisplay, root_window, xfree86_vt_atom, + 0L, 1L, False, XA_INTEGER, + &return_type_atom, &return_format, + &return_count, &bytes_left, + &return_value) != Success) { + goto out; + } + + if (return_type_atom != XA_INTEGER) { + goto out; + } + + if (return_format != 32) { + goto out; + } + + if (return_count != 1) { + goto out; + } + + if (bytes_left != 0) { + goto out; + } + + vt = *((glong *) return_value); + + if (vt <= 0) { + goto out; + } + + display = g_strdup_printf ("/dev/tty%ld", vt); + +out: + if (return_value != NULL) { + XFree (return_value); + } + + return display; +} + int main (int argc, char **argv) @@ -115,6 +182,11 @@ main (int argc, if (res) { if (pid > 0) { device = get_tty_for_pid (pid); + + if (device == NULL) { + device = get_tty_for_display (xdisplay); + } + if (device != NULL) { printf ("%s\n", device); g_free (device); -- cgit