From 5e0f4848e5bfd325840d405582bdedf3a27b98f8 Mon Sep 17 00:00:00 2001 From: William Jon McCann Date: Mon, 27 Aug 2007 16:34:45 -0400 Subject: move some of the freebsd specific stuff into sysdeps --- src/ck-sysdeps-freebsd.c | 37 +++++++++++++++++ src/ck-sysdeps-linux.c | 49 +++++++++++++++++++++++ src/ck-sysdeps-solaris.c | 48 +++++++++++++++++++++++ src/ck-sysdeps-unix.c | 69 ++++++++++++++++++++++++++++++++ src/ck-sysdeps.h | 6 +++ src/ck-vt-monitor.c | 96 +++++++++------------------------------------ src/test-tty-idle-monitor.c | 14 +------ 7 files changed, 229 insertions(+), 90 deletions(-) (limited to 'src') diff --git a/src/ck-sysdeps-freebsd.c b/src/ck-sysdeps-freebsd.c index 0e7f845..99a1791 100644 --- a/src/ck-sysdeps-freebsd.c +++ b/src/ck-sysdeps-freebsd.c @@ -35,6 +35,9 @@ #include #include #include +#include + +#include #define DEV_ENCODE(M,m) ( \ ( (M&0xfff) << 8) | ( (m&0xfff00) << 12) | (m&0xff) \ @@ -42,6 +45,10 @@ #include "ck-sysdeps.h" +#ifndef ERROR +#define ERROR -1 +#endif + /* adapted from procps */ struct _CkProcessStat { @@ -412,3 +419,33 @@ ck_get_console_num_from_device (const char *device, return ret; } + +gboolean +ck_get_active_console_num (int console_fd, + guint *num) +{ + gboolean ret; + int res; + int active; + + g_assert (console_fd != -1); + + active = 0; + ret = FALSE; + + res = ioctl (console_fd, VT_GETACTIVE, &active); + if (res == ERROR) { + perror ("ioctl VT_GETACTIVE"); + goto out; + } + + g_debug ("Active VT is: ttyv%d", active); + ret = TRUE; + + out: + if (num != NULL) { + *num = active; + } + + return ret; +} diff --git a/src/ck-sysdeps-linux.c b/src/ck-sysdeps-linux.c index fcf3b4e..7dc99f7 100644 --- a/src/ck-sysdeps-linux.c +++ b/src/ck-sysdeps-linux.c @@ -25,8 +25,10 @@ #include #include #include +#include #include #include +#include #include #include @@ -38,6 +40,10 @@ #include "ck-sysdeps.h" +#ifndef ERROR +#define ERROR -1 +#endif + /* adapted from procps */ struct _CkProcessStat { @@ -655,3 +661,46 @@ ck_get_console_num_from_device (const char *device, return ret; } + +gboolean +ck_get_active_console_num (int console_fd, + guint *num) +{ + gboolean ret; + int res; + guint active; + struct vt_stat stat; + + g_assert (console_fd != -1); + + active = 0; + ret = FALSE; + + res = ioctl (console_fd, VT_GETSTATE, &stat); + if (res == ERROR) { + perror ("ioctl VT_GETSTATE"); + goto out; + } + + { + int i; + + g_debug ("Current VT: tty%d", stat.v_active); + for (i = 1; i <= 16; i++) { + gboolean is_on; + is_on = stat.v_state & (1 << i); + + g_debug ("VT %d:%s", i, is_on ? "on" : "off"); + } + } + + active = stat.v_active; + ret = TRUE; + + out: + if (num != NULL) { + *num = active; + } + + return ret; +} diff --git a/src/ck-sysdeps-solaris.c b/src/ck-sysdeps-solaris.c index 83a28f8..ac41ece 100644 --- a/src/ck-sysdeps-solaris.c +++ b/src/ck-sysdeps-solaris.c @@ -28,6 +28,7 @@ #include #include #include +#include #define DEV_ENCODE(M,m) ( \ ( (M&0xfff) << 8) | ( (m&0xfff00) << 12) | (m&0xff) \ @@ -41,6 +42,10 @@ #include "ck-sysdeps.h" +#ifndef ERROR +#define ERROR -1 +#endif + /* adapted from procps */ struct _CkProcessStat { @@ -419,3 +424,46 @@ ck_get_console_num_from_device (const char *device, return ret; } + +gboolean +ck_get_active_console_num (int console_fd, + guint *num) +{ + gboolean ret; + int res; + guint active; + struct vt_stat stat; + + g_assert (console_fd != -1); + + active = 0; + ret = FALSE; + + res = ioctl (console_fd, VT_GETSTATE, &stat); + if (res == ERROR) { + perror ("ioctl VT_GETSTATE"); + goto out; + } + + { + int i; + + g_debug ("Current VT: tty%d", stat.v_active); + for (i = 1; i <= 16; i++) { + gboolean is_on; + is_on = stat.v_state & (1 << i); + + g_debug ("VT %d:%s", i, is_on ? "on" : "off"); + } + } + + active = stat.v_active; + ret = TRUE; + + out: + if (num != NULL) { + *num = active; + } + + return ret; +} diff --git a/src/ck-sysdeps-unix.c b/src/ck-sysdeps-unix.c index 6c9b34c..2094e48 100644 --- a/src/ck-sysdeps-unix.c +++ b/src/ck-sysdeps-unix.c @@ -33,6 +33,7 @@ #ifdef __linux__ #include +#include #endif #ifdef __FreeBSD__ @@ -45,6 +46,10 @@ #include "ck-sysdeps.h" +#ifndef ERROR +#define ERROR -1 +#endif + /* Adapted from dbus-sysdeps-unix.c:_dbus_read_credentials_socket() */ gboolean ck_get_socket_peer_credentials (int socket_fd, @@ -234,3 +239,67 @@ ck_is_root_user (void) #endif return FALSE; } + +gboolean +ck_wait_for_active_console_num (int console_fd, + guint num) +{ + gboolean ret; + int res; + + g_assert (console_fd != -1); + + again: + ret = FALSE; + + g_debug ("VT_WAITACTIVE for vt %d", num); + errno = 0; + res = ioctl (console_fd, VT_WAITACTIVE, num); + + g_debug ("VT_WAITACTIVE for vt %d returned %d", num, ret); + + if (res == ERROR) { + const char *errmsg; + + errmsg = g_strerror (errno); + + if (errno == EINTR) { + g_debug ("Interrupted waiting for native console %d activation: %s", + num, + errmsg); + goto again; + } else { + g_warning ("Error waiting for native console %d activation: %s", + num, + errmsg); + } + goto out; + } + + ret = TRUE; + + out: + return ret; +} + +gboolean +ck_activate_console_num (int console_fd, + guint num) +{ + gboolean ret; + int res; + + g_assert (console_fd != -1); + + ret = FALSE; + + errno = 0; + res = ioctl (console_fd, VT_ACTIVATE, num); + if (res == 0) { + ret = TRUE; + } else { + g_warning ("Unable to activate console: %s", g_strerror (errno)); + } + + return ret; +} diff --git a/src/ck-sysdeps.h b/src/ck-sysdeps.h index 3ef7c14..f0fceb7 100644 --- a/src/ck-sysdeps.h +++ b/src/ck-sysdeps.h @@ -63,6 +63,12 @@ gboolean ck_get_max_num_consoles (guint *num); char * ck_get_console_device_for_num (guint num); gboolean ck_get_console_num_from_device (const char *device, guint *num); +gboolean ck_get_active_console_num (int console_fd, + guint *num); +gboolean ck_activate_console_num (int console_fd, + guint num); +gboolean ck_wait_for_active_console_num (int console_fd, + guint num); G_END_DECLS diff --git a/src/ck-vt-monitor.c b/src/ck-vt-monitor.c index 58ca2fa..59d3d1c 100644 --- a/src/ck-vt-monitor.c +++ b/src/ck-vt-monitor.c @@ -26,12 +26,6 @@ #include #include #include -#include -#if defined(__FreeBSD__) -#include -#else -#include -#endif #include #include @@ -46,7 +40,9 @@ #include "ck-sysdeps.h" #include "ck-marshal.h" +#ifndef ERROR #define ERROR -1 +#endif #define CK_VT_MONITOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CK_TYPE_VT_MONITOR, CkVtMonitorPrivate)) @@ -121,8 +117,8 @@ ck_vt_monitor_set_active (CkVtMonitor *vt_monitor, return FALSE; } - res = ioctl (vt_monitor->priv->vfd, VT_ACTIVATE, num); - if (res == 0) { + res = ck_activate_console_num (vt_monitor->priv->vfd, num); + if (res) { ret = TRUE; } else { g_set_error (error, @@ -272,35 +268,16 @@ static void * vt_thread_start (ThreadData *data) { CkVtMonitor *vt_monitor; + gboolean res; int ret; gint32 num; vt_monitor = data->vt_monitor; num = data->num; - again: - g_debug ("VT_WAITACTIVE for vt %d", num); - ret = ioctl (vt_monitor->priv->vfd, VT_WAITACTIVE, num); - - g_debug ("VT_WAITACTIVE for vt %d returned %d", num, ret); - - if (ret == ERROR) { - const char *errmsg; - - errmsg = g_strerror (errno); - - if (errno == EINTR) { - g_debug ("Interrupted waiting for native console %d activation: %s", - num, - errmsg); - goto again; - } else { - g_warning ("Error waiting for native console %d activation: %s", - num, - errmsg); - } - - g_free (data); + res = ck_wait_for_active_console_num (vt_monitor->priv->vfd, num); + if (! res) { + /* FIXME: what do we do if it fails? */ } else { EventData *event; @@ -390,50 +367,6 @@ vt_add_watches (CkVtMonitor *vt_monitor) G_UNLOCK (hash_lock); } -static guint -get_active_native (CkVtMonitor *vt_monitor) -{ - int ret; -#if defined(__FreeBSD__) - int active; -#else - struct vt_stat stat; -#endif - -#if defined(__FreeBSD__) - ret = ioctl (vt_monitor->priv->vfd, VT_GETACTIVE, &active); -#else - ret = ioctl (vt_monitor->priv->vfd, VT_GETSTATE, &stat); -#endif - if (ret == ERROR) { -#if defined(__FreeBSD__) - perror ("ioctl VT_GETACTIVE"); -#else - perror ("ioctl VT_GETSTATE"); -#endif - return -1; - } - -#if defined(__FreeBSD__) - g_debug ("Active VT is: ttyv%d", active); - return active; -#else - { - int i; - - g_debug ("Current VT: tty%d", stat.v_active); - for (i = 1; i <= 16; i++) { - gboolean is_on; - is_on = stat.v_state & (1 << i); - - g_debug ("VT %d:%s", i, is_on ? "on" : "off"); - } - } - - return stat.v_active; -#endif -} - static void ck_vt_monitor_class_init (CkVtMonitorClass *klass) { @@ -469,11 +402,20 @@ ck_vt_monitor_init (CkVtMonitor *vt_monitor) errmsg = g_strerror (errno); g_warning ("Unable to open a console: %s", errmsg); } else { + gboolean res; + guint active; + + res = ck_get_active_console_num (fd, &active); + if (! res) { + /* FIXME: handle failure */ + g_warning ("Could not determine active console"); + active = 0; + } + + vt_monitor->priv->active_num = active; vt_monitor->priv->event_queue = g_async_queue_new (); vt_monitor->priv->vt_thread_hash = g_hash_table_new (g_direct_hash, g_direct_equal); - vt_monitor->priv->active_num = get_active_native (vt_monitor); - vt_add_watches (vt_monitor); } } diff --git a/src/test-tty-idle-monitor.c b/src/test-tty-idle-monitor.c index 030eba5..f56b37f 100644 --- a/src/test-tty-idle-monitor.c +++ b/src/test-tty-idle-monitor.c @@ -31,11 +31,6 @@ #include #include #include - -#ifdef __linux__ -#include -#endif - #include #include @@ -81,14 +76,7 @@ main (int argc, char **argv) g_type_init (); if (argc < 2) { -#if defined(__FreeBSD__) - device = ttyname (0); -#else - device = g_file_read_link ("/proc/self/fd/0", NULL); - if (device == NULL) { - device = g_strdup ("/proc/self/fd/0"); - } -#endif + device = ttyname (0); } else { device = g_strdup (argv[1]); } -- cgit