$NetBSD: patch-af,v 1.1 2008/11/25 13:40:05 hasso Exp $

--- src/ck-sysdeps-freebsd.c.orig	2008-11-27 05:12:41 +0200
+++ src/ck-sysdeps-freebsd.c	2008-11-27 05:08:28 +0200
@@ -27,6 +27,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
+#include <glob.h>
 #include <paths.h>
 #include <ttyent.h>
 #include <kvm.h>
@@ -151,14 +152,38 @@ stat2proc (pid_t        pid,
                 return FALSE;
         }
 
+#ifdef __DragonFly__
+        num = MAXCOMLEN;
+#else
         num = OCOMMLEN;
+#endif
         if (num >= sizeof P->cmd) {
                 num = sizeof P->cmd - 1;
         }
 
+#ifdef __DragonFly__
+        memcpy (P->cmd, p.kp_comm, num);
+#else
         memcpy (P->cmd, p.ki_ocomm, num);
+#endif
 
         P->cmd[num]   = '\0';
+#ifdef __DragonFly__
+        P->pid        = p.kp_pid;
+        P->ppid       = p.kp_ppid;
+        P->pgrp       = p.kp_pgid;
+        P->session    = p.kp_sid;
+        P->rss        = p.kp_vm_rssize;
+        P->vsize      = p.kp_vm_map_size;
+        P->start_time = p.kp_start.tv_sec;
+        P->wchan      = (unsigned long) p.kp_lwp.kl_wchan;
+        P->state      = p.kp_stat;
+        P->nice       = p.kp_nice;
+        P->flags      = p.kp_flags;
+        P->tpgid      = p.kp_tpgid;
+        P->processor  = p.kp_lwp.kl_cpuid;
+        P->nlwp       = p.kp_nthreads;
+#else
         P->pid        = p.ki_pid;
         P->ppid       = p.ki_ppid;
         P->pgrp       = p.ki_pgid;
@@ -173,19 +198,33 @@ stat2proc (pid_t        pid,
         P->tpgid      = p.ki_tpgid;
         P->processor  = p.ki_oncpu;
         P->nlwp       = p.ki_numthreads;
+#endif
 
         /* we like it Linux-encoded :-) */
+#ifdef __DragonFly__
+        tty_maj = major (p.kp_tdev);
+        tty_min = minor (p.kp_tdev);
+#else
         tty_maj = major (p.ki_tdev);
         tty_min = minor (p.ki_tdev);
+#endif
         P->tty = DEV_ENCODE (tty_maj,tty_min);
 
         snprintf (P->tty_text, sizeof P->tty_text, "%3d,%-3d", tty_maj, tty_min);
 
+#ifdef __DragonFly__
+        if (p.kp_tdev != NODEV && (ttname = devname (p.kp_tdev, S_IFCHR)) != NULL) {
+#else
         if (p.ki_tdev != NODEV && (ttname = devname (p.ki_tdev, S_IFCHR)) != NULL) {
+#endif
                 memcpy (P->tty_text, ttname, sizeof P->tty_text);
         }
 
+#ifdef __DragonFly__
+        if (p.kp_tdev == NODEV) {
+#else
         if (p.ki_tdev == NODEV) {
+#endif
                 memcpy (P->tty_text, "   ?   ", sizeof P->tty_text);
         }
 
@@ -202,7 +241,6 @@ ck_process_stat_new_for_unix_pid (pid_t 
                                   GError        **error)
 {
         gboolean       res;
-        GError        *local_error;
         CkProcessStat *proc;
 
         g_return_val_if_fail (pid > 1, FALSE);
@@ -217,7 +255,6 @@ ck_process_stat_new_for_unix_pid (pid_t 
         if (res) {
                 *stat = proc;
         } else {
-                g_propagate_error (error, local_error);
                 *stat = NULL;
         }
 
@@ -308,7 +345,11 @@ ck_unix_pid_get_uid (pid_t pid)
         res = get_kinfo_proc (pid, &p);
 
         if (res) {
+#ifdef __DragonFly__
+                uid = p.kp_uid;
+#else
                 uid = p.ki_uid;
+#endif
         }
 
         return uid;
@@ -327,38 +368,40 @@ gboolean
 ck_get_max_num_consoles (guint *num)
 {
         int      max_consoles;
-        int      res;
-        gboolean ret;
-        struct ttyent *t;
+        int      i;
+        glob_t   g;
 
-        ret = FALSE;
         max_consoles = 0;
 
-        res = setttyent ();
-        if (res == 0) {
-                goto done;
-        }
-
-        while ((t = getttyent ()) != NULL) {
-                if (t->ty_status & TTY_ON && strncmp (t->ty_name, "ttyv", 4) == 0)
+        g.gl_offs = 0;
+        glob ("/dev/ttyv*", GLOB_DOOFFS, NULL, &g);
+        for (i = 0; i < g.gl_pathc && g.gl_pathv[i] != NULL; i++) {
+                int fd;
+                char *cdev;
+
+                cdev = g.gl_pathv[i];
+                fd = open (cdev, O_RDONLY | O_NOCTTY);
+                if (fd > -1) {
+                        close (fd);
                         max_consoles++;
+                } else {
+                        break;
+                }
         }
 
-        /* Increment one more so that all consoles are properly counted
+        globfree (&g);
+
+        /*
+         * Increment one more so that all consoles are properly counted
          * this is arguable a bug in vt_add_watches().
          */
         max_consoles++;
 
-        ret = TRUE;
-
-        endttyent ();
-
-done:
         if (num != NULL) {
                 *num = max_consoles;
         }
 
-        return ret;
+        return TRUE;
 }
 
 char *
@@ -369,7 +412,12 @@ ck_get_console_device_for_num (guint num
         /* The device number is always one less than the VT number. */
         num--;
 
-        device = g_strdup_printf ("/dev/ttyv%u", num);
+        if (num < 10)
+                device = g_strdup_printf ("/dev/ttyv%i", num);
+        else if (num < 32)
+                device = g_strdup_printf ("/dev/ttyv%c", num - 10 + 'a');
+        else
+                device = NULL;
 
         return device;
 }
@@ -379,6 +427,7 @@ ck_get_console_num_from_device (const ch
                                 guint      *num)
 {
         guint    n;
+        char     c;
         gboolean ret;
 
         n = 0;
@@ -388,7 +437,11 @@ ck_get_console_num_from_device (const ch
                 return FALSE;
         }
 
-        if (sscanf (device, "/dev/ttyv%u", &n) == 1) {
+        if (sscanf (device, "/dev/ttyv%c", &c) == 1) {
+                if (c < 58)
+                        n = c - 48;
+                else
+                        n = c - 'a' + 10;
                 /* The VT number is always one more than the device number. */
                 n++;
                 ret = TRUE;
@@ -408,6 +461,7 @@ ck_get_active_console_num (int    consol
         gboolean ret;
         int      res;
         int      active;
+        char      ttyn;
 
         g_assert (console_fd != -1);
 
@@ -420,7 +474,12 @@ ck_get_active_console_num (int    consol
                 goto out;
         }
 
-        g_debug ("Active VT is: %d (ttyv%d)", active, active - 1);
+        if (active - 1 < 10)
+                ttyn = active - 1 + '0';
+        else
+                ttyn = active - 11 + 'a';
+
+        g_debug ("Active VT is: %d (ttyv%c)", active, ttyn);
         ret = TRUE;
 
  out:
