'This makes the pseudo-terminal the controlling tty. This should be
more portable than the pty.fork() function. Specifically, this should
work on Solaris.'

def __pty_make_controlling_tty(self, tty_fd):

    child_name = os.ttyname(tty_fd)
    try:
        fd = os.open('/dev/tty', (os.O_RDWR | os.O_NOCTTY))
        if (fd >= 0):
            os.close(fd)
    except OSError:
        pass
    os.setsid()
    try:
        fd = os.open('/dev/tty', (os.O_RDWR | os.O_NOCTTY))
        if (fd >= 0):
            os.close(fd)
            raise Exception(('Failed    to    disconnect    from    ' + 'controlling    tty.    It    is    still    possible    to    open    /dev/tty.'))
    except OSError:
        pass
    fd = os.open(child_name, os.O_RDWR)
    if (fd < 0):
        raise Exception(('Could    not    open    child    pty,    ' + child_name))
    else:
        os.close(fd)
    fd = os.open('/dev/tty', os.O_WRONLY)
    if (fd < 0):
        raise Exception('Could    not    open    controlling    tty,    /dev/tty')
    else:
        os.close(fd)
