2009-04-22 15:48:07 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2012-07-12 21:38:03 +04:00
|
|
|
Copyright (c) 2006-2012 sqlmap developers (http://sqlmap.org/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import errno
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
2010-10-29 14:51:09 +04:00
|
|
|
from lib.core.common import dataToStdout
|
2009-06-11 19:01:48 +04:00
|
|
|
from lib.core.settings import IS_WIN
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if not IS_WIN:
|
2009-06-11 19:01:48 +04:00
|
|
|
import fcntl
|
|
|
|
|
|
|
|
if (sys.hexversion >> 16) >= 0x202:
|
|
|
|
FCNTL = fcntl
|
|
|
|
else:
|
|
|
|
import FCNTL
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
def blockingReadFromFD(fd):
|
|
|
|
# Quick twist around original Twisted function
|
|
|
|
# Blocking read from a non-blocking file descriptor
|
|
|
|
output = ""
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
output += os.read(fd, 8192)
|
|
|
|
except (OSError, IOError), ioe:
|
|
|
|
if ioe.args[0] in (errno.EAGAIN, errno.EINTR):
|
|
|
|
# Uncomment the following line if the process seems to
|
|
|
|
# take a huge amount of cpu time
|
|
|
|
# time.sleep(0.01)
|
2013-01-04 02:57:07 +04:00
|
|
|
continue
|
2009-04-22 15:48:07 +04:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
break
|
|
|
|
|
|
|
|
if not output:
|
2013-01-04 02:20:55 +04:00
|
|
|
raise EOFError("fd %s has been closed." % fd )
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
return output
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
def blockingWriteToFD(fd, data):
|
|
|
|
# Another quick twist
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data_length = len(data)
|
2011-04-30 17:20:05 +04:00
|
|
|
wrote_data = os.write(fd, data)
|
2009-04-22 15:48:07 +04:00
|
|
|
except (OSError, IOError), io:
|
|
|
|
if io.errno in (errno.EAGAIN, errno.EINTR):
|
2013-01-04 02:57:07 +04:00
|
|
|
continue
|
2009-04-22 15:48:07 +04:00
|
|
|
else:
|
2013-01-04 02:57:07 +04:00
|
|
|
raise
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
if wrote_data < data_length:
|
|
|
|
blockingWriteToFD(fd, data[wrote_data:])
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
def setNonBlocking(fd):
|
|
|
|
"""
|
|
|
|
Make a file descriptor non-blocking
|
|
|
|
"""
|
|
|
|
|
2009-06-11 19:01:48 +04:00
|
|
|
if IS_WIN is not True:
|
|
|
|
flags = fcntl.fcntl(fd, FCNTL.F_GETFL)
|
|
|
|
flags = flags | os.O_NONBLOCK
|
|
|
|
fcntl.fcntl(fd, FCNTL.F_SETFL, flags)
|
2010-10-29 14:51:09 +04:00
|
|
|
|
2012-07-03 18:49:34 +04:00
|
|
|
def pollProcess(process, suppress_errors=False):
|
2010-10-29 14:51:09 +04:00
|
|
|
while True:
|
|
|
|
dataToStdout(".")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
returncode = process.poll()
|
|
|
|
|
|
|
|
if returncode is not None:
|
2012-07-03 18:49:34 +04:00
|
|
|
if not suppress_errors:
|
|
|
|
if returncode == 0:
|
|
|
|
dataToStdout(" done\n")
|
|
|
|
elif returncode < 0:
|
|
|
|
dataToStdout(" process terminated by signal %d\n" % returncode)
|
|
|
|
elif returncode > 0:
|
|
|
|
dataToStdout(" quit unexpectedly with return code %d\n" % returncode)
|
2010-10-29 14:51:09 +04:00
|
|
|
|
|
|
|
break
|