Beispiel #1
0
    '''
    Inspector(dbfile, pidfile).snapshot(mode)


if __name__ == '__main__':
    if len(sys.argv) != 4:
        print >> sys.stderr, "This module is not intended to use directly!"
        sys.exit(1)

    pidfile, dbfile, mode = sys.argv[1:]
    is_alive(pidfile)

    # Double-fork stuff
    try:
        if os.fork() > 0:
            reinit_crypto()
            sys.exit(0)
        else:
            reinit_crypto()
    except OSError as ex:
        sys.exit(1)

    os.setsid()
    os.umask(0)

    try:
        pid = os.fork()
        if pid > 0:
            reinit_crypto()
            with salt.utils.fopen(pidfile, 'w') as fp_:
                fp_.write('{0}\n'.format(pid))
Beispiel #2
0
        def __fork_ptys(self):
            '''
            Fork the PTY

            The major difference from the python source is that we separate the
            stdout from stderr output.
            '''
            stdout_parent_fd, stdout_child_fd = pty.openpty()
            if stdout_parent_fd < 0 or stdout_child_fd < 0:
                raise TerminalException('Failed to open a TTY for stdout')

            stderr_parent_fd, stderr_child_fd = pty.openpty()
            if stderr_parent_fd < 0 or stderr_child_fd < 0:
                raise TerminalException('Failed to open a TTY for stderr')

            pid = os.fork()
            if pid < pty.CHILD:
                raise TerminalException('Failed to fork')
            elif pid == pty.CHILD:
                # Child.
                # Close parent FDs
                os.close(stdout_parent_fd)
                os.close(stderr_parent_fd)
                reinit_crypto()

                # ----- Make STDOUT the controlling PTY --------------------->
                child_name = os.ttyname(stdout_child_fd)
                # Disconnect from controlling tty. Harmless if not already
                # connected
                try:
                    tty_fd = os.open('/dev/tty', os.O_RDWR | os.O_NOCTTY)
                    if tty_fd >= 0:
                        os.close(tty_fd)
                # which exception, shouldn't we catch explicitly .. ?
                except:  # pylint: disable=W0702
                    # Already disconnected. This happens if running inside cron
                    pass

                # New session!
                os.setsid()

                # Verify we are disconnected from controlling tty
                # by attempting to open it again.
                try:
                    tty_fd = os.open('/dev/tty', os.O_RDWR | os.O_NOCTTY)
                    if tty_fd >= 0:
                        os.close(tty_fd)
                        raise TerminalException(
                            'Failed to disconnect from controlling tty. It is '
                            'still possible to open /dev/tty.'
                        )
                # which exception, shouldn't we catch explicitly .. ?
                except:  # pylint: disable=W0702
                    # Good! We are disconnected from a controlling tty.
                    pass

                # Verify we can open child pty.
                tty_fd = os.open(child_name, os.O_RDWR)
                if tty_fd < 0:
                    raise TerminalException(
                        'Could not open child pty, {0}'.format(child_name)
                    )
                else:
                    os.close(tty_fd)

                # Verify we now have a controlling tty.
                if os.name != 'posix':
                    # Only do this check in not BSD-like operating systems. BSD-like operating systems breaks at this point
                    tty_fd = os.open('/dev/tty', os.O_WRONLY)
                    if tty_fd < 0:
                        raise TerminalException(
                            'Could not open controlling tty, /dev/tty'
                        )
                    else:
                        os.close(tty_fd)
                # <---- Make STDOUT the controlling PTY ----------------------

                # ----- Duplicate Descriptors ------------------------------->
                os.dup2(stdout_child_fd, pty.STDIN_FILENO)
                os.dup2(stdout_child_fd, pty.STDOUT_FILENO)
                os.dup2(stderr_child_fd, pty.STDERR_FILENO)
                # <---- Duplicate Descriptors --------------------------------
            else:
                # Parent. Close Child PTY's
                reinit_crypto()
                os.close(stdout_child_fd)
                os.close(stderr_child_fd)

            return pid, stdout_parent_fd, stderr_parent_fd
Beispiel #3
0
    '''
    Inspector(dbfile, pidfile).snapshot(mode)


if __name__ == '__main__':
    if len(sys.argv) != 4:
        print >> sys.stderr, "This module is not intended to use directly!"
        sys.exit(1)

    pidfile, dbfile, mode = sys.argv[1:]
    is_alive(pidfile)

    # Double-fork stuff
    try:
        if os.fork() > 0:
            reinit_crypto()
            sys.exit(0)
        else:
            reinit_crypto()
    except OSError as ex:
        sys.exit(1)

    os.setsid()
    os.umask(0)

    try:
        pid = os.fork()
        if pid > 0:
            reinit_crypto()
            fpid = open(pidfile, "w")
            fpid.write("{0}\n".format(pid))