Esempio n. 1
0
def t03(w):
    pretty = '%s t3' % __file__
    print(pretty)

    pid_path = w.make_tempfile()
    log_path = w.make_tempfile()
    log_file = open(log_path, 'w')
    d        = MockDaemon(pid_path, log_file)

    d.start()
    pid = d.get_pid()

    ok = False
    for i in range(10):
        proc = Process(pid)
        if isinstance(type(proc).ppid, property):
            ppid = proc.ppid
        else:
            ppid = proc.ppid()
        if ppid == 1:
            ok = True
            break
        time.sleep(0.3)
    if not ok:
        print('FAIL %s: daemon not reparented: ppid=%s' % (pretty, ppid))

    d.stop()

    return ok
Esempio n. 2
0
def list_parent_processes(pid):
    pid = int(pid)
    parents = {pid}

    try:
        proc = Process(pid)
        ppid = proc.ppid()

        while ppid > 0:
            parents.add(ppid)
            proc = Process(ppid)
            ppid = proc.ppid()
    except Exception:
        pass

    return parents
Esempio n. 3
0
def _rkill(process: psutil.Process, *, dry=True) -> List[KillVictim]:
    victims = []

    if process.is_running():

        with process.oneshot():

            ppid = process.ppid()
            pid = process.pid
            name = process.name()

        for child_process in process.children(recursive=True):
            victims.extend(_rkill(child_process, dry=dry))

        victims.append(KillVictim(ppid, pid, name, ("SKIPPED" if dry else _kill(process))))

    return victims
Esempio n. 4
0
def t07(w):
    pretty = '%s t7' % __file__
    print(pretty)

    class CrashDaemon(Daemon):
        def run(self):
            time.sleep(1)
            raise Exception('good news everyone!')

    pid_path = w.make_tempfile()
    log_path = w.make_tempfile()
    log_file = open(log_path, 'w')
    d        = CrashDaemon(pid_path, log_file, stderr=log_file)

    d.start()

    proc = Process(d.get_pid())
    ok   = True
    if isinstance(type(proc).ppid, property):
        ppid = proc.ppid
    else:
        ppid = proc.ppid()
    if ppid != 1:
        print('FAIL %s: daemon not reparented: ppid=%d' % (pretty, ppid))
        ok = False

    join(d.get_pid(), 5)

    if os.path.exists(pid_path):
        print('FAIL %s: crashed daemon did not remove its pid file' % pretty)
        return False

    with open(log_path) as f:
        contents = f.read()
        if 'good news everyone!' not in contents:
            print('FAIL %s: stack trace not logged:\n%s' % (pretty, contents))
            return False

    return True
Esempio n. 5
0
def get_chaos_temp_dir() -> str:
    """
    Create a temporary directory unique to each chaos experiment.

    The temporary directory will take the form <tempdir>/chaosindy.<pid>
    The <pid> will be the chaos processe's pid iff it exists. Otherwise, the
    subprocess's pid.

    :return: str
    """
    # Get current process info
    myp = Process()
    subprocess_pid = myp.pid
    chaos_pid = None
    # Walk all the way up the process tree
    while (1):
        #  Break when we find the 'chaos' process
        if myp.name() == 'chaos':
            logger.debug("Found 'chaos' process")
            chaos_pid = myp.pid
            break
        try:
            myp = Process(myp.ppid())
            logger.debug("myp.name=%s", myp.name())
        except NoSuchProcess as e:
            logger.info("Did not find chaos pid before traversing all the way" \
                        " to the top of the process tree! Defaulting to %s",
                        subprocess_pid)
            logger.exception(e)
            chaos_pid = subprocess_pid
            break

    logger.debug("subprocess pid: %s chaos pid: %s", subprocess_pid, chaos_pid)
    tempdir_path = "{}/chaosindy.{}".format(tempfile.gettempdir(), chaos_pid)
    tempdir = makedirs(tempdir_path, exist_ok=True)
    logger.debug("tempdir: %s", tempdir_path)
    return tempdir_path
Esempio n. 6
0
    def track_process(self, p: psutil.Process):
        with p.oneshot():
            key = None
            if p.pid in self.pids:
                key = self.pids[p.pid]
                if self.processes[key].name != p.name():
                    key = None
            if key is None:
                key = len(self.processes)
                self.processes.append(ProcessInfo(key,
                                               p.pid,
                                               p.name()))
                self.pids[p.pid] = key
                self.data.update({
                    f'process.{key}.name': p.name(),
                    f'process.{key}.pid': p.pid,
                    f'process.{key}.ppid': p.ppid(),
                    f'process.{key}.create_time': p.create_time(),
                })

                try:
                    self.data.update({
                        f'process.{key}.exe': p.exe(),
                    })
                except (psutil.AccessDenied, psutil.ZombieProcess):
                    pass

                try:
                    self.data.update({
                        f'process.{key}.cmdline': '\n'.join(p.cmdline()),
                    })
                except (psutil.AccessDenied, psutil.ZombieProcess):
                    pass

            self.processes[key].active = True

            try:
                res = p.memory_info()
                self.data.update({
                    f'process.{key}.rss': res.rss,
                    f'process.{key}.vms': res.vms,
                })
            except (psutil.AccessDenied, psutil.ZombieProcess):
                pass

            try:
                res = p.cpu_times()
                self.data.update({
                    f'process.{key}.user': res.user,
                    f'process.{key}.system': res.system,
                })
                if hasattr(res, 'iowait'):
                    self.data.update({
                        f'process.{key}.iowait': res.iowait,
                    })
            except (psutil.AccessDenied, psutil.ZombieProcess):
                pass

            try:
                res = p.cpu_percent()
                self.data.update({
                    f'process.{key}.cpu': res,
                })
            except (psutil.AccessDenied, psutil.ZombieProcess):
                pass

            try:
                res = p.num_threads()
                self.data.update({
                    f'process.{key}.threads': res,
                })
            except (psutil.AccessDenied, psutil.ZombieProcess):
                pass