chrome/updater/test/service/win/impersonate.py
def RunAsPidOnDeskstop(command_line,
                       pid,
                       window_station='WinSta0',
                       desktop='default',
                       env=None,
                       cwd=None,
                       timeout=win32event.INFINITE):
  """Runs a command with pid's security token and on the given desktop.

  Args:
    command_line: The command line string, including arguments, to run.
    pid: ID of the process to get the security token from.
    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'. If the command needs to interact with UAC prompt on secure
      desktop, pass 'winlogon'.
    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 in seconds to wait for child process.

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

  logging.info('RunAsPidOnDeskstop: [%s][%s]', pid, command_line)

  process_handle = None
  token_handle = None
  try:
    process_handle = win32api.OpenProcess(win32con.GENERIC_ALL, False, pid)
    token_handle = win32security.OpenProcessToken(process_handle,
                                                  win32con.TOKEN_ALL_ACCESS)
    return _RunAsOnWindowStationDesktop(
        command_line, token_handle, window_station, desktop, env, cwd, timeout)
  except (pywintypes.error, ImpersonationError) as err:
    logging.error(err)
    return (None, None, None, None)
  finally:
    if process_handle:
      process_handle.Close()
    if token_handle:
      token_handle.Close()
Runs a command with pid's security token and on the given desktop.

  Args:
    command_line: The command line string, including arguments, to run.
    pid: ID of the process to get the security token from.
    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'. If the command needs to interact with UAC prompt on secure
      desktop, pass 'winlogon'.
    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 in seconds to wait for child process.

  Returns:
    (pid, exit_code, stdout, stderr) tuple.
  
Runs a command with pid's security token and on the given desktop.
Runs a command with pid ' s security token and on the given desktop .
def RunAsPidOnDeskstop(command_line,
                       pid,
                       window_station='WinSta0',
                       desktop='default',
                       env=None,
                       cwd=None,
                       timeout=win32event.INFINITE):
  logging.info('RunAsPidOnDeskstop: [%s][%s]', pid, command_line)
  process_handle = None
  token_handle = None
  try:
    process_handle = win32api.OpenProcess(win32con.GENERIC_ALL, False, pid)
    token_handle = win32security.OpenProcessToken(process_handle,
                                                  win32con.TOKEN_ALL_ACCESS)
    return _RunAsOnWindowStationDesktop(
        command_line, token_handle, window_station, desktop, env, cwd, timeout)
  except (pywintypes.error, ImpersonationError) as err:
    logging.error(err)
    return (None, None, None, None)
  finally:
    if process_handle:
      process_handle.Close()
    if token_handle:
      token_handle.Close()
def RunAsPidOnDeskstop ( command_line , pid , window_station = 'WinSta0' , desktop = 'default' , env = None , cwd = None , timeout = win32event . INFINITE ) : logging . info ( 'RunAsPidOnDeskstop: [%s][%s]' , pid , command_line ) process_handle = None token_handle = None try : process_handle = win32api . OpenProcess ( win32con . GENERIC_ALL , False , pid ) token_handle = win32security . OpenProcessToken ( process_handle , win32con . TOKEN_ALL_ACCESS ) return _RunAsOnWindowStationDesktop ( command_line , token_handle , window_station , desktop , env , cwd , timeout ) except ( pywintypes . error , ImpersonationError ) as err : logging . error ( err ) return ( None , None , None , None ) finally : if process_handle : process_handle . Close ( ) if token_handle : token_handle . Close ( )
Runs a command with pid's security token and on the given desktop.
Runs a command with pid ' s security token and on the given desktop .
"""Runs a command with pid's security token and on the given desktop.

  Args:
    command_line: The command line string, including arguments, to run.
    pid: ID of the process to get the security token from.
    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'. If the command needs to interact with UAC prompt on secure
      desktop, pass 'winlogon'.
    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 in seconds to wait for child process.

  Returns:
    (pid, exit_code, stdout, stderr) tuple.
  """
(pid, exit_code, stdout, stderr) tuple.
( pid exit_code stdout stderr ) tuple .
command_line
The command line string, including arguments, to run.
The command line string including arguments to run .
pid
ID of the process to get the security token from.
ID of the process to get the security token from .
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'. If the command needs to interact with UAC prompt on secure
desktop, pass 'winlogon'.
Desktop that the new process will be associatedw with typically is ' default ' . If the command needs to interact with UAC prompt on secure desktop pass ' winlogon ' .
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