--- gdm-2.30.7/daemon/main.c-orig 2011-08-29 16:11:16.857687716 -0500 +++ gdm-2.30.7/daemon/main.c 2011-08-29 16:22:28.322458642 -0500 @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -461,10 +462,53 @@ gdm_daemon_change_user (uid_t *uidp, g_free (groupname); } +gboolean +ck_fd_is_a_console (int fd) +{ +#ifdef __linux__ + struct vt_stat vts; +#elif defined(__FreeBSD__) + int vers; +#endif + int kb_ok; + + errno = 0; +#ifdef __linux__ + kb_ok = (ioctl (fd, VT_GETSTATE, &vts) == 0); +#elif defined(__FreeBSD__) + kb_ok = (ioctl (fd, CONS_GETVERS, &vers) == 0); +#else + kb_ok = 1; +#endif + + return (isatty (fd) && kb_ok); +} + +static int +open_a_console (char *fnam) +{ + int fd; + + fd = open (fnam, O_RDONLY | O_NOCTTY); + if (fd < 0 && errno == EACCES) + fd = open (fnam, O_WRONLY | O_NOCTTY); + + if (fd < 0) + return -1; + + if (! ck_fd_is_a_console (fd)) { + close (fd); + fd = -1; + } + + return fd; +} + static gboolean signal_cb (int signo, gpointer data) { + int fd; int ret; g_debug ("Got callback for signal %d", signo); @@ -484,6 +528,25 @@ signal_cb (int signo, case SIGTERM: /* let the fatal signals interrupt us */ g_debug ("Caught signal %d, shutting down normally.", signo); + + /* Switch to VT1 before going down. */ + fd = open_a_console ("/dev/vt/active"); + if (fd < 0) { + fd = open_a_console ("/dev/vt/0"); + } + + if (fd >= 0) { + int res; + + res = ioctl (fd, VT_ACTIVATE, 1); + + if (res != 0) { + if (errno == ENOTSUP) { + g_debug ("VT_ENABLE not supported, cannot switch to VT1"); + } + } + } + ret = FALSE; break;