Exemplo n.º 1
0
  def test_connections(self):
    """
    Checks for our control port in the stem.util.proc.connections output if
    we have one.
    """

    runner = test.runner.get_runner()

    if not proc.is_available():
      test.runner.skip(self, '(proc unavailable)')
      return
    elif test.runner.Torrc.PORT not in runner.get_options():
      test.runner.skip(self, '(no control port)')
      return
    elif not test.runner.get_runner().is_ptraceable():
      test.runner.skip(self, '(DisableDebuggerAttachment is set)')
      return
    elif not os.access('/proc/net/tcp', os.R_OK) or not os.access('/proc/net/udp', os.R_OK):
      test.runner.skip(self, '(proc lacks read permissions)')
      return

    # making a controller connection so that we have something to query for
    with runner.get_tor_socket():
      tor_pid = test.runner.get_runner().get_pid()

      for conn in proc.connections(tor_pid):
        if ('127.0.0.1', test.runner.CONTROL_PORT) == conn[:2]:
          return

      self.fail()
Exemplo n.º 2
0
  def test_get_connections(self):
    """
    Checks for our control port in the stem.util.proc.get_connections output if
    we have one.
    """

    runner = test.runner.get_runner()

    if not proc.is_available():
      test.runner.skip(self, "(proc unavailable)")
      return
    elif not test.runner.Torrc.PORT in runner.get_options():
      test.runner.skip(self, "(no control port)")
      return
    elif not test.runner.get_runner().is_ptraceable():
      test.runner.skip(self, "(DisableDebuggerAttachment is set)")
      return

    # making a controller connection so that we have something to query for
    with runner.get_tor_socket():
      tor_pid = test.runner.get_runner().get_pid()

      for conn in proc.get_connections(tor_pid):
        if ("127.0.0.1", test.runner.CONTROL_PORT) == conn[:2]:
          return

      self.fail()
Exemplo n.º 3
0
    def __init__(self, rate):
        super(ResourceTracker, self).__init__(rate)

        self._resources = None
        self._use_proc = proc.is_available(
        )  # determines if we use proc or ps for lookups
        self._failure_count = 0  # number of times in a row we've failed to get results
Exemplo n.º 4
0
    def test_get_connections(self):
        """
    Checks for our control port in the stem.util.proc.get_connections output if
    we have one.
    """

        runner = test.runner.get_runner()

        if not proc.is_available():
            test.runner.skip(self, "(proc unavailable)")
            return
        elif not test.runner.Torrc.PORT in runner.get_options():
            test.runner.skip(self, "(no control port)")
            return
        elif not test.runner.get_runner().is_ptraceable():
            test.runner.skip(self, "(DisableDebuggerAttachment is set)")
            return

        # making a controller connection so that we have something to query for
        with runner.get_tor_socket():
            tor_pid = test.runner.get_runner().get_pid()

            for conn in proc.get_connections(tor_pid):
                if ("127.0.0.1", test.runner.CONTROL_PORT) == conn[:2]:
                    return

            self.fail()
Exemplo n.º 5
0
    def test_get_uid(self):
        """
    Checks that stem.util.proc.get_uid matches our tor instance's uid.
    """

        if not proc.is_available():
            test.runner.skip(self, "(proc unavailable)")
            return

        tor_pid = test.runner.get_runner().get_pid()
        self.assertEquals(os.geteuid(), proc.get_uid(tor_pid))
Exemplo n.º 6
0
  def test_get_uid(self):
    """
    Checks that stem.util.proc.get_uid matches our tor instance's uid.
    """

    if not proc.is_available():
      test.runner.skip(self, "(proc unavailable)")
      return

    tor_pid = test.runner.get_runner().get_pid()
    self.assertEquals(os.geteuid(), proc.get_uid(tor_pid))
Exemplo n.º 7
0
  def test_get_cwd(self):
    """
    Checks that stem.util.proc.get_cwd matches our tor instance's cwd.
    """

    if not proc.is_available():
      test.runner.skip(self, "(proc unavailable)")
      return
    elif not test.runner.get_runner().is_ptraceable():
      test.runner.skip(self, "(DisableDebuggerAttachment is set)")
      return

    runner = test.runner.get_runner()
    runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd()
    self.assertEquals(tor_cwd, proc.get_cwd(runner_pid))
Exemplo n.º 8
0
    def test_get_memory_usage(self):
        """
    Checks that stem.util.proc.get_memory_usage looks somewhat reasonable.
    """

        if not proc.is_available():
            test.runner.skip(self, "(proc unavailable)")
            return

        tor_pid = test.runner.get_runner().get_pid()
        res_size, vir_size = proc.get_memory_usage(tor_pid)

        # checks that they're larger than a kilobyte
        self.assertTrue(res_size > 1024)
        self.assertTrue(vir_size > 1024)
Exemplo n.º 9
0
    def test_get_cwd(self):
        """
    Checks that stem.util.proc.get_cwd matches our tor instance's cwd.
    """

        if not proc.is_available():
            test.runner.skip(self, "(proc unavailable)")
            return
        elif not test.runner.get_runner().is_ptraceable():
            test.runner.skip(self, "(DisableDebuggerAttachment is set)")
            return

        runner = test.runner.get_runner()
        runner_pid, tor_cwd = runner.get_pid(), runner.get_tor_cwd()
        self.assertEquals(tor_cwd, proc.get_cwd(runner_pid))
Exemplo n.º 10
0
  def test_get_memory_usage(self):
    """
    Checks that stem.util.proc.get_memory_usage looks somewhat reasonable.
    """

    if not proc.is_available():
      test.runner.skip(self, "(proc unavailable)")
      return

    tor_pid = test.runner.get_runner().get_pid()
    res_size, vir_size = proc.get_memory_usage(tor_pid)

    # checks that they're larger than a kilobyte
    self.assertTrue(res_size > 1024)
    self.assertTrue(vir_size > 1024)
Exemplo n.º 11
0
  def test_get_stats(self):
    """
    Checks that stem.util.proc.get_stats looks somewhat reasonable.
    """

    if not proc.is_available():
      test.runner.skip(self, "(proc unavailable)")
      return

    tor_pid = test.runner.get_runner().get_pid()
    command, utime, stime, start_time = proc.get_stats(tor_pid, 'command', 'utime', 'stime', 'start time')

    self.assertEquals('tor', command)
    self.assertTrue(float(utime) > 0)
    self.assertTrue(float(stime) > 0)
    self.assertTrue(float(start_time) > proc.get_system_start_time())
Exemplo n.º 12
0
  def test_get_stats(self):
    """
    Checks that stem.util.proc.get_stats looks somewhat reasonable.
    """

    if not proc.is_available():
      test.runner.skip(self, "(proc unavailable)")
      return

    tor_pid = test.runner.get_runner().get_pid()
    command, utime, stime, start_time = proc.get_stats(tor_pid, 'command', 'utime', 'stime', 'start time')

    self.assertEquals('tor', command)
    self.assertTrue(utime > 0)
    self.assertTrue(stime > 0)
    self.assertTrue(start_time > proc.get_system_start_time())
Exemplo n.º 13
0
  def test_stats(self):
    """
    Checks that stem.util.proc.stats looks somewhat reasonable.
    """

    if not proc.is_available():
      test.runner.skip(self, '(proc unavailable)')
      return

    tor_cmd = test.runner.get_runner().get_tor_command(True)
    tor_pid = test.runner.get_runner().get_pid()
    command, utime, stime, start_time = proc.stats(tor_pid, 'command', 'utime', 'stime', 'start time')

    self.assertEqual(tor_cmd, command)
    self.assertTrue(float(utime) > 0)
    self.assertTrue(float(stime) >= 0)
    self.assertTrue(float(start_time) > proc.system_start_time())
Exemplo n.º 14
0
    def __init__(self, processPid, resolveRate):
        """
    Initializes a new resolver daemon. When no longer needed it's suggested
    that this is stopped.
    
    Arguments:
      processPid  - pid of the process being tracked
      resolveRate - time between resolving resource usage, resolution is
                    disabled if zero
    """

        threading.Thread.__init__(self)
        self.setDaemon(True)

        self.processPid = processPid
        self.resolveRate = resolveRate

        self.cpuSampling = 0.0  # latest cpu usage sampling
        self.cpuAvg = 0.0  # total average cpu usage
        self.memUsage = 0  # last sampled memory usage in bytes
        self.memUsagePercentage = 0.0  # percentage cpu usage

        # resolves usage via proc results if true, ps otherwise
        self._useProc = proc.is_available()

        # used to get the deltas when querying cpu time
        self._lastCpuTotal = 0

        self.lastLookup = -1
        self._halt = False  # terminates thread if true
        self._valLock = threading.RLock()
        self._cond = threading.Condition()  # used for pausing the thread

        # number of successful calls we've made
        self._runCount = 0

        # sequential times we've failed with this method of resolution
        self._failureCount = 0
Exemplo n.º 15
0
 def __init__(self, processPid, resolveRate):
   """
   Initializes a new resolver daemon. When no longer needed it's suggested
   that this is stopped.
   
   Arguments:
     processPid  - pid of the process being tracked
     resolveRate - time between resolving resource usage, resolution is
                   disabled if zero
   """
   
   threading.Thread.__init__(self)
   self.setDaemon(True)
   
   self.processPid = processPid
   self.resolveRate = resolveRate
   
   self.cpuSampling = 0.0  # latest cpu usage sampling
   self.cpuAvg = 0.0       # total average cpu usage
   self.memUsage = 0       # last sampled memory usage in bytes
   self.memUsagePercentage = 0.0 # percentage cpu usage
   
   # resolves usage via proc results if true, ps otherwise
   self._useProc = proc.is_available()
   
   # used to get the deltas when querying cpu time
   self._lastCpuTotal = 0
   
   self.lastLookup = -1
   self._halt = False      # terminates thread if true
   self._valLock = threading.RLock()
   self._cond = threading.Condition()  # used for pausing the thread
   
   # number of successful calls we've made
   self._runCount = 0
   
   # sequential times we've failed with this method of resolution
   self._failureCount = 0
Exemplo n.º 16
0
def getSystemResolvers(osType = None):
  """
  Provides the types of connection resolvers available on this operating
  system.
  
  Arguments:
    osType - operating system type, fetched from the os module if undefined
  """
  
  if osType == None: osType = os.uname()[0]
  
  if osType == "FreeBSD":
    resolvers = [Resolver.BSD_SOCKSTAT, Resolver.BSD_PROCSTAT, Resolver.LSOF]
  elif osType in ("OpenBSD", "Darwin"):
    resolvers = [Resolver.LSOF]
  else:
    resolvers = [Resolver.NETSTAT, Resolver.SOCKSTAT, Resolver.LSOF, Resolver.SS]
  
  # proc resolution, by far, outperforms the others so defaults to this is able
  if proc.is_available():
    resolvers = [Resolver.PROC] + resolvers
  
  return resolvers
Exemplo n.º 17
0
def getProcessName(pid, default=None, cacheFailure=True):
    """
  Provides the name associated with the given process id. This isn't available
  on all platforms.
  
  Arguments:
    pid          - process id for the process being returned
    default      - result if the process name can't be retrieved (raises an
                   IOError on failure instead if undefined)
    cacheFailure - if the lookup fails and there's a default then caches the
                   default value to prevent further lookups
  """

    if pid in PROCESS_NAME_CACHE:
        return PROCESS_NAME_CACHE[pid]

    processName, raisedExc = "", None

    # fetch it from proc contents if available
    if proc.is_available():
        try:
            processName = proc.get_stats(pid, proc.Stat.COMMAND)[0]
        except IOError, exc:
            raisedExc = exc
Exemplo n.º 18
0
def getProcessName(pid, default = None, cacheFailure = True):
  """
  Provides the name associated with the given process id. This isn't available
  on all platforms.
  
  Arguments:
    pid          - process id for the process being returned
    default      - result if the process name can't be retrieved (raises an
                   IOError on failure instead if undefined)
    cacheFailure - if the lookup fails and there's a default then caches the
                   default value to prevent further lookups
  """
  
  if pid in PROCESS_NAME_CACHE:
    return PROCESS_NAME_CACHE[pid]
  
  processName, raisedExc = "", None
  
  # fetch it from proc contents if available
  if proc.is_available():
    try:
      processName = proc.get_stats(pid, proc.Stat.COMMAND)[0]
    except IOError, exc:
      raisedExc = exc
Exemplo n.º 19
0
  def __init__(self, rate):
    super(ResourceTracker, self).__init__(rate)

    self._resources = None
    self._use_proc = proc.is_available()  # determines if we use proc or ps for lookups
    self._failure_count = 0  # number of times in a row we've failed to get results