Ejemplo n.º 1
0
def portablekill(pid):
    global ostype
    global osrealtype

    if ostype == None:
        init_ostype()

    if ostype == 'Linux' or ostype == 'Darwin':
        # On Android, block until any pending sensor call has returned.
        if osrealtype == 'Android':
            # Acquire all sensor locks. This may take a few seconds, especially
            # when text-to-speech is progressing.
            for lock in repysensors.locklist:
                lock.acquire(True)
        try:
            os.kill(pid, signal.SIGTERM)
        except:
            pass

        try:
            os.kill(pid, signal.SIGKILL)
        except:
            pass

    elif ostype == 'Windows':
        # Use new api
        windows_api.kill_process(pid)

    else:
        raise UnsupportedSystemException, "Unsupported system type: '" + osrealtype + "' (alias: " + ostype + ")"
Ejemplo n.º 2
0
def main():
  if len(sys.argv) != 4:
    print "Error, didn't get the right number of args:",sys.argv
    sys.exit(1)

  ppid = int(sys.argv[1])
  limit = float(sys.argv[2])
  freq = float(sys.argv[3])

  # run forever, checking the process' CPU use and stopping when appropriate
  try:
    while True:
    # Base amount of sleeping on return value of win_check_cpu_use to prevent under/over sleeping
      slept = nonportable.win_check_cpu_use(limit, ppid)

      if slept == -1:
        # Something went wrong, try again
        pass
      elif slept == 0:
        time.sleep(freq)
      elif (slept < freq):
        time.sleep(freq-slept)
    
      # see if the process exited...
      status = windows_api.process_exit_code(ppid)
      # Amazing! They rely on the programmer to not return 259 to know when 
      # something actually exited.   Luckily, I do control the return codes...
      if status != 259:
        sys.exit(0)

  except SystemExit:
    pass

  except windows_api.DeadProcess:
    # This can be caused when getting process times for a dead thread or
    # Trying to timeout a dead thread, either way, we just exit
    sys.exit(0)

  except:
    tracebackrepy.handle_exception()
    print >> sys.stderr, "Win nanny died!   Trying to kill everything else"
    
    # kill the program we're monitoring
    # Use newer api to kill process
    windows_api.kill_process(ppid)
Ejemplo n.º 3
0
def kill_webserver(pid, url):
    """
  <Purpose>
    When killing the webserver, we want to make sure it is no longer serving
    files before declaring it officially dead.  This method ensures that this
    happens.
  <Arguments>
    pid - The pid of the webserver
    url - The url the webserver can be accessed at.  Queried to ensure the 
          webserver is no longer serving data.
  <Exceptions>
    Exception if the pid is invalid
  <Side Effects>
    The given pid is killed.
  <Returns>
    None
  """
    if nonportable.ostype == 'Windows':
        windows_api.kill_process(pid)
    else:
        os.kill(pid, signal.SIGKILL)

    # We need to make sure the webserver is stopped up before returning.  Waiting
    # an arbitrary amount of time is just asking for strange and inconsistent
    # errors, so I will actually query the web server until it fails.  However,
    # to prevent an infinite loop, I will limit the waiting to 1 minute.  What
    # else can we do if it doesn't die other than just raising an exception?
    for junk in xrange(60):
        try:
            # If we can successfully retrieve the url, we aren't ready.
            urllib.urlretrieve(url)
        except Exception:
            # We are done when it fails!
            return

        time.sleep(1)

    # If it didn't die within the 60 seconds it was given, it probably won't die
    # any time soon, so we will raise an exception and to exit ourselves.
    raise Exception("Could not kill the webserver.   pid == " + str(pid) + \
        " :: url == " + str(url))
def kill_webserver(pid, url):
  """
  <Purpose>
    When killing the webserver, we want to make sure it is no longer serving
    files before declaring it officially dead.  This method ensures that this
    happens.
  <Arguments>
    pid - The pid of the webserver
    url - The url the webserver can be accessed at.  Queried to ensure the 
          webserver is no longer serving data.
  <Exceptions>
    Exception if the pid is invalid
  <Side Effects>
    The given pid is killed.
  <Returns>
    None
  """
  if nonportable.ostype == 'Windows':
    windows_api.kill_process(pid)
  else:
    os.kill(pid, signal.SIGKILL)

  # We need to make sure the webserver is stopped up before returning.  Waiting
  # an arbitrary amount of time is just asking for strange and inconsistent
  # errors, so I will actually query the web server until it fails.  However,
  # to prevent an infinite loop, I will limit the waiting to 1 minute.  What
  # else can we do if it doesn't die other than just raising an exception?
  for junk in xrange(60):
    try:
      # If we can successfully retrieve the url, we aren't ready.
      urllib.urlretrieve(url)
    except Exception:
      # We are done when it fails!
      return
    
    time.sleep(1)

  # If it didn't die within the 60 seconds it was given, it probably won't die
  # any time soon, so we will raise an exception and to exit ourselves.
  raise Exception("Could not kill the webserver.   pid == " + str(pid) + \
      " :: url == " + str(url))
Ejemplo n.º 5
0
def portablekill(pid):
  global ostype
  global osrealtype

  if ostype == None:
    init_ostype()

  if ostype == 'Linux' or ostype == 'Darwin':
    try:
      os.kill(pid, signal.SIGTERM)
    except:
      pass

    try:
      os.kill(pid, signal.SIGKILL)
    except:
      pass

  elif ostype == 'Windows':
    # Use new api
    windows_api.kill_process(pid)
    
  else:
    raise UnsupportedSystemException, "Unsupported system type: '"+osrealtype+"' (alias: "+ostype+")"
Ejemplo n.º 6
0
def portablekill(pid):
    global ostype
    global osrealtype

    if ostype == None:
        init_ostype()

    if ostype == 'Linux' or ostype == 'Darwin':
        try:
            os.kill(pid, signal.SIGTERM)
        except:
            pass

        try:
            os.kill(pid, signal.SIGKILL)
        except:
            pass

    elif ostype == 'Windows':
        # Use new api
        windows_api.kill_process(pid)

    else:
        raise UnsupportedSystemException, "Unsupported system type: '" + osrealtype + "' (alias: " + ostype + ")"