--- libaitio/src/pty.c 2011/09/22 22:25:12 1.1.2.7 +++ libaitio/src/pty.c 2012/03/27 21:37:56 1.2.8.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: pty.c,v 1.1.2.7 2011/09/22 22:25:12 misho Exp $ +* $Id: pty.c,v 1.2.8.1 2012/03/27 21:37:56 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -48,6 +48,7 @@ SUCH DAMAGE. /* * ioAllocPTY() Allocate new PTY and TTY + * * @ptyfd = master fd, pty * @ttyfd = slave fd, tty * @name = tty device name if not null @@ -76,25 +77,30 @@ ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict n } /* - * ioFreeTTY() Release PTY and TTY device - * @ptyfd = master fd, pty + * ioFreePTY() Release PTY and TTY device + * + * @ptyfd = master fd, pty (==-1 skip closing pty) * @ttyname = tty filename * return: none */ inline void -ioFreeTTY(int ptyfd, const char *ttyname) +ioFreePTY(int ptyfd, const char *ttyname) { assert(ttyname); if (!ttyname) return; - close(ptyfd); - 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); + } } /* * ioChgWinPTY() Change window size of PTY + * * @ptyfd = master fd, pty * @row = row * @col = col @@ -122,6 +128,7 @@ ioChgWinPTY(int ptyfd, u_short row, u_short col, u_sho /* * ioSetOwnerTTY() Set owner to TTY + * * @ttyname = tty filename * @UID = uid * @GID = gid @@ -171,6 +178,7 @@ ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GI /* * 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 @@ -232,6 +240,7 @@ ioSetSidTTY(int *ttyfd, const char *ttyname) /* * ioSetRAWMode() Enter into RAW mode + * * @fd = tty fd * @otio = saved old termios for later restore if !=NULL * return: -1 error or 0 ok @@ -271,6 +280,7 @@ ioSetRAWMode(int fd, struct termios *otio) /* * ioRestoreMode() Restore termios to tty fd + * * @fd = tty fd * @tio = termios structure for restore * return: -1 error or 0 ok @@ -288,6 +298,7 @@ ioRestoreMode(int fd, struct termios tio) /* * 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. @@ -312,11 +323,11 @@ ioForkPTY(int *ptyfd, char * __restrict name, int name return -1; case 0: if (ioSetOwnerTTY(name, getuid(), getgid()) == -1) { - ioFreeTTY(*ptyfd, name); + ioFreePTY(*ptyfd, name); return -1; } if (ioSetSidTTY(&ttyfd, name) == -1) { - ioFreeTTY(*ptyfd, name); + ioFreePTY(*ptyfd, name); return -1; } close(*ptyfd); @@ -325,8 +336,6 @@ ioForkPTY(int *ptyfd, char * __restrict name, int name break; default: close(ttyfd); - - ioSetRAWMode(*ptyfd, otio); /* PARENT */ break;