--- libaitio/src/pty.c 2011/09/21 12:55:07 1.1.2.5 +++ libaitio/src/pty.c 2011/09/21 13:25:19 1.1.2.6 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: pty.c,v 1.1.2.5 2011/09/21 12:55:07 misho Exp $ +* $Id: pty.c,v 1.1.2.6 2011/09/21 13:25:19 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -282,4 +282,54 @@ 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: + close(*ptyfd); + + if (ioSetOwnerTTY(name, getuid(), getgid()) == -1) { + ioFreeTTY(name); + return -1; + } + if (ioSetSidTTY(&ttyfd, name) == -1) { + ioFreeTTY(name); + return -1; + } + + /* CHILD */ + break; + default: + close(ttyfd); + + ioSetRAWMode(*ptyfd, otio); + + /* PARENT */ + break; + } + + return pid; }