summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRay Strode <rstrode@redhat.com>2009-07-20 16:37:45 -0400
committerRay Strode <rstrode@redhat.com>2009-07-20 16:37:45 -0400
commit282c47eb102f839cbf3bee646ba2841362284760 (patch)
tree0e3acf1754917582f541afedf0719e984758d3ae
parentbcc18427eec07139c9d907ac3995f2c157a7c715 (diff)
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.
-rw-r--r--tools/ck-get-x11-display-device.c72
1 files changed, 72 insertions, 0 deletions
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 <stdio.h>
#include <X11/Xlib.h>
+#include <X11/Xatom.h>
#include <glib.h>
#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);