File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / pty.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Mon Oct 31 13:53:51 2011 UTC (12 years, 8 months ago) by misho
Branches: MAIN
CVS tags: io2_5, io2_4, io2_3, io2_2, IO2_4, IO2_3, IO2_2, IO2_1, HEAD
ver 2.1

    1: /*************************************************************************
    2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: pty.c,v 1.2 2011/10/31 13:53:51 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: 
   48: 
   49: /*
   50:  * ioAllocPTY() Allocate new PTY and TTY
   51:  * @ptyfd = master fd, pty
   52:  * @ttyfd = slave fd, tty
   53:  * @name = tty device name if not null
   54:  * @namesiz = name length, must be above 63 bytes.
   55:  * @term = termios for terminal
   56:  * @winz = winsize for terminal
   57:  * return: -1 error or 0 ok
   58:  */
   59: inline int
   60: ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz, 
   61: 		struct termios * __restrict term, struct winsize * __restrict winz)
   62: {
   63: 	assert(ptyfd && ttyfd);
   64: 	if (!ptyfd || !ttyfd || (name && namesiz < 64)) {
   65: 		io_SetErr(EINVAL, "Error:: invalid arguments ...");
   66: 		return -1;
   67: 	}
   68: 
   69: 	memset(name, 0, namesiz);
   70: 	if (openpty(ptyfd, ttyfd, name, term, winz) == -1) {
   71: 		LOGERR;
   72: 		return -1;
   73: 	}
   74: 
   75: 	return 0;
   76: }
   77: 
   78: /*
   79:  * ioFreePTY() Release PTY and TTY device
   80:  * @ptyfd = master fd, pty (==-1 skip closing pty)
   81:  * @ttyname = tty filename
   82:  * return: none
   83:  */
   84: inline void
   85: ioFreePTY(int ptyfd, const char *ttyname)
   86: {
   87: 	assert(ttyname);
   88: 	if (!ttyname)
   89: 		return;
   90: 
   91: 	if (ptyfd != -1)
   92: 		close(ptyfd);
   93: 	if (ttyname) {
   94: 		chown(ttyname, (uid_t) 0, (gid_t) 0);
   95: 		chmod(ttyname, (mode_t) 0666);
   96: 	}
   97: }
   98: 
   99: /*
  100:  * ioChgWinPTY() Change window size of PTY
  101:  * @ptyfd = master fd, pty
  102:  * @row = row
  103:  * @col = col
  104:  * @xpxl = x pixels
  105:  * @ypxl = y pixels
  106:  * return: -1 error or 0 ok
  107:  */
  108: inline int
  109: ioChgWinPTY(int ptyfd, u_short row, u_short col, u_short xpxl, u_short ypxl)
  110: {
  111: 	struct winsize w;
  112: 
  113: 	w.ws_row = row;
  114: 	w.ws_col = col;
  115: 	w.ws_xpixel = xpxl;
  116: 	w.ws_ypixel = ypxl;
  117: 
  118: 	if (ioctl(ptyfd, TIOCSWINSZ, &w) == -1) {
  119: 		LOGERR;
  120: 		return -1;
  121: 	}
  122: 
  123: 	return 0;
  124: }
  125: 
  126: /*
  127:  * ioSetOwnerTTY() Set owner to TTY
  128:  * @ttyname = tty filename
  129:  * @UID = uid
  130:  * @GID = gid
  131:  * return: -1 error or 0 ok
  132:  */
  133: int
  134: ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GID)
  135: {
  136: 	struct group *grp;
  137: 	gid_t gid;
  138: 	mode_t mode;
  139: 	struct stat st;
  140: 
  141: 	assert(ttyname);
  142: 	if (!ttyname) {
  143: 		io_SetErr(EINVAL, "Error:: invalid arguments ...");
  144: 		return -1;
  145: 	}
  146: 
  147: 	grp = getgrnam("tty");
  148: 	if (!grp) {
  149: 		gid = GID;
  150: 		mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
  151: 	} else {
  152: 		gid = grp->gr_gid;
  153: 		mode = S_IRUSR | S_IWUSR | S_IWGRP;
  154: 	}
  155: 
  156: 	if (stat(ttyname, &st) == -1) {
  157: 		LOGERR;
  158: 		return -1;
  159: 	}
  160: 
  161: 	if (st.st_uid != UID || st.st_gid != gid)
  162: 		if (chown(ttyname, UID, gid) == -1) {
  163: 			LOGERR;
  164: 			return -1;
  165: 		}
  166: 	if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode)
  167: 		if (chmod(ttyname, mode) == -1) {
  168: 			LOGERR;
  169: 			return -1;
  170: 		}
  171: 
  172: 	return 0;
  173: }
  174: 
  175: /*
  176:  * ioSetSidTTY() Makes the process's controlling TTY and sets it to sane modes.
  177:  * @ttyfd = slave fd, tty
  178:  * @ttyname = tty filename
  179:  * return: -1 error or 0 ok
  180:  */
  181: int
  182: ioSetSidTTY(int *ttyfd, const char *ttyname)
  183: {
  184: 	int fd;
  185: 
  186: 	/* First disconnect from the old controlling tty. */
  187: #ifdef TIOCNOTTY
  188: 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
  189: 	if (fd >= 0) {
  190: 		ioctl(fd, TIOCNOTTY, NULL);
  191: 		close(fd);
  192: 	}
  193: #endif
  194: 	setsid();
  195: 
  196: 	/* Verify that we are successfully disconnected from the controlling tty. */
  197: 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
  198: 	if (fd >= 0) {
  199: 		io_SetErr(ENXIO, "Error:: Failed to disconnect from controlling tty.");
  200: 		close(fd);
  201: 		return -1;
  202: 	}
  203: 	/* Make it our controlling tty. */
  204: #ifdef TIOCSCTTY
  205: 	if (ioctl(*ttyfd, TIOCSCTTY, NULL) == -1) {
  206: 		LOGERR;
  207: 		return -1;
  208: 	}
  209: #endif
  210: 	fd = open(ttyname, O_RDWR);
  211: 	if (fd == -1) {
  212: 		LOGERR;
  213: 		return -1;
  214: 	} else
  215: 		close(fd);
  216: 
  217: 	/* Verify that we now have a controlling tty. */
  218: 	fd = open(_PATH_TTY, O_WRONLY);
  219: 	if (fd == -1) {
  220: 		LOGERR;
  221: 		return -1;
  222: 	} else
  223: 		close(fd);
  224: 
  225: 	/* redirect standart handles to tty */
  226: 	dup2(*ttyfd, STDIN_FILENO);
  227: 	dup2(*ttyfd, STDOUT_FILENO);
  228: 	dup2(*ttyfd, STDERR_FILENO);
  229: 	if (*ttyfd > 2)
  230: 		close(*ttyfd);
  231: 
  232: 	/* NOW TTY IS READY! */
  233: 	return 0;
  234: }
  235: 
  236: /*
  237:  * ioSetRAWMode() Enter into RAW mode
  238:  * @fd = tty fd
  239:  * @otio = saved old termios for later restore if !=NULL
  240:  * return: -1 error or 0 ok
  241:  */
  242: inline int
  243: ioSetRAWMode(int fd, struct termios *otio)
  244: {
  245: 	struct termios tio;
  246: 
  247: 	if (tcgetattr(fd, &tio) == -1) {
  248: 		LOGERR;
  249: 		return -1;
  250: 	}
  251: 	if (otio)
  252: 		*otio = tio;
  253: 
  254: 	tio.c_iflag |= IGNPAR;
  255: 	tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
  256: #ifdef IUCLC
  257: 	tio.c_iflag &= ~IUCLC;
  258: #endif
  259: 	tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
  260: #ifdef IEXTEN
  261: 	tio.c_lflag &= ~IEXTEN;
  262: #endif
  263: 	tio.c_oflag &= ~OPOST;
  264: 	tio.c_cc[VMIN] = 1;
  265: 	tio.c_cc[VTIME] = 0;
  266: 
  267: 	if (tcsetattr(fd, TCSADRAIN, &tio) == -1) {
  268: 		LOGERR;
  269: 		return -1;
  270: 	}
  271: 
  272: 	return 0;
  273: }
  274: 
  275: /*
  276:  * ioRestoreMode() Restore termios to tty fd
  277:  * @fd = tty fd
  278:  * @tio = termios structure for restore
  279:  * return: -1 error or 0 ok
  280:  */
  281: inline int
  282: ioRestoreMode(int fd, struct termios tio)
  283: {
  284: 	if (tcsetattr(fd, TCSADRAIN, &tio) == -1) {
  285: 		LOGERR;
  286: 		return -1;
  287: 	}
  288: 
  289: 	return 0;
  290: }
  291: 
  292: /*
  293:  * ioForkPTY() Fork new process with session leader and new TTY
  294:  * @ptyfd = master fd, pty
  295:  * @name = tty device name if not null
  296:  * @namesiz = name length, must be above 63 bytes.
  297:  * @term = termios for terminal
  298:  * @winz = winsize for terminal
  299:  * @otio = old termios structure for restore
  300:  * return: -1 error, 0 child process or >0 parent: pid of child
  301:  */
  302: pid_t
  303: ioForkPTY(int *ptyfd, char * __restrict name, int namesiz, struct termios * __restrict term, 
  304: 		struct winsize * __restrict winz, struct termios * __restrict otio)
  305: {
  306: 	int ttyfd;
  307: 	pid_t pid;
  308: 
  309: 	if (ioAllocPTY(ptyfd, &ttyfd, name, namesiz, term, winz))
  310: 		return -1;
  311: 
  312: 	switch ((pid = fork())) {
  313: 		case -1:
  314: 			LOGERR;
  315: 			return -1;
  316: 		case 0:
  317: 			if (ioSetOwnerTTY(name, getuid(), getgid()) == -1) {
  318: 				ioFreePTY(*ptyfd, name);
  319: 				return -1;
  320: 			}
  321: 			if (ioSetSidTTY(&ttyfd, name) == -1) {
  322: 				ioFreePTY(*ptyfd, name);
  323: 				return -1;
  324: 			}
  325: 			close(*ptyfd);
  326: 
  327: 			/* CHILD */
  328: 			break;
  329: 		default:
  330: 			close(ttyfd);
  331: 
  332: 			/* PARENT */
  333: 			break;
  334: 	}
  335: 
  336: 	return pid;
  337: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>