def _stub(cmd_name, stdin_name, stdout_name, stderr_name):
"""INTERNAL: Stub process that will start up the child process."""
# Open the 4 pipes (command, stdin, stdout, stderr)
cmd_pipe = CreateFile(cmd_name, GENERIC_READ|GENERIC_WRITE, 0, None,
OPEN_EXISTING, 0, None)
SetHandleInformation(cmd_pipe, HANDLE_FLAG_INHERIT, 1)
stdin_pipe = CreateFile(stdin_name, GENERIC_READ, 0, None,
OPEN_EXISTING, 0, None)
SetHandleInformation(stdin_pipe, HANDLE_FLAG_INHERIT, 1)
stdout_pipe = CreateFile(stdout_name, GENERIC_WRITE, 0, None,
OPEN_EXISTING, 0, None)
SetHandleInformation(stdout_pipe, HANDLE_FLAG_INHERIT, 1)
stderr_pipe = CreateFile(stderr_name, GENERIC_WRITE, 0, None,
OPEN_EXISTING, 0, None)
SetHandleInformation(stderr_pipe, HANDLE_FLAG_INHERIT, 1)
# Learn what we need to do..
header = _read_header(cmd_pipe)
input = _parse_header(header)
if 'command' not in input or 'args' not in input:
ExitProcess(2)
# http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx
startupinfo = STARTUPINFO()
startupinfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW
startupinfo.hStdInput = stdin_pipe
startupinfo.hStdOutput = stdout_pipe
startupinfo.hStdError = stderr_pipe
startupinfo.wShowWindow = SW_HIDE
# Grant access so that our parent can open its grandchild.
if 'parent_sid' in input:
mysid = _get_current_sid()
parent = ConvertStringSidToSid(input['parent_sid'])
sattrs = _create_security_attributes(mysid, parent,
access=PROCESS_ALL_ACCESS)
else:
sattrs = None
try:
res = CreateProcess(input['command'], input['args'], sattrs, None,
True, CREATE_NEW_CONSOLE, os.environ, os.getcwd(),
startupinfo)
except WindowsError, e:
message = _quote_header(str(e))
WriteFile(cmd_pipe, 'status=error\nmessage=%s\n\n' % message)
ExitProcess(3)
else:
pid = res[2]
# Pass back results and exit
err, nbytes = WriteFile(cmd_pipe, 'status=ok\npid=%s\n\n' % pid)
ExitProcess(0)
评论列表
文章目录