chrome/updater/test/service/win/impersonate.py
def _RunAsOnWindowStationDesktop(command_line,
                                 security_token,
                                 window_station,
                                 desktop,
                                 env=None,
                                 cwd=None,
                                 timeout=win32event.INFINITE):
  """Runs a command as the security token user on given desktop.

  Args:
    command_line: Full command line string to run.
    security_token: Security token that the command run as.
    window_station: Window station for the new process to run, tpically is
       "WinSta0", aka the interactive window station.
    desktop: Desktop that the new process will be associatedw with, typically is
      'default'.
    env: The environment variables to pass to the child process, or None to
      inherit from parent.
    cwd: The working directory of the child process, or None to inherit from
      parent.
    timeout: How long should wait for child process. 0 means no wait, None means
      infinitely.

  Returns:
    (pid, exit_code, stdout, stderr) tuple.

  Raises:
    ImpersonationError: when impersonation failed.
  """
  pipes = _StdoutStderrPipes()
  si = win32process.STARTUPINFO()
  si.dwFlags = win32process.STARTF_USESHOWWINDOW | win32con.STARTF_USESTDHANDLES
  si.wShowWindow = win32con.SW_SHOW
  si.lpDesktop = '%s\\%s' % (window_station, desktop)
  si.hStdOutput = pipes.stdout_w
  si.hStdError = pipes.stderr_w
  create_flags = (
      win32process.CREATE_NEW_CONSOLE | win32process.CREATE_UNICODE_ENVIRONMENT)

  if env:
    saved_env = dict(os.environ)
    os.environ.update(env)
  env_block = win32profile.CreateEnvironmentBlock(security_token, True)
  (process_handle, unused_thread, pid,
   unused_thread_id) = win32process.CreateProcessAsUser(
       security_token, None, command_line, None, None, 1,
       create_flags, env_block, cwd, si)
  if env:
    os.environ.clear()
    os.environ.update(saved_env)
  pipes.CloseWriteHandles()
  if not process_handle:
    logging.error('Failed to create child process [%s] on [%s\\%s]',
                  command_line, window_station, desktop)
    raise ImpersonationError(
        'Failed to create process [%s] with impersonation: [%s\\%s][%s]' %
        (command_line, window_station, desktop, cwd))

  pipes.ReadAll()
  logging.info('Child process [%s] created on [%s\\%s]',
      command_line, window_station, desktop)
  logging.info('Waiting %s seconds for child process.', timeout)
  if timeout != win32event.INFINITE:
    timeout *= 1000  # Convert from seconds to milli-seconds.
  wait_result = win32event.WaitForSingleObject(process_handle, timeout * 1000)
  if wait_result == win32event.WAIT_OBJECT_0:
    exit_code = win32process.GetExitCodeProcess(process_handle)
    logging.info('Child process exited with code %s.', exit_code)
    logging.info('Child process STDOUT: %s', pipes.stdout)
    logging.error('Child process STDERR: %s.', pipes.stderr)
    return (pid, exit_code, pipes.stdout, pipes.stderr)
  else:
    if timeout != 0:
      logging.warning('Wait for child process timeout in %s seconds',
                      timeout / 1000)
    return (pid, None, None, None)
Runs a command as the security token user on given desktop.

  Args:
    command_line: Full command line string to run.
    security_token: Security token that the command run as.
    window_station: Window station for the new process to run, tpically is
       "WinSta0", aka the interactive window station.
    desktop: Desktop that the new process will be associatedw with, typically is
      'default'.
    env: The environment variables to pass to the child process, or None to
      inherit from parent.
    cwd: The working directory of the child process, or None to inherit from
      parent.
    timeout: How long should wait for child process. 0 means no wait, None means
      infinitely.

  Returns:
    (pid, exit_code, stdout, stderr) tuple.

  Raises:
    ImpersonationError: when impersonation failed.
  
Runs a command as the security token user on given desktop.
Runs a command as the security token user on given desktop .
def _RunAsOnWindowStationDesktop(command_line,
                                 security_token,
                                 window_station,
                                 desktop,
                                 env=None,
                                 cwd=None,
                                 timeout=win32event.INFINITE):
  pipes = _StdoutStderrPipes()
  si = win32process.STARTUPINFO()
  si.dwFlags = win32process.STARTF_USESHOWWINDOW | win32con.STARTF_USESTDHANDLES
  si.wShowWindow = win32con.SW_SHOW
  si.lpDesktop = '%s\\%s' % (window_station, desktop)
  si.hStdOutput = pipes.stdout_w
  si.hStdError = pipes.stderr_w
  create_flags = (
      win32process.CREATE_NEW_CONSOLE | win32process.CREATE_UNICODE_ENVIRONMENT)
  if env:
    saved_env = dict(os.environ)
    os.environ.update(env)
  env_block = win32profile.CreateEnvironmentBlock(security_token, True)
  (process_handle, unused_thread, pid,
   unused_thread_id) = win32process.CreateProcessAsUser(
       security_token, None, command_line, None, None, 1,
       create_flags, env_block, cwd, si)
  if env:
    os.environ.clear()
    os.environ.update(saved_env)
  pipes.CloseWriteHandles()
  if not process_handle:
    logging.error('Failed to create child process [%s] on [%s\\%s]',
                  command_line, window_station, desktop)
    raise ImpersonationError(
        'Failed to create process [%s] with impersonation: [%s\\%s][%s]' %
        (command_line, window_station, desktop, cwd))
  pipes.ReadAll()
  logging.info('Child process [%s] created on [%s\\%s]',
      command_line, window_station, desktop)
  logging.info('Waiting %s seconds for child process.', timeout)
  if timeout != win32event.INFINITE:
    timeout *= 1000  
  wait_result = win32event.WaitForSingleObject(process_handle, timeout * 1000)
  if wait_result == win32event.WAIT_OBJECT_0:
    exit_code = win32process.GetExitCodeProcess(process_handle)
    logging.info('Child process exited with code %s.', exit_code)
    logging.info('Child process STDOUT: %s', pipes.stdout)
    logging.error('Child process STDERR: %s.', pipes.stderr)
    return (pid, exit_code, pipes.stdout, pipes.stderr)
  else:
    if timeout != 0:
      logging.warning('Wait for child process timeout in %s seconds',
                      timeout / 1000)
    return (pid, None, None, None)
def _RunAsOnWindowStationDesktop ( command_line , security_token , window_station , desktop , env = None , cwd = None , timeout = win32event . INFINITE ) : pipes = _StdoutStderrPipes ( ) si = win32process . STARTUPINFO ( ) si . dwFlags = win32process . STARTF_USESHOWWINDOW | win32con . STARTF_USESTDHANDLES si . wShowWindow = win32con . SW_SHOW si . lpDesktop = '%s\\%s' % ( window_station , desktop ) si . hStdOutput = pipes . stdout_w si . hStdError = pipes . stderr_w create_flags = ( win32process . CREATE_NEW_CONSOLE | win32process . CREATE_UNICODE_ENVIRONMENT ) if env : saved_env = dict ( os . environ ) os . environ . update ( env ) env_block = win32profile . CreateEnvironmentBlock ( security_token , True ) ( process_handle , unused_thread , pid , unused_thread_id ) = win32process . CreateProcessAsUser ( security_token , None , command_line , None , None , 1 , create_flags , env_block , cwd , si ) if env : os . environ . clear ( ) os . environ . update ( saved_env ) pipes . CloseWriteHandles ( ) if not process_handle : logging . error ( 'Failed to create child process [%s] on [%s\\%s]' , command_line , window_station , desktop ) raise ImpersonationError ( 'Failed to create process [%s] with impersonation: [%s\\%s][%s]' % ( command_line , window_station , desktop , cwd ) ) pipes . ReadAll ( ) logging . info ( 'Child process [%s] created on [%s\\%s]' , command_line , window_station , desktop ) logging . info ( 'Waiting %s seconds for child process.' , timeout ) if timeout != win32event . INFINITE : timeout *= 1000 wait_result = win32event . WaitForSingleObject ( process_handle , timeout * 1000 ) if wait_result == win32event . WAIT_OBJECT_0 : exit_code = win32process . GetExitCodeProcess ( process_handle ) logging . info ( 'Child process exited with code %s.' , exit_code ) logging . info ( 'Child process STDOUT: %s' , pipes . stdout ) logging . error ( 'Child process STDERR: %s.' , pipes . stderr ) return ( pid , exit_code , pipes . stdout , pipes . stderr ) else : if timeout != 0 : logging . warning ( 'Wait for child process timeout in %s seconds' , timeout / 1000 ) return ( pid , None , None , None )
Runs a command as the security token user on given desktop.
Runs a command as the security token user on given desktop .
"""Runs a command as the security token user on given desktop.

  Args:
    command_line: Full command line string to run.
    security_token: Security token that the command run as.
    window_station: Window station for the new process to run, tpically is
       "WinSta0", aka the interactive window station.
    desktop: Desktop that the new process will be associatedw with, typically is
      'default'.
    env: The environment variables to pass to the child process, or None to
      inherit from parent.
    cwd: The working directory of the child process, or None to inherit from
      parent.
    timeout: How long should wait for child process. 0 means no wait, None means
      infinitely.

  Returns:
    (pid, exit_code, stdout, stderr) tuple.

  Raises:
    ImpersonationError: when impersonation failed.
  """ # Convert from seconds to milli-seconds.
(pid, exit_code, stdout, stderr) tuple.
( pid exit_code stdout stderr ) tuple .
when impersonation failed.
when impersonation failed .
ImpersonationError
command_line
Full command line string to run.
Full command line string to run .
security_token
Security token that the command run as.
Security token that the command run as .
window_station
Window station for the new process to run, tpically is
"WinSta0", aka the interactive window station.
Window station for the new process to run tpically is " WinSta0 " aka the interactive window station .
desktop
Desktop that the new process will be associatedw with, typically is
'default'.
Desktop that the new process will be associatedw with typically is ' default ' .
env
The environment variables to pass to the child process, or None to
inherit from parent.
The environment variables to pass to the child process or None to inherit from parent .
cwd
The working directory of the child process, or None to inherit from
parent.
The working directory of the child process or None to inherit from parent .
timeout
None