#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif
#include <X11/X.h>
#include "compiler.h"
#include "xf86.h"
#include "xf86Priv.h"
#include "xf86_OSlib.h"
#include <sys/utsname.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <errno.h>
static Bool KeepTty = FALSE;
static int devConsoleFd = -1;
#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
static int VTnum = -1;
static int initialVT = -1;
static Bool ShareVTs = FALSE;
#endif
#ifdef PCCONS_SUPPORT
#ifndef __OpenBSD__
# define PCCONS_CONSOLE_DEV1 "/dev/ttyv0"
#else
# define PCCONS_CONSOLE_DEV1 "/dev/ttyC0"
#endif
#define PCCONS_CONSOLE_DEV2 "/dev/vga"
#define PCCONS_CONSOLE_MODE O_RDWR|O_NDELAY
#endif
#ifdef SYSCONS_SUPPORT
#define SYSCONS_CONSOLE_DEV1 "/dev/ttyv0"
#define SYSCONS_CONSOLE_DEV2 "/dev/vga"
#define SYSCONS_CONSOLE_MODE O_RDWR|O_NDELAY
#endif
#ifdef PCVT_SUPPORT
#ifndef __OpenBSD__
# define PCVT_CONSOLE_DEV "/dev/ttyv0"
#else
# define PCVT_CONSOLE_DEV "/dev/ttyC0"
#endif
#define PCVT_CONSOLE_MODE O_RDWR|O_NDELAY
#endif
#if defined(WSCONS_SUPPORT) && defined(__NetBSD__)
#define WSCONS_PCVT_COMPAT_CONSOLE_DEV "/dev/ttyE0"
#endif
#ifdef __GLIBC__
#define setpgrp setpgid
#endif
#define CHECK_DRIVER_MSG \
"Check your kernel's console driver configuration and /dev entries"
static char *supported_drivers[] = {
#ifdef PCCONS_SUPPORT
"pccons (with X support)",
#endif
#ifdef SYSCONS_SUPPORT
"syscons",
#endif
#ifdef PCVT_SUPPORT
"pcvt",
#endif
#ifdef WSCONS_SUPPORT
"wscons",
#endif
};
typedef int (*xf86ConsOpen_t)(void);
#ifdef PCCONS_SUPPORT
static int xf86OpenPccons(void);
#endif
#ifdef SYSCONS_SUPPORT
static int xf86OpenSyscons(void);
#endif
#ifdef PCVT_SUPPORT
static int xf86OpenPcvt(void);
#endif
#ifdef WSCONS_SUPPORT
static int xf86OpenWScons(void);
#endif
static xf86ConsOpen_t xf86ConsTab[] = {
#ifdef PCVT_SUPPORT
xf86OpenPcvt,
#endif
#ifdef SYSCONS_SUPPORT
xf86OpenSyscons,
#endif
#ifdef PCCONS_SUPPORT
xf86OpenPccons,
#endif
#ifdef WSCONS_SUPPORT
xf86OpenWScons,
#endif
(xf86ConsOpen_t)NULL
};
void
xf86OpenConsole()
{
int i, fd = -1;
xf86ConsOpen_t *driver;
#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
int result;
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
struct utsname uts;
#endif
vtmode_t vtmode;
#endif
if (serverGeneration == 1)
{
if (geteuid() != 0)
{
FatalError("xf86OpenConsole: Server must be suid root");
}
if (!KeepTty)
{
setpgrp(0, getpid());
if ((i = open("/dev/tty",O_RDWR)) >= 0)
{
ioctl(i,TIOCNOTTY,(char *)0);
close(i);
}
}
for (driver = xf86ConsTab; *driver; driver++)
{
if ((fd = (*driver)()) >= 0)
break;
}
if (fd < 0)
{
char cons_drivers[80] = {0, };
for (i = 0; i < sizeof(supported_drivers) / sizeof(char *); i++)
{
if (i)
{
strcat(cons_drivers, ", ");
}
strcat(cons_drivers, supported_drivers[i]);
}
FatalError(
"%s: No console driver found\n\tSupported drivers: %s\n\t%s",
"xf86OpenConsole", cons_drivers, CHECK_DRIVER_MSG);
}
#if 0
fclose(stdin);
#endif
xf86Info.consoleFd = fd;
xf86Info.screenFd = fd;
switch (xf86Info.consType)
{
#ifdef PCCONS_SUPPORT
case PCCONS:
if (ioctl (xf86Info.consoleFd, CONSOLE_X_MODE_ON, 0) < 0)
{
FatalError("%s: CONSOLE_X_MODE_ON failed (%s)\n%s",
"xf86OpenConsole", strerror(errno),
CHECK_DRIVER_MSG);
}
if ((devConsoleFd = open("/dev/console", O_WRONLY,0)) < 0)
{
xf86Msg(X_WARNING,
"xf86OpenConsole: couldn't open /dev/console (%s)\n",
strerror(errno));
}
break;
#endif
#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
case SYSCONS:
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
uname (&uts);
i = atof(uts.release) * 100;
if (i >= 310) goto acquire_vt;
#endif
case PCVT:
#if !(defined(__NetBSD__) && (__NetBSD_Version__ >= 200000000))
if (initialVT != 1) {
if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, 1) != 0)
{
xf86Msg(X_WARNING,
"xf86OpenConsole: VT_ACTIVATE failed\n");
}
sleep(1);
}
#endif
acquire_vt:
if (!ShareVTs) {
SYSCALL(result =
ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno));
if (result != 0)
{
xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed\n");
}
SYSCALL(result =
ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno));
if (result != 0)
{
xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed\n");
}
signal(SIGUSR1, xf86VTRequest);
vtmode.mode = VT_PROCESS;
vtmode.relsig = SIGUSR1;
vtmode.acqsig = SIGUSR1;
vtmode.frsig = SIGUSR1;
if (ioctl(xf86Info.consoleFd, VT_SETMODE, &vtmode) < 0)
{
FatalError("xf86OpenConsole: VT_SETMODE VT_PROCESS failed");
}
#if !defined(USE_DEV_IO) && !defined(USE_I386_IOPL)
if (ioctl(xf86Info.consoleFd, KDENABIO, 0) < 0)
{
FatalError("xf86OpenConsole: KDENABIO failed (%s)",
strerror(errno));
}
#endif
if (ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS) < 0)
{
FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed");
}
} else {
close(xf86Info.consoleFd);
}
break;
#endif
#ifdef WSCONS_SUPPORT
case WSCONS:
break;
#endif
}
}
else
{
#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
if (!ShareVTs) if (xf86Info.consType == SYSCONS || xf86Info.consType == PCVT)
{
if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) != 0)
{
xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed\n");
}
}
#endif
}
return;
}
#ifdef PCCONS_SUPPORT
static int
xf86OpenPccons()
{
int fd = -1;
if ((fd = open(PCCONS_CONSOLE_DEV1, PCCONS_CONSOLE_MODE, 0))
>= 0 ||
(fd = open(PCCONS_CONSOLE_DEV2, PCCONS_CONSOLE_MODE, 0))
>= 0)
{
if (ioctl(fd, CONSOLE_X_MODE_OFF, 0) < 0)
{
FatalError(
"%s: CONSOLE_X_MODE_OFF failed (%s)\n%s\n%s",
"xf86OpenPccons",
strerror(errno),
"Was expecting pccons driver with X support",
CHECK_DRIVER_MSG);
}
xf86Info.consType = PCCONS;
xf86Msg(X_PROBED, "Using pccons driver with X support\n");
}
return fd;
}
#endif
#ifdef SYSCONS_SUPPORT
static int
xf86OpenSyscons()
{
int fd = -1;
vtmode_t vtmode;
char vtname[12];
struct stat status;
long syscons_version;
MessageType from;
if ((fd = open(SYSCONS_CONSOLE_DEV1, SYSCONS_CONSOLE_MODE, 0)) >= 0
|| (fd = open(SYSCONS_CONSOLE_DEV2, SYSCONS_CONSOLE_MODE, 0)) >= 0)
{
if (ioctl(fd, VT_GETMODE, &vtmode) >= 0)
{
if (ioctl(fd, CONS_GETVERS, &syscons_version) < 0)
{
syscons_version = 0;
}
xf86Info.vtno = VTnum;
from = X_CMDLINE;
#ifdef VT_GETACTIVE
if (ioctl(fd, VT_GETACTIVE, &initialVT) < 0)
initialVT = -1;
#endif
if (ShareVTs)
xf86Info.vtno = initialVT;
if (xf86Info.vtno == -1)
{
#if 0
if (syscons_version >= 0x100)
{
#endif
if (ioctl(fd, VT_OPENQRY, &xf86Info.vtno) < 0)
{
xf86Info.vtno = -1;
}
#if 0
}
#endif
if (xf86Info.vtno == -1)
{
if (initialVT != -1)
{
xf86Info.vtno = initialVT;
}
else if ((fstat(0, &status) >= 0)
&& S_ISCHR(status.st_mode)
&& (ioctl(0, VT_GETMODE, &vtmode) >= 0))
{
xf86Info.vtno = minor(status.st_rdev) + 1;
}
else
{
if (syscons_version >= 0x100)
{
FatalError("%s: Cannot find a free VT",
"xf86OpenSyscons");
}
FatalError("%s: %s %s\n\t%s %s",
"xf86OpenSyscons",
"syscons versions prior to 1.0 require",
"either the",
"server's stdin be a VT",
"or the use of the vtxx server option");
}
}
from = X_PROBED;
}
close(fd);
#ifndef __OpenBSD__
sprintf(vtname, "/dev/ttyv%01x", xf86Info.vtno - 1);
#else
sprintf(vtname, "/dev/ttyC%01x", xf86Info.vtno - 1);
#endif
if ((fd = open(vtname, SYSCONS_CONSOLE_MODE, 0)) < 0)
{
FatalError("xf86OpenSyscons: Cannot open %s (%s)",
vtname, strerror(errno));
}
if (ioctl(fd, VT_GETMODE, &vtmode) < 0)
{
FatalError("xf86OpenSyscons: VT_GETMODE failed");
}
xf86Info.consType = SYSCONS;
xf86Msg(X_PROBED, "Using syscons driver with X support");
if (syscons_version >= 0x100)
{
xf86ErrorF(" (version %ld.%ld)\n", syscons_version >> 8,
syscons_version & 0xFF);
}
else
{
xf86ErrorF(" (version 0.x)\n");
}
xf86Msg(from, "using VT number %d\n\n", xf86Info.vtno);
}
else
{
close(fd);
fd = -1;
}
}
return fd;
}
#endif
#ifdef PCVT_SUPPORT
static int
xf86OpenPcvt()
{
int fd = -1;
vtmode_t vtmode;
char vtname[12], *vtprefix;
struct stat status;
struct pcvtid pcvt_version;
#ifndef __OpenBSD__
vtprefix = "/dev/ttyv";
#else
vtprefix = "/dev/ttyC";
#endif
fd = open(PCVT_CONSOLE_DEV, PCVT_CONSOLE_MODE, 0);
#ifdef WSCONS_PCVT_COMPAT_CONSOLE_DEV
if (fd < 0)
{
fd = open(WSCONS_PCVT_COMPAT_CONSOLE_DEV, PCVT_CONSOLE_MODE, 0);
vtprefix = "/dev/ttyE";
}
#endif
if (fd >= 0)
{
if (ioctl(fd, VGAPCVTID, &pcvt_version) >= 0)
{
if(ioctl(fd, VT_GETMODE, &vtmode) < 0)
{
FatalError("%s: VT_GETMODE failed\n%s%s\n%s",
"xf86OpenPcvt",
"Found pcvt driver but X11 seems to be",
" not supported.", CHECK_DRIVER_MSG);
}
xf86Info.vtno = VTnum;
if (ioctl(fd, VT_GETACTIVE, &initialVT) < 0)
initialVT = -1;
if (xf86Info.vtno == -1)
{
if (ioctl(fd, VT_OPENQRY, &xf86Info.vtno) < 0)
{
xf86Info.vtno = -1;
}
if (xf86Info.vtno == -1)
{
if (initialVT != -1)
{
xf86Info.vtno = initialVT;
}
else if ((fstat(0, &status) >= 0)
&& S_ISCHR(status.st_mode)
&& (ioctl(0, VT_GETMODE, &vtmode) >= 0))
{
xf86Info.vtno = minor(status.st_rdev) + 1;
}
else
{
FatalError("%s: Cannot find a free VT",
"xf86OpenPcvt");
}
}
}
close(fd);
sprintf(vtname, "%s%01x", vtprefix, xf86Info.vtno - 1);
if ((fd = open(vtname, PCVT_CONSOLE_MODE, 0)) < 0)
{
ErrorF("xf86OpenPcvt: Cannot open %s (%s)",
vtname, strerror(errno));
xf86Info.vtno = initialVT;
sprintf(vtname, "%s%01x", vtprefix, xf86Info.vtno - 1);
if ((fd = open(vtname, PCVT_CONSOLE_MODE, 0)) < 0) {
FatalError("xf86OpenPcvt: Cannot open %s (%s)",
vtname, strerror(errno));
}
}
if (ioctl(fd, VT_GETMODE, &vtmode) < 0)
{
FatalError("xf86OpenPcvt: VT_GETMODE failed");
}
xf86Info.consType = PCVT;
#ifdef WSCONS_SUPPORT
xf86Msg(X_PROBED,
"Using wscons driver on %s in pcvt compatibility mode "
"(version %d.%d)\n", vtname,
pcvt_version.rmajor, pcvt_version.rminor);
#else
xf86Msg(X_PROBED, "Using pcvt driver (version %d.%d)\n",
pcvt_version.rmajor, pcvt_version.rminor);
#endif
}
else
{
close(fd);
fd = -1;
}
}
return fd;
}
#endif
#ifdef WSCONS_SUPPORT
static int
xf86OpenWScons()
{
int fd = -1;
int mode = WSDISPLAYIO_MODE_MAPPED;
int i;
char ttyname[16];
for (i = 0; i < 8; i++) {
#if defined(__NetBSD__)
sprintf(ttyname, "/dev/ttyE%d", i);
#elif defined(__OpenBSD__)
sprintf(ttyname, "/dev/ttyC%x", i);
#endif
if ((fd = open(ttyname, 2)) != -1)
break;
}
if (fd != -1) {
if (ioctl(fd, WSDISPLAYIO_SMODE, &mode) < 0) {
FatalError("%s: WSDISPLAYIO_MODE_MAPPED failed (%s)\n%s",
"xf86OpenConsole", strerror(errno),
CHECK_DRIVER_MSG);
}
xf86Info.consType = WSCONS;
xf86Msg(X_PROBED, "Using wscons driver\n");
}
return fd;
}
#endif
void
xf86CloseConsole()
{
#if defined(SYSCONS_SUPPORT) || defined(PCVT_SUPPORT)
struct vt_mode VT;
#endif
if (ShareVTs) return;
switch (xf86Info.consType)
{
#ifdef PCCONS_SUPPORT
case PCCONS:
ioctl (xf86Info.consoleFd, CONSOLE_X_MODE_OFF, 0);
break;
#endif
#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
case SYSCONS:
case PCVT:
ioctl(xf86Info.consoleFd, KDSETMODE, KD_TEXT);
if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) != -1)
{
VT.mode = VT_AUTO;
ioctl(xf86Info.consoleFd, VT_SETMODE, &VT);
}
#if !defined(OpenBSD) && !defined(USE_DEV_IO) && !defined(USE_I386_IOPL)
if (ioctl(xf86Info.consoleFd, KDDISABIO, 0) < 0)
{
xf86FatalError("xf86CloseConsole: KDDISABIO failed (%s)",
strerror(errno));
}
#endif
if (initialVT != -1)
ioctl(xf86Info.consoleFd, VT_ACTIVATE, initialVT);
break;
#endif
#ifdef WSCONS_SUPPORT
case WSCONS:
{
int mode = WSDISPLAYIO_MODE_EMUL;
ioctl(xf86Info.screenFd, WSDISPLAYIO_SMODE, &mode);
break;
}
#endif
}
if (xf86Info.screenFd != xf86Info.consoleFd)
{
close(xf86Info.screenFd);
close(xf86Info.consoleFd);
if ((xf86Info.consoleFd = open("/dev/console",O_RDONLY,0)) <0)
{
xf86FatalError("xf86CloseConsole: Cannot open /dev/console (%s)",
strerror(errno));
}
}
close(xf86Info.consoleFd);
if (devConsoleFd >= 0)
close(devConsoleFd);
return;
}
int
xf86ProcessArgument(int argc, char *argv[], int i)
{
if (!strcmp(argv[i], "-keeptty"))
{
KeepTty = TRUE;
return(1);
}
#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
if (!strcmp(argv[i], "-sharevts"))
{
ShareVTs = TRUE;
return(1);
}
if ((argv[i][0] == 'v') && (argv[i][1] == 't'))
{
if (sscanf(argv[i], "vt%2d", &VTnum) == 0 ||
VTnum < 1 || VTnum > 12)
{
UseMsg();
VTnum = -1;
return(0);
}
return(1);
}
#endif
return(0);
}
void
xf86UseMsg()
{
#if defined (SYSCONS_SUPPORT) || defined (PCVT_SUPPORT)
ErrorF("vtXX use the specified VT number (1-12)\n");
ErrorF("-sharevts share VTs with another X server\n");
#endif
ErrorF("-keeptty ");
ErrorF("don't detach controlling tty (for debugging only)\n");
return;
}