Exemple #1
0
    def testWeakRefSet(self):

        c1 = utils.TimeBasedCache()
        c2 = utils.TimeBasedCache()

        self.assertIn(c1, utils.TimeBasedCache.active_caches)
        self.assertIn(c2, utils.TimeBasedCache.active_caches)

        l = len(utils.TimeBasedCache.active_caches)

        del c1

        # This should work even though the weak ref to c1 should be gone.
        utils.TimeBasedCache.house_keeper_thread.target()

        # Make sure it's actually gone.
        self.assertLess(len(utils.TimeBasedCache.active_caches), l)
Exemple #2
0
    def test05TimeBasedCache(self):

        key = "key"
        tested_cache = utils.TimeBasedCache(max_age=50)
        with test_lib.FakeTime(100):

            # Stop the housekeeper thread - we test it explicitely here
            tested_cache.exit = True
            tested_cache.Put(key, "hello")

            self.assertEqual(tested_cache.Get(key), "hello")

        with test_lib.FakeTime(160):

            # Force the housekeeper to run
            tested_cache.house_keeper_thread.target()

            # This should now be expired
            self.assertRaises(KeyError, tested_cache.Get, key)
Exemple #3
0
  def __init__(self,
               queues=queues_config.WORKER_LIST,
               threadpool_prefix="grr_threadpool",
               threadpool_size=None,
               token=None):
    """Constructor.

    Args:
      queues: The queues we use to fetch new messages from.
      threadpool_prefix: A name for the thread pool used by this worker.
      threadpool_size: The number of workers to start in this thread pool.
      token: The token to use for the worker.

    Raises:
      RuntimeError: If the token is not provided.
    """
    logging.info("started worker with queues: %s", str(queues))
    self.queues = queues

    # self.queued_flows is a timed cache of locked flows. If this worker
    # encounters a lock failure on a flow, it will not attempt to grab this flow
    # until the timeout.
    self.queued_flows = utils.TimeBasedCache(max_size=10, max_age=60)

    if token is None:
      raise RuntimeError("A valid ACLToken is required.")

    # Make the thread pool a global so it can be reused for all workers.
    if self.__class__.thread_pool is None:
      if threadpool_size is None:
        threadpool_size = config.CONFIG["Threadpool.size"]

      self.__class__.thread_pool = threadpool.ThreadPool.Factory(
          threadpool_prefix, min_threads=2, max_threads=threadpool_size)

      self.__class__.thread_pool.Start()

    self.token = token
    self.last_active = 0
    self.last_mh_lease_attempt = rdfvalue.RDFDatetime.FromSecondsSinceEpoch(0)

    # Well known flows are just instantiated.
    self.well_known_flows = flow.WellKnownFlow.GetAllWellKnownFlows(token=token)
Exemple #4
0
    def testTimeBasedCacheSingleThread(self):

        utils.TimeBasedCache()
        num_threads = threading.active_count()
        utils.TimeBasedCache()
        self.assertEqual(threading.active_count(), num_threads)
Exemple #5
0
import logging
import os
import platform
import re
import sys
import threading

from grr_response_client import client_utils
from grr_response_client import vfs
from grr.core.grr_response_core.lib import utils
from grr.core.grr_response_core.lib.rdfvalues import paths as rdf_paths

# File handles are cached here. They expire after a couple minutes so
# we don't keep files locked on the client.
FILE_HANDLE_CACHE = utils.TimeBasedCache(max_age=300)


class LockedFileHandle(object):
  """An object which encapsulates access to a file."""

  def __init__(self, filename, mode="rb"):
    self.lock = threading.RLock()
    self.fd = open(filename, mode)
    self.filename = filename

  def Seek(self, offset, whence=0):
    self.fd.seek(offset, whence)

  def Read(self, length):
    return self.fd.read(length)
Exemple #6
0
Fichier : vfs.py Projet : qsdj/grr
#!/usr/bin/env python
"""This file implements a VFS abstraction on the client."""

import os

from grr import config
from grr_response_client import client_utils
from grr.core.grr_response_core.lib import registry
from grr.core.grr_response_core.lib import utils
from grr.core.grr_response_core.lib.rdfvalues import paths as rdf_paths

# A central Cache for vfs handlers. This can be used to keep objects alive
# for a limited time.
DEVICE_CACHE = utils.TimeBasedCache()


class VFSHandler(object):
    """Base class for handling objects in the VFS."""
    supported_pathtype = -1

    # Should this handler be auto-registered?
    auto_register = False

    size = 0
    offset = 0

    # This is the VFS path to this specific handler.
    path = "/"

    # This will be set by the VFSOpen factory to the pathspec of the final
    # destination of this handler. This pathspec will be case corrected and