Beispiel #1
0
/*
 * If SIGUSR1 was set to SIG_IGN when the server started, assume that either
 *
 *  a- The parent process is ignoring SIGUSR1
 *
 * or
 *
 *  b- The parent process is expecting a SIGUSR1
 *     when the server is ready to accept connections
 *
 * In the first case, the signal will be harmless, in the second case,
 * the signal will be quite useful.
 */
static void
InitParentProcess(void)
{
#if !defined(WIN32)
    OsSigHandlerPtr handler;
    handler = OsSignal (SIGUSR1, SIG_IGN);
    if ( handler == SIG_IGN)
	RunFromSmartParent = TRUE;
    OsSignal(SIGUSR1, handler);
    ParentProcess = getppid ();
#endif
}
Beispiel #2
0
Bool
ephyrInitialize(KdCardInfo * card, EphyrPriv * priv)
{
    OsSignal(SIGUSR1, hostx_handle_signal);

    priv->base = 0;
    priv->bytes_per_line = 0;
    return TRUE;
}
Beispiel #3
0
static void block_sigio_test_nested(void)
{
#ifdef SIG_BLOCK
    /* Check for bug releasing SIGIO during SIGIO signal handling.
       test case:
           raise signal
           → in signal handler:
                raise signal
                OsBlockSignals()
                OsReleaseSignals()
                tail guard
       tail guard must be hit.
     */
    void (*old_handler)(int);
    old_handler = OsSignal(SIGIO, sighandler);
    expect_signal = 1;
    assert(raise(SIGIO) == 0);
    assert(OsSignal(SIGIO, old_handler) == sighandler);
#endif
}
/*
 * If SIGUSR1 was set to SIG_IGN when the server started, assume that either
 *
 *  a- The parent process is ignoring SIGUSR1
 *
 * or
 *
 *  b- The parent process is expecting a SIGUSR1
 *     when the server is ready to accept connections
 *
 * In the first case, the signal will be harmless, in the second case,
 * the signal will be quite useful.
 */
static void
InitParentProcess(void)
{
#if !defined(WIN32)
    OsSigHandlerPtr handler;
    handler = OsSignal (SIGUSR1, SIG_IGN);
    if ( handler == SIG_IGN)
	RunFromSmartParent = TRUE;
    OsSignal(SIGUSR1, handler);
    ParentProcess = getppid ();
#ifdef __UNIXOS2__
    /*
     * fg030505: under OS/2, xinit is not the parent process but
     * the "grant parent" process of the server because execvpe()
     * presents us an additional process number;
     * GetPPID(pid) is part of libemxfix
     */
    ParentProcess = GetPPID (ParentProcess);
#endif /* __UNIXOS2__ */
#endif
}
Beispiel #5
0
static void
InstallSignalHandlers(void)
{
    /*
     * Install signal handler for unexpected signals
     */
    xf86Info.caughtSignal = FALSE;
    if (!xf86Info.notrapSignals) {
        OsRegisterSigWrapper(xf86SigWrapper);
    }
    else {
        OsSignal(SIGSEGV, SIG_DFL);
        OsSignal(SIGILL, SIG_DFL);
#ifdef SIGEMT
        OsSignal(SIGEMT, SIG_DFL);
#endif
        OsSignal(SIGFPE, SIG_DFL);
        OsSignal(SIGBUS, SIG_DFL);
        OsSignal(SIGSYS, SIG_DFL);
        OsSignal(SIGXCPU, SIG_DFL);
        OsSignal(SIGXFSZ, SIG_DFL);
    }
}
Beispiel #6
0
void
OsVendorInit(void)
{
    static Bool beenHere = FALSE;

    OsSignal(SIGCHLD, SIG_DFL);   /* Need to wait for child processes */

    if (!beenHere) {
        umask(022);
        xf86LogInit();
    }

    /* Set stderr to non-blocking. */
#ifndef O_NONBLOCK
#if defined(FNDELAY)
#define O_NONBLOCK FNDELAY
#elif defined(O_NDELAY)
#define O_NONBLOCK O_NDELAY
#endif

#ifdef O_NONBLOCK
    if (!beenHere) {
        if (xf86PrivsElevated()) {
            int status;

            status = fcntl(fileno(stderr), F_GETFL, 0);
            if (status != -1) {
                fcntl(fileno(stderr), F_SETFL, status | O_NONBLOCK);
            }
        }
    }
#endif
#endif

    beenHere = TRUE;
}
Beispiel #7
0
void
xf86OpenConsole(void)
{
  int i, ioctl_ret;
  struct vt_mode VT;
  struct vid_info vidinf;
  struct sigaction sigvtsw;
  char *ttn;

  if (serverGeneration == 1) {
    /* check if we're run with euid==0 */
    if (geteuid() != 0) {
      FatalError("xf86OpenConsole: Server must be setuid root\n");
    }

    /* If we are run in the background we will get SIGTTOU. Ignore it. */
    OsSignal (SIGTTOU, SIG_IGN);

    /*
     * Set up the virtual terminal (multiscreen in SCO parlance).
     * For the actual console itself, screens are numbered from
     * 1 to (usually) 16. However, it is possible to have a nested
     * server, and it is also possible to be on a multi-console
     * system such as MaxSpeed or SunRiver. Therefore, we should
     * not make any assumptions about the TTY name we are on, and
     * instead we rely on ttyname() to give us the real TTY name.
     * Previously, we tried to determine the TTY name manually.
     * This is wrong. The only time we need to futz with the TTY name
     * is if we were given the name of a TTY to run on explicity on
     * the command line.
     */

    if (VTnum == -1) {
      /*
       * No device was specified. We need to query the kernel to see which
       * console device we are on (and in fact if we are on a console at all).
       */
      ttn = ttyname (1);

      if (ttn == (char *)0) {
	FatalError ("xf86OpenConsole: Could not determine TTY name: %s\n",
	  strerror(errno));
      }
      strlcpy (vtdevice, ttn, sizeof(vtdevice));
    } else if (VTnum >= 0) {
      snprintf (vtdevice, sizeof(vtdevice), "/dev/tty%02d", VTnum);
    }

    /*
     * Now we can dispose of stdin/stdout
     */
    fclose (stdin);
    fclose (stdout);

    if ((xf86Info.consoleFd = open(vtdevice, O_RDWR | O_NDELAY, 0)) < 0) {
      FatalError("xf86OpenConsole: Cannot open %s: %s\n", vtdevice,
	strerror(errno));
    }

    /*
     * We make 100% sure we use the correct VT number. This can get ugly
     * where there are multi-consoles in use, so we make sure we query
     * the kernel for the correct VT number. It knows best, we don't.
     */
    vidinf.size = sizeof(vidinf);
    if (ioctl (xf86Info.consoleFd, CONS_GETINFO, &vidinf) < 0) {
      FatalError ("xf86OpenConsole: %s not a console device "
	"or error querying device: %s\n", vtdevice, strerror (errno));
    }
    xf86Info.vtno = vidinf.m_num;
    VTnum = vidinf.m_num + 1; /* 0-based */

    ErrorF("(using VT%02d device %s)\n\n", VTnum, vtdevice);

    /* We activate the console just in case its not the one we are on */
    if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) != 0) {
        ErrorF("xf86OpenConsole: VT_ACTIVATE failed (%s)\n", strerror(errno));
    }

    /* Disassociate from controling TTY */
    if (!KeepTty) {
      setpgrp();
    }

    /*
     * Now we get the current mode that the console device is in. We will
     * use this later when we close the console device to restore it to
     * that same mode.
     */
    if ((sco_console_mode = ioctl(xf86Info.consoleFd, CONS_GET, 0L)) < 0) {
      FatalError("xf86OpenConsole: CONS_GET failed on console (%s)\n",
          strerror(errno));
    }

    if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) < 0) {
      FatalError("xf86OpenConsole: VT_GETMODE failed (%s)\n", strerror(errno));
    }

    sigvtsw.sa_handler = xf86VTRequest;
    sigfillset(&sigvtsw.sa_mask);
    sigvtsw.sa_flags = 0;

    /* NOTE: Using sigaction means we dont have to re-arm the signal */
    sigaction(SIGUSR1, &sigvtsw, NULL);

    VT.mode = VT_PROCESS;
    VT.relsig = SIGUSR1;
    VT.acqsig = SIGUSR1;
    VT.frsig = SIGINT;          /* Not implemented */
    VT.waitv = 0;

    /*
     * The SCO X server tries the following call 5 times. Lets do the same
     * thing. It shouldn't really be required but sometimes things take a
     * while to settle down when switching screens. *helpless shrug* I know
     * its sucks but ...
     */

    ioctl_ret = 0;
    for (i = 0; i < 5; i++) {
      ioctl_ret = ioctl(xf86Info.consoleFd, VT_SETMODE, &VT);
      if (ioctl_ret >= 0)
        break;
      usleep(999999); /* Dont use nap() - it forces linking with -lx */
    }

    if (ioctl_ret < 0) {
      FatalError("xf86OpenConsole: VT_SETMODE failed (%s)\n", strerror(errno));
    }

    /*
     * Convince the console driver we are in graphics mode.
     */
    if (ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS) < 0) {
        ErrorF("Failed to set graphics mode (%s)\n", strerror(errno));
    }
  } else { /* serverGeneration != 1 */
    if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) != 0) {
      ErrorF("xf86OpenConsole: VT_ACTIVATE failed (%s)\n", strerror(errno));
    }
  }
}
Beispiel #8
0
void
CreateWellKnownSockets(void)
{
    int		i;
    int		partial;
    char 	port[20];

    FD_ZERO(&AllSockets);
    FD_ZERO(&AllClients);
    FD_ZERO(&LastSelectMask);
    FD_ZERO(&ClientsWithInput);

#if !defined(WIN32)
    for (i=0; i<MaxClients; i++) ConnectionTranslation[i] = 0;
#else
    ClearConnectionTranslation();
#endif

    FD_ZERO (&WellKnownConnections);

    sprintf (port, "%d", atoi (display));

    if ((_XSERVTransMakeAllCOTSServerListeners (port, &partial,
	&ListenTransCount, &ListenTransConns) >= 0) &&
	(ListenTransCount >= 1))
    {
	if (!PartialNetwork && partial)
	{
	    FatalError ("Failed to establish all listening sockets");
	}
	else
	{
	    ListenTransFds = xalloc (ListenTransCount * sizeof (int));

	    for (i = 0; i < ListenTransCount; i++)
	    {
		int fd = _XSERVTransGetConnectionNumber (ListenTransConns[i]);
		
		ListenTransFds[i] = fd;
		FD_SET (fd, &WellKnownConnections);

		if (!_XSERVTransIsLocal (ListenTransConns[i]))
		{
		    DefineSelf (fd);
		}
	    }
	}
    }

    if (!XFD_ANYSET (&WellKnownConnections))
        FatalError ("Cannot establish any listening sockets - Make sure an X server isn't already running");
#if !defined(WIN32)
    OsSignal (SIGPIPE, SIG_IGN);
    OsSignal (SIGHUP, AutoResetServer);
#endif
    OsSignal (SIGINT, GiveUp);
    OsSignal (SIGTERM, GiveUp);
    XFD_COPYSET (&WellKnownConnections, &AllSockets);
    ResetHosts(display);

    InitParentProcess();

#ifdef XDMCP
    XdmcpInit ();
#endif
}
Beispiel #9
0
void
CreateWellKnownSockets(void)
{
    int i;
    int partial;

    FD_ZERO(&AllSockets);
    FD_ZERO(&AllClients);
    FD_ZERO(&LastSelectMask);
    FD_ZERO(&ClientsWithInput);

#if !defined(WIN32)
    for (i = 0; i < MaxClients; i++)
        ConnectionTranslation[i] = 0;
#else
    ClearConnectionTranslation();
#endif

    FD_ZERO(&WellKnownConnections);

    /* display is initialized to "0" by main(). It is then set to the display
     * number if specified on the command line. */

    if (NoListenAll) {
        ListenTransCount = 0;
    }
    else if ((displayfd < 0) || explicit_display) {
        if (TryCreateSocket(atoi(display), &partial) &&
            ListenTransCount >= 1)
            if (!PartialNetwork && partial)
                FatalError ("Failed to establish all listening sockets");
    }
    else { /* -displayfd and no explicit display number */
        Bool found = 0;
        for (i = 0; i < 65536 - X_TCP_PORT; i++) {
            if (TryCreateSocket(i, &partial) && ListenTransCount >= 1 && (PartialNetwork || !partial)) {
                found = 1;
                break;
            }
            else
                CloseWellKnownConnections();
        }
        if (!found)
            FatalError("Failed to find a socket to listen on");
        dynamic_display_id=i;
        snprintf(dynamic_display, sizeof(dynamic_display), "%d", i);
        display = dynamic_display;
    }

    ListenTransFds = xallocarray(ListenTransCount, sizeof (int));
    if (ListenTransFds == NULL)
        FatalError ("Failed to create listening socket array");

    for (i = ListenTransCount; i > 0; i--) {
        int fd = _XSERVTransGetConnectionNumber (ListenTransConns[i-1]);

        ListenTransFds[i-1] = fd;
        FD_SET(fd, &WellKnownConnections);

        if (!_XSERVTransIsLocal (ListenTransConns[i-1])) {
            int protocol = 0;
            if (!strcmp("inet", ListenTransConns[i-1]->transptr->TransName)) protocol = 4;
            else if (!strcmp("inet6", ListenTransConns[i-1]->transptr->TransName)) protocol = 6;
            DefineSelf (fd, protocol);
        }
    }

    if (!XFD_ANYSET(&WellKnownConnections) && !NoListenAll)
        FatalError
            ("Cannot establish any listening sockets - Make sure an X server isn't already running");

#if !defined(WIN32)
    OsSignal(SIGPIPE, SIG_IGN);
    OsSignal(SIGHUP, AutoResetServer);
#endif
    OsSignal(SIGINT, GiveUp);
    OsSignal(SIGTERM, GiveUp);
    XFD_COPYSET(&WellKnownConnections, &AllSockets);
    ResetHosts(display);

    InitParentProcess();

#ifdef XDMCP
    XdmcpInit();
#endif
}
Beispiel #10
0
void
xf86OpenConsole(void)
{
    int i, ret;
    struct vt_stat vts;
    struct vt_mode VT;
    const char *vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };

    if (serverGeneration == 1) {
        linux_parse_vt_settings(FALSE);

        if (!KeepTty) {
            pid_t ppid = getppid();
            pid_t ppgid;

            ppgid = getpgid(ppid);

            /*
             * change to parent process group that pgid != pid so
             * that setsid() doesn't fail and we become process
             * group leader
             */
            if (setpgid(0, ppgid) < 0)
                xf86Msg(X_WARNING, "xf86OpenConsole: setpgid failed: %s\n",
                        strerror(errno));

            /* become process group leader */
            if ((setsid() < 0))
                xf86Msg(X_WARNING, "xf86OpenConsole: setsid failed: %s\n",
                        strerror(errno));
        }

        i = 0;
        while (vcs[i] != NULL) {
            snprintf(vtname, sizeof(vtname), vcs[i], xf86Info.vtno);    /* /dev/tty1-64 */
            if ((xf86Info.consoleFd = open(vtname, O_RDWR | O_NDELAY, 0)) >= 0)
                break;
            i++;
        }

        if (xf86Info.consoleFd < 0)
            FatalError("xf86OpenConsole: Cannot open virtual console"
                       " %d (%s)\n", xf86Info.vtno, strerror(errno));

        /*
         * Linux doesn't switch to an active vt after the last close of a vt,
         * so we do this ourselves by remembering which is active now.
         */
        SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_GETSTATE, &vts));
        if (ret < 0)
            xf86Msg(X_WARNING, "xf86OpenConsole: VT_GETSTATE failed: %s\n",
                    strerror(errno));
        else
            activeVT = vts.v_active;

        if (!xf86Info.ShareVTs) {
            struct termios nTty;

            /*
             * now get the VT.  This _must_ succeed, or else fail completely.
             */
            if (!switch_to(xf86Info.vtno, "xf86OpenConsole"))
                FatalError("xf86OpenConsole: Switching VT failed\n");

            SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_GETMODE, &VT));
            if (ret < 0)
                FatalError("xf86OpenConsole: VT_GETMODE failed %s\n",
                           strerror(errno));

            OsSignal(SIGUSR1, xf86VTRequest);

            VT.mode = VT_PROCESS;
            VT.relsig = SIGUSR1;
            VT.acqsig = SIGUSR1;

            SYSCALL(ret = ioctl(xf86Info.consoleFd, VT_SETMODE, &VT));
            if (ret < 0)
                FatalError
                    ("xf86OpenConsole: VT_SETMODE VT_PROCESS failed: %s\n",
                     strerror(errno));

            SYSCALL(ret = ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS));
            if (ret < 0)
                FatalError("xf86OpenConsole: KDSETMODE KD_GRAPHICS failed %s\n",
                           strerror(errno));

            tcgetattr(xf86Info.consoleFd, &tty_attr);
            SYSCALL(ioctl(xf86Info.consoleFd, KDGKBMODE, &tty_mode));

            /* disable kernel special keys and buffering, new style */
            SYSCALL(ret = ioctl(xf86Info.consoleFd, KDSKBMUTE, 1));
            if (ret < 0)
            {
                /* disable kernel special keys and buffering, old style */
                SYSCALL(ret = ioctl(xf86Info.consoleFd, KDSKBMODE, K_OFF));
                if (ret < 0)
                {
                    /* fine, just disable special keys */
                    SYSCALL(ret = ioctl(xf86Info.consoleFd, KDSKBMODE, K_RAW));
                    if (ret < 0)
                        FatalError("xf86OpenConsole: KDSKBMODE K_RAW failed %s\n",
                                   strerror(errno));

                    /* ... and drain events, else the kernel gets angry */
                    xf86SetConsoleHandler(drain_console, NULL);
                }
            }

            nTty = tty_attr;
            nTty.c_iflag = (IGNPAR | IGNBRK) & (~PARMRK) & (~ISTRIP);
            nTty.c_oflag = 0;
            nTty.c_cflag = CREAD | CS8;
            nTty.c_lflag = 0;
            nTty.c_cc[VTIME] = 0;
            nTty.c_cc[VMIN] = 1;
            cfsetispeed(&nTty, 9600);
            cfsetospeed(&nTty, 9600);
            tcsetattr(xf86Info.consoleFd, TCSANOW, &nTty);
        }
    }
    else {                      /* serverGeneration != 1 */
        if (!xf86Info.ShareVTs && xf86Info.autoVTSwitch) {
            /* now get the VT */
            if (!switch_to(xf86Info.vtno, "xf86OpenConsole"))
                FatalError("xf86OpenConsole: Switching VT failed\n");
        }
    }
}
Beispiel #11
0
void
CreateWellKnownSockets(void)
{
    int i;
    int partial;

    FD_ZERO(&AllSockets);
    FD_ZERO(&AllClients);
    FD_ZERO(&LastSelectMask);
    FD_ZERO(&ClientsWithInput);

#if !defined(WIN32)
    for (i = 0; i < MaxClients; i++)
        ConnectionTranslation[i] = 0;
#else
    ClearConnectionTranslation();
#endif

    FD_ZERO(&WellKnownConnections);

    /* display is initialized to "0" by main(). It is then set to the display
     * number if specified on the command line, or to NULL when the -displayfd
     * option is used. */
    if (display) {
        if (TryCreateSocket(atoi(display), &partial) &&
            ListenTransCount >= 1)
            if (!PartialNetwork && partial)
                FatalError ("Failed to establish all listening sockets");
    }
    else { /* -displayfd */
        Bool found = 0;
        for (i = 0; i < 65535 - X_TCP_PORT; i++) {
            if (TryCreateSocket(i, &partial) && !partial) {
                found = 1;
                break;
            }
            else
                CloseWellKnownConnections();
        }
        if (!found)
            FatalError("Failed to find a socket to listen on");
        snprintf(dynamic_display, sizeof(dynamic_display), "%d", i);
        display = dynamic_display;
    }

    ListenTransFds = malloc(ListenTransCount * sizeof (int));

    for (i = 0; i < ListenTransCount; i++) {
        int fd = _XSERVTransGetConnectionNumber(ListenTransConns[i]);

        ListenTransFds[i] = fd;
        FD_SET(fd, &WellKnownConnections);

        if (!_XSERVTransIsLocal(ListenTransConns[i]))
            DefineSelf (fd);
    }

    if (!XFD_ANYSET(&WellKnownConnections))
        FatalError
            ("Cannot establish any listening sockets - Make sure an X server isn't already running");
#if !defined(WIN32)
    OsSignal(SIGPIPE, SIG_IGN);
    OsSignal(SIGHUP, AutoResetServer);
#endif
    OsSignal(SIGINT, GiveUp);
    OsSignal(SIGTERM, GiveUp);
    XFD_COPYSET(&WellKnownConnections, &AllSockets);
    ResetHosts(display);

    InitParentProcess();

#ifdef XDMCP
    XdmcpInit();
#endif
}
Beispiel #12
0
void
xf86OpenConsole(void)
{
  int fd, i, ioctl_ret;
  struct vt_mode VT;
  struct vt_stat vts;
  MessageType from = X_PROBED;
  struct sigaction sigvtsw;
  char *ttn;

  if (serverGeneration == 1) {
    int isconsole = 0, consdev = 0;

    /* check if we're run with euid==0 */
    if (geteuid() != 0) {
      FatalError("xf86OpenConsole: Server must be suid root\n");
    }

    /* If we are run in the background we will get SIGTTOU. Ignore it. */
    OsSignal (SIGTTOU, SIG_IGN);

    /* Protect page 0 to help find NULL dereferencing */
    /* mprotect() doesn't seem to work */
    if (Protect0) {
      int fd = -1;

      if ((fd = open("/dev/zero", O_RDONLY, 0)) < 0) {
	xf86Msg(X_WARNING, "xf86OpenConsole: cannot open /dev/zero (%s)\n",
	  strerror(errno));
      } else {
	if ((int)mmap(0, 0x1000, PROT_NONE,
	    MAP_FIXED | MAP_SHARED, fd, 0) == -1) {
	  xf86Msg(X_WARNING, "xf86OpenConsole: failed to protect page 0 (%s)\n",
	    strerror(errno));
	}
	close(fd);
      }
    }

    /*
     * setup the virtual terminal manager
     */
    if (VTnum == -1) {
      /*
       * No device was specified. We need to query the kernel to see which
       * console device we are on (and in fact if we are on a console at all).
       */
      if (ioctl (0, VT_GETSTATE, &vts) < 0) {
	FatalError("xf86OpenConsole: Could not query active VT: %s\n",
	  strerror(errno));
      }
      VTnum = vts.v_active;
      if (is_osr6())
	snprintf (vtdevice, sizeof(vtdevice), "/dev/tty%02d", VTnum + 1);
      else
	snprintf (vtdevice, sizeof(vtdevice), "/dev/vt%02d", VTnum);
    } else {
      from = X_CMDLINE;
      if (is_osr6())
	snprintf (vtdevice, sizeof(vtdevice), "/dev/tty%02d", VTnum + 1);
      else
	snprintf (vtdevice, sizeof(vtdevice), "/dev/vt%02d", VTnum);
    }

    if (IsConsoleDevice(vtdevice)) {
      isconsole = 1;
      CRTSpecified = FALSE;	/* Dont honour -crt /dev/console */
    }

    if (ioctl (0, KIOCINFO, 0) >= 0)
      consdev = 1 + isconsole;

    if ((!CRTSpecified) && (isconsole || (!consdev))) {
      /*
       * Need to find a free VT
       */
      if ((fd = open ("/dev/console", O_WRONLY | O_NOCTTY)) < 0) {
	FatalError ("xf86OpenConsole: Could not open /dev/console: %s\n",
	  strerror (errno));
      }

      if (ioctl (fd, VT_OPENQRY, &VTnum) < 0) {
	FatalError ("xf86OpenConsole: Cannot find a free VT: %s\n",
	  strerror(errno));
      }
      close (fd);
      if (usl_is_osr6)
	snprintf (vtdevice, sizeof(vtdevice), "/dev/tty%02d", VTnum + 1);
      else
	snprintf (vtdevice, sizeof(vtdevice), "/dev/vt%02d", VTnum);
    }

    /*
     * Now we can dispose of stdin/stdout
     */
    fclose (stdin);
    fclose (stdout);

    if (CRTSpecified || isconsole || consdev != 1) {
      if (!KeepTty) {
	setpgrp();
      }
    }

    if ((xf86Info.consoleFd = open(vtdevice, O_RDWR | O_NONBLOCK, 0)) < 0) {
      FatalError("xf86OpenConsole: Cannot open %s: %s\n", vtdevice,
	strerror(errno));
    }

    xf86Msg (from, "using VT number %d (%s)\n\n", VTnum, vtdevice);
    xf86Info.vtno = VTnum;

    /* change ownership of the vt */
    chown(vtdevice, getuid(), getgid());

    /*
     * now get the VT
     */
    if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) != 0) {
      FatalError("xf86OpenConsole: VT_ACTIVATE failed: %s\n",
	strerror(errno));
    }
    if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) != 0) {
      FatalError("xf86OpenConsole: VT_WAITACTIVE failed: %s\n",strerror(errno));
    }

    if (ioctl(xf86Info.consoleFd, VT_GETMODE, &VT) < 0) {
      FatalError("xf86OpenConsole: VT_GETMODE failed: %s\n", strerror(errno));
    }

    sigvtsw.sa_handler = xf86VTRequest;
    sigfillset(&sigvtsw.sa_mask);
    sigvtsw.sa_flags = 0;
    sigaction(SIGUSR1, &sigvtsw, NULL);

    VT.mode = VT_PROCESS;
    VT.relsig = SIGUSR1;
    VT.acqsig = SIGUSR1;

    ioctl_ret = ioctl(xf86Info.consoleFd, VT_SETMODE, &VT);
    if (ioctl_ret < 0) {
      FatalError("xf86OpenConsole: VT_SETMODE failed: %s\n", strerror(errno));
    }

    if (ioctl(xf86Info.consoleFd, KDSETMODE, KD_GRAPHICS) < 0) {
      FatalError("xf86OpenConsole: KD_GRAPHICS failed: %s\n", strerror(errno));
    }
  } else { /* serverGeneration != 1 */
    /*
     * now get the VT
     */
    if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) != 0) {
      FatalError("xf86OpenConsole: VT_ACTIVATE failed: %s\n", strerror(errno));
    }
    if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) != 0) {
      FatalError("xf86OpenConsole: VT_WAITACTIVE failed: %s\n",strerror(errno));
    }
    /*
     * If the server doesn't have the VT when the reset occurs,
     * this is to make sure we don't continue until the activate
     * signal is received.
     */
    if (!xf86Screens[0]->vtSema)
      sleep(5);
  }
  return;
}