--- libaitio/src/pty.c 2011/09/19 23:30:50 1.1.2.4 +++ libaitio/src/pty.c 2011/09/30 14:26:57 1.1.2.12 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: pty.c,v 1.1.2.4 2011/09/19 23:30:50 misho Exp $ +* $Id: pty.c,v 1.1.2.12 2011/09/30 14:26:57 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -76,19 +76,24 @@ ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict n } /* - * ioFreeTTY() Release TTY + * ioFreePTY() Release PTY and TTY device + * @ptyfd = master fd, pty (==-1 skip closing pty) * @ttyname = tty filename * return: none */ inline void -ioFreeTTY(const char *ttyname) +ioFreePTY(int ptyfd, const char *ttyname) { assert(ttyname); if (!ttyname) return; - chown(ttyname, (uid_t) 0, (gid_t) 0); - chmod(ttyname, (mode_t) 0666); + if (ptyfd != -1) + close(ptyfd); + if (ttyname) { + chown(ttyname, (uid_t) 0, (gid_t) 0); + chmod(ttyname, (mode_t) 0666); + } } /* @@ -217,6 +222,14 @@ ioSetSidTTY(int *ttyfd, const char *ttyname) } else close(fd); + /* redirect standart handles to tty */ + dup2(*ttyfd, STDIN_FILENO); + dup2(*ttyfd, STDOUT_FILENO); + dup2(*ttyfd, STDERR_FILENO); + if (*ttyfd > 2) + close(*ttyfd); + + /* NOW TTY IS READY! */ return 0; } @@ -274,4 +287,51 @@ ioRestoreMode(int fd, struct termios tio) } return 0; +} + +/* + * ioForkPTY() Fork new process with session leader and new TTY + * @ptyfd = master fd, pty + * @name = tty device name if not null + * @namesiz = name length, must be above 63 bytes. + * @term = termios for terminal + * @winz = winsize for terminal + * @otio = old termios structure for restore + * return: -1 error, 0 child process or >0 parent: pid of child + */ +pid_t +ioForkPTY(int *ptyfd, char * __restrict name, int namesiz, struct termios * __restrict term, + struct winsize * __restrict winz, struct termios * __restrict otio) +{ + int ttyfd; + pid_t pid; + + if (ioAllocPTY(ptyfd, &ttyfd, name, namesiz, term, winz)) + return -1; + + switch ((pid = fork())) { + case -1: + LOGERR; + return -1; + case 0: + if (ioSetOwnerTTY(name, getuid(), getgid()) == -1) { + ioFreePTY(*ptyfd, name); + return -1; + } + if (ioSetSidTTY(&ttyfd, name) == -1) { + ioFreePTY(*ptyfd, name); + return -1; + } + close(*ptyfd); + + /* CHILD */ + break; + default: + close(ttyfd); + + /* PARENT */ + break; + } + + return pid; }