[PATCH 1/1] Fix FreeBSD issues translating VT numbers to devices

Joe Marcus Clarke marcus at FreeBSD.org
Tue Aug 28 21:27:37 PDT 2007


In FreeBSD the device number is always one less than the VT number (e.g.
VT 1 is /dev/ttyv0).  Account for this.  Also, fix up reading console
entries from /etc/ttys, and make sure the full TTY device name is returned
for a given process stat.
---
 src/ck-sysdeps-freebsd.c |   22 ++++++++++++++--------
 1 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/ck-sysdeps-freebsd.c b/src/ck-sysdeps-freebsd.c
index ed86204..1fbdf7d 100644
--- a/src/ck-sysdeps-freebsd.c
+++ b/src/ck-sysdeps-freebsd.c
@@ -182,11 +182,11 @@ stat2proc (pid_t        pid,
         snprintf (P->tty_text, sizeof P->tty_text, "%3d,%-3d", tty_maj, tty_min);
 
         if (p.ki_tdev != NODEV && (ttname = devname (p.ki_tdev, S_IFCHR)) != NULL) {
-                memcpy (P->tty_text, ttname, 8);
+                memcpy (P->tty_text, ttname, sizeof P->tty_text);
         }
 
         if (p.ki_tdev == NODEV) {
-                memcpy (P->tty_text, "   ?   ", 8);
+                memcpy (P->tty_text, "   ?   ", sizeof P->tty_text);
         }
 
         if (P->pid != pid) {
@@ -335,11 +335,12 @@ ck_get_max_num_consoles (guint *num)
                         max_consoles++;
         }
 
-        if (errno == 0) {
-                ret = TRUE;
-        } else {
-                max_consoles = 0;
-        }
+        /* Increment one more so that all consoles are properly counted
+         * this is arguable a bug in vt_add_watches().
+         */
+        max_consoles++;
+
+        ret = TRUE;
 
         endttyent ();
 
@@ -356,6 +357,9 @@ ck_get_console_device_for_num (guint num)
 {
         char *device;
 
+        /* The device number is always one less than the VT number. */
+        num--;
+
         device = g_strdup_printf ("/dev/ttyv%u", num);
 
         return device;
@@ -376,6 +380,8 @@ ck_get_console_num_from_device (const char *device,
         }
 
         if (sscanf (device, "/dev/ttyv%u", &n) == 1) {
+                /* The VT number is always one more than the device number. */
+                n++;
                 ret = TRUE;
         }
 
@@ -405,7 +411,7 @@ ck_get_active_console_num (int    console_fd,
                 goto out;
         }
 
-        g_debug ("Active VT is: ttyv%d", active);
+        g_debug ("Active VT is: %d (ttyv%d)", active, active - 1);
         ret = TRUE;
 
  out:
-- 
1.5.2.1



More information about the hal mailing list