Example #1
0
PMClose
xf86OSPMOpen(void)
{
#ifndef __OpenBSD__
    int kq;
    struct kevent ev;

    if (APMihPtr || !xf86Info.pmFlag) {
        return NULL;
    }
    if ((devFd = open(_PATH_APM_DEV, O_RDONLY)) == -1) {
        return NULL;
    }
    if ((kq = kqueue()) <= 0) {
        close(devFd);
        return NULL;
    }
    EV_SET(&ev, devFd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, NULL);
    if (kevent(kq, &ev, 1, NULL, 0, NULL) < 0) {
        close(devFd);
        return NULL;
    }

    xf86PMGetEventFromOs = bsdPMGetEventFromOS;
    xf86PMConfirmEventToOs = bsdPMConfirmEventToOs;
    APMihPtr = xf86AddGeneralHandler(kq, xf86HandlePMEvents, NULL);
#endif
    return bsdCloseAPM;
}
static PMClose
lnxAPMOpen(void)
{
    int fd, pfd;

    DebugF("APM: OSPMOpen called\n");
    if (APMihPtr || !xf86Info.pmFlag)
        return NULL;

    DebugF("APM: Opening device\n");
    if ((fd = open(APM_DEVICE, O_RDWR)) > -1) {
        if (access(APM_PROC, R_OK) || ((pfd = open(APM_PROC, O_RDONLY)) == -1)) {
            xf86MsgVerb(X_WARNING, 3, "Cannot open APM (%s) (%s)\n",
                        APM_PROC, strerror(errno));
            close(fd);
            return NULL;
        }
        else
            close(pfd);
        xf86PMGetEventFromOs = lnxPMGetEventFromOs;
        xf86PMConfirmEventToOs = lnxPMConfirmEventToOs;
        APMihPtr = xf86AddGeneralHandler(fd, xf86HandlePMEvents, NULL);
        xf86MsgVerb(X_INFO, 3, "Open APM successful\n");
        return lnxCloseAPM;
    }
    return NULL;
}
Example #3
0
/**
 * Set the handler for the console's fd. Replaces (and returns) the previous
 * handler or NULL, whichever appropriate.
 * proc may be NULL if the server should not handle events on the console.
 */
InputHandlerProc
xf86SetConsoleHandler(InputHandlerProc proc, pointer data)
{
    static IHPtr handler = NULL;
    IHPtr old_handler = handler;

    if (old_handler)
        xf86RemoveGeneralHandler(old_handler);

    handler = xf86AddGeneralHandler(xf86Info.consoleFd, proc, data);

    return (old_handler) ? old_handler->ihproc : NULL;
}
void
xf86OpenConsole(void)
{
    int i, fd = -1;
    struct vt_mode VT;
    struct vt_stat vts;
    MessageType from = X_PROBED;
    char *tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
    char *vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };

    if (serverGeneration == 1) {

	/* when KeepTty check if we're run with euid==0 */
	if (KeepTty && geteuid() != 0) 
	    FatalError("xf86OpenConsole:"
		       " Server must be suid root for option \"KeepTTY\"\n");

	/*
	 * setup the virtual terminal manager
	 */
	if (VTnum != -1) {
	    xf86Info.vtno = VTnum;
	    from = X_CMDLINE;
	} else {

	    i=0;
	    while (tty0[i] != NULL) {
		if ((fd = open(tty0[i],O_WRONLY,0)) >= 0)
		  break;
		i++;
	    }
	    
	    if (fd < 0)
		FatalError(
		    "xf86OpenConsole: Cannot open /dev/tty0 (%s)\n",
		    strerror(errno));

            if (ShareVTs)
            {
                if (ioctl(fd, VT_GETSTATE, &vts) == 0)
                    xf86Info.vtno = vts.v_active;
                else
                    FatalError("xf86OpenConsole: Cannot find the current"
                               " VT (%s)\n", strerror(errno));
            } else {
	        if ((ioctl(fd, VT_OPENQRY, &xf86Info.vtno) < 0) ||
		    (xf86Info.vtno == -1))
		    FatalError("xf86OpenConsole: Cannot find a free VT: %s\n",
                               strerror(errno));
            }
	    close(fd);
	}

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

	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) {
            sprintf(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));

        if (!ShareVTs)
        {
	    /*
	     * Grab the vt ownership before we overwrite it.
	     * Hard coded /dev/tty0 into this function as well for below.
	     */
	    if (!saveVtPerms())
	        xf86Msg(X_WARNING,
		        "xf86OpenConsole: Could not save ownership of VT\n");

	    if (geteuid() == 0) {
		    /* change ownership of the vt */
		    if (chown(vtname, getuid(), getgid()) < 0)
			    xf86Msg(X_WARNING,"xf86OpenConsole: chown %s failed: %s\n",
				    vtname, strerror(errno));

		    /*
		     * the current VT device we're running on is not
		     * "console", we want to grab all consoles too
		     *
		     * Why is this needed??
		     */
		    if (chown("/dev/tty0", getuid(), getgid()) < 0)
			    xf86Msg(X_WARNING,"xf86OpenConsole: chown /dev/tty0 failed: %s\n",
				    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.
	 */
	if (ioctl(xf86Info.consoleFd, VT_GETSTATE, &vts) < 0)
	    xf86Msg(X_WARNING,"xf86OpenConsole: VT_GETSTATE failed: %s\n",
		    strerror(errno));
	else
	    activeVT = vts.v_active;

#if 0
	if (!KeepTty) {
	    /*
	     * Detach from the controlling tty to avoid char loss
	     */
	    if ((i = open("/dev/tty",O_RDWR)) >= 0) {
		ioctl(i, TIOCNOTTY, 0);
		close(i);
	    }
	}
#endif

        if (!ShareVTs)
        {
            struct termios nTty;

	    /*
	     * now get the VT.  This _must_ succeed, or else fail completely.
	     */
	    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));

	    signal(SIGUSR1, xf86VTRequest);

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

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

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

            if (ioctl(xf86Info.consoleFd, KDSKBMODE, K_RAW) < 0)
                FatalError("xf86OpenConsole: KDSKBMODE K_RAW failed %s\n",
                        strerror(errno));

            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);

            /* need to keep the buffer clean, else the kernel gets angry */
            console_handler = xf86AddGeneralHandler(xf86Info.consoleFd,
                    drain_console, NULL);

	    /* we really should have a InitOSInputDevices() function instead
	     * of Init?$#*&Device(). So I just place it here */
        }
    } else { 	/* serverGeneration != 1 */
        if (!ShareVTs && VTSwitch)
        {
	    /*
	     * now get the VT
	     */
	    if (ioctl(xf86Info.consoleFd, VT_ACTIVATE, xf86Info.vtno) < 0)
	        xf86Msg(X_WARNING, "xf86OpenConsole: VT_ACTIVATE failed %s\n",
		        strerror(errno));

	    if (ioctl(xf86Info.consoleFd, VT_WAITACTIVE, xf86Info.vtno) < 0)
	        xf86Msg(X_WARNING, "xf86OpenConsole: VT_WAITACTIVE failed %s\n",
		        strerror(errno));
        }
    }
}