--- libaitio/src/pty.c 2011/09/19 21:59:58 1.1.2.1 +++ libaitio/src/pty.c 2011/09/19 23:30:50 1.1.2.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: pty.c,v 1.1.2.1 2011/09/19 21:59:58 misho Exp $ +* $Id: pty.c,v 1.1.2.4 2011/09/19 23:30:50 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -47,7 +47,7 @@ SUCH DAMAGE. /* - * ioAllocPTY() Allocate new PTY + * ioAllocPTY() Allocate new PTY and TTY * @ptyfd = master fd, pty * @ttyfd = slave fd, tty * @name = tty device name if not null @@ -76,12 +76,12 @@ ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict n } /* - * ioFreePTY() Release PTY + * ioFreeTTY() Release TTY * @ttyname = tty filename * return: none */ inline void -ioFreePTY(const char *ttyname) +ioFreeTTY(const char *ttyname) { assert(ttyname); if (!ttyname) @@ -89,4 +89,189 @@ ioFreePTY(const char *ttyname) chown(ttyname, (uid_t) 0, (gid_t) 0); chmod(ttyname, (mode_t) 0666); +} + +/* + * ioChgWinPTY() Change window size of PTY + * @ptyfd = master fd, pty + * @row = row + * @col = col + * @xpxl = x pixels + * @ypxl = y pixels + * return: -1 error or 0 ok + */ +inline int +ioChgWinPTY(int ptyfd, u_short row, u_short col, u_short xpxl, u_short ypxl) +{ + struct winsize w; + + w.ws_row = row; + w.ws_col = col; + w.ws_xpixel = xpxl; + w.ws_ypixel = ypxl; + + if (ioctl(ptyfd, TIOCSWINSZ, &w) == -1) { + LOGERR; + return -1; + } + + return 0; +} + +/* + * ioSetOwnerTTY() Set owner to TTY + * @ttyname = tty filename + * @UID = uid + * @GID = gid + * return: -1 error or 0 ok + */ +int +ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GID) +{ + struct group *grp; + gid_t gid; + mode_t mode; + struct stat st; + + assert(ttyname); + if (!ttyname) { + io_SetErr(EINVAL, "Error:: invalid arguments ..."); + return -1; + } + + grp = getgrnam("tty"); + if (!grp) { + gid = GID; + mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; + } else { + gid = grp->gr_gid; + mode = S_IRUSR | S_IWUSR | S_IWGRP; + } + + if (stat(ttyname, &st) == -1) { + LOGERR; + return -1; + } + + if (st.st_uid != UID || st.st_gid != gid) + if (chown(ttyname, UID, gid) == -1) { + LOGERR; + return -1; + } + if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) + if (chmod(ttyname, mode) == -1) { + LOGERR; + return -1; + } + + return 0; +} + +/* + * ioSetSidTTY() Makes the process's controlling TTY and sets it to sane modes. + * @ttyfd = slave fd, tty + * @ttyname = tty filename + * return: -1 error or 0 ok + */ +int +ioSetSidTTY(int *ttyfd, const char *ttyname) +{ + int fd; + + /* First disconnect from the old controlling tty. */ +#ifdef TIOCNOTTY + fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); + if (fd >= 0) { + ioctl(fd, TIOCNOTTY, NULL); + close(fd); + } +#endif + setsid(); + + /* Verify that we are successfully disconnected from the controlling tty. */ + fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); + if (fd >= 0) { + io_SetErr(ENXIO, "Error:: Failed to disconnect from controlling tty."); + close(fd); + return -1; + } + /* Make it our controlling tty. */ +#ifdef TIOCSCTTY + if (ioctl(*ttyfd, TIOCSCTTY, NULL) == -1) { + LOGERR; + return -1; + } +#endif + fd = open(ttyname, O_RDWR); + if (fd == -1) { + LOGERR; + return -1; + } else + close(fd); + + /* Verify that we now have a controlling tty. */ + fd = open(_PATH_TTY, O_WRONLY); + if (fd == -1) { + LOGERR; + return -1; + } else + close(fd); + + return 0; +} + +/* + * ioSetRAWMode() Enter into RAW mode + * @fd = tty fd + * @otio = saved old termios for later restore if !=NULL + * return: -1 error or 0 ok + */ +inline int +ioSetRAWMode(int fd, struct termios *otio) +{ + struct termios tio; + + if (tcgetattr(fd, &tio) == -1) { + LOGERR; + return -1; + } + if (otio) + *otio = tio; + + tio.c_iflag |= IGNPAR; + tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF); +#ifdef IUCLC + tio.c_iflag &= ~IUCLC; +#endif + tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL); +#ifdef IEXTEN + tio.c_lflag &= ~IEXTEN; +#endif + tio.c_oflag &= ~OPOST; + tio.c_cc[VMIN] = 1; + tio.c_cc[VTIME] = 0; + + if (tcsetattr(fd, TCSADRAIN, &tio) == -1) { + LOGERR; + return -1; + } + + return 0; +} + +/* + * ioRestoreMode() Restore termios to tty fd + * @fd = tty fd + * @tio = termios structure for restore + * return: -1 error or 0 ok + */ +inline int +ioRestoreMode(int fd, struct termios tio) +{ + if (tcsetattr(fd, TCSADRAIN, &tio) == -1) { + LOGERR; + return -1; + } + + return 0; }