Python os.wait4() Examples

The following are 21 code examples of os.wait4(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module os , or try the search function .
Example #1
Source File: baseexecutor.py    From benchexec with Apache License 2.0 6 votes vote down vote up
def _wait_for_process(self, pid, name):
        """Wait for the given process to terminate.
        @return tuple of exit code and resource usage
        """
        try:
            logging.debug("Waiting for process %s with pid %s", name, pid)
            unused_pid, exitcode, ru_child = os.wait4(pid, 0)
            return exitcode, ru_child
        except OSError as e:
            if self.PROCESS_KILLED and e.errno == errno.EINTR:
                # Interrupted system call seems always to happen
                # if we killed the process ourselves after Ctrl+C was pressed
                # We can try again to get exitcode and resource usage.
                logging.debug(
                    "OSError %s while waiting for termination of %s (%s): %s.",
                    e.errno,
                    name,
                    pid,
                    e.strerror,
                )
                try:
                    unused_pid, exitcode, ru_child = os.wait4(pid, 0)
                    return exitcode, ru_child
                except OSError:
                    pass  # original error will be handled and this ignored

            logging.critical(
                "OSError %s while waiting for termination of %s (%s): %s.",
                e.errno,
                name,
                pid,
                e.strerror,
            )
            return 0, None 
Example #2
Source File: test_wait4.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def wait_impl(self, cpid):
        option = os.WNOHANG
        if sys.platform.startswith('aix'):
            # Issue #11185: wait4 is broken on AIX and will always return 0
            # with WNOHANG.
            option = 0
        for i in range(10):
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, option)
            if spid == cpid:
                break
            time.sleep(1.0)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
        self.assertTrue(rusage) 
Example #3
Source File: test_wait4.py    From oss-ftp with MIT License 6 votes vote down vote up
def wait_impl(self, cpid):
        option = os.WNOHANG
        if sys.platform.startswith('aix'):
            # Issue #11185: wait4 is broken on AIX and will always return 0
            # with WNOHANG.
            option = 0
        for i in range(10):
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, option)
            if spid == cpid:
                break
            time.sleep(1.0)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
        self.assertTrue(rusage) 
Example #4
Source File: test_wait4.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def wait_impl(self, cpid):
        option = os.WNOHANG
        if sys.platform.startswith('aix'):
            # Issue #11185: wait4 is broken on AIX and will always return 0
            # with WNOHANG.
            option = 0
        deadline = time.monotonic() + 10.0
        while time.monotonic() <= deadline:
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, option)
            if spid == cpid:
                break
            time.sleep(0.1)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
        self.assertTrue(rusage) 
Example #5
Source File: test_wait4.py    From android_universal with MIT License 6 votes vote down vote up
def wait_impl(self, cpid):
        option = os.WNOHANG
        if sys.platform.startswith('aix'):
            # Issue #11185: wait4 is broken on AIX and will always return 0
            # with WNOHANG.
            option = 0
        deadline = time.monotonic() + 10.0
        while time.monotonic() <= deadline:
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, option)
            if spid == cpid:
                break
            time.sleep(0.1)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
        self.assertTrue(rusage) 
Example #6
Source File: test_wait4.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def wait_impl(self, cpid):
        option = os.WNOHANG
        if sys.platform.startswith('aix'):
            # Issue #11185: wait4 is broken on AIX and will always return 0
            # with WNOHANG.
            option = 0
        for i in range(10):
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, option)
            if spid == cpid:
                break
            time.sleep(1.0)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
        self.assertTrue(rusage) 
Example #7
Source File: test_wait4.py    From gcblue with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def wait_impl(self, cpid):
        option = os.WNOHANG
        if sys.platform.startswith('aix'):
            # Issue #11185: wait4 is broken on AIX and will always return 0
            # with WNOHANG.
            option = 0
        for i in range(10):
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, option)
            if spid == cpid:
                break
            time.sleep(1.0)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
        self.assertTrue(rusage) 
Example #8
Source File: test_wait4.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def wait_impl(self, cpid):
        option = os.WNOHANG
        if sys.platform.startswith('aix'):
            # Issue #11185: wait4 is broken on AIX and will always return 0
            # with WNOHANG.
            option = 0
        deadline = time.monotonic() + 10.0
        while time.monotonic() <= deadline:
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, option)
            if spid == cpid:
                break
            time.sleep(0.1)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
        self.assertTrue(rusage) 
Example #9
Source File: test_wait4.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def wait_impl(self, cpid):
        for i in range(10):
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, os.WNOHANG)
            if spid == cpid:
                break
            time.sleep(1.0)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
        self.assertTrue(rusage) 
Example #10
Source File: executable.py    From westpa with MIT License 5 votes vote down vote up
def exec_child(self, executable, environ=None, stdin=None, stdout=None, stderr=None, cwd=None):
        '''Execute a child process with the environment set from the current environment, the
        values of self.addtl_child_environ, the random numbers returned by self.random_val_env_vars, and
        the given ``environ`` (applied in that order). stdin/stdout/stderr are optionally redirected.
        
        This function waits on the child process to finish, then returns
        (rc, rusage), where rc is the child's return code and rusage is the resource usage tuple from os.wait4()'''
        
        all_environ = dict(os.environ)
        all_environ.update(self.addtl_child_environ)
        all_environ.update(self.random_val_env_vars())
        all_environ.update(environ or {})
        
        stdin  = open(stdin, 'rb') if stdin else sys.stdin        
        stdout = open(stdout, 'wb') if stdout else sys.stdout
        if stderr == 'stdout':
            stderr = stdout
        else:
            stderr = open(stderr, 'wb') if stderr else sys.stderr
                
        # close_fds is critical for preventing out-of-file errors
        proc = subprocess.Popen([executable],
                                cwd = cwd,
                                stdin=stdin, stdout=stdout, stderr=stderr if stderr != stdout else subprocess.STDOUT,
                                close_fds=True, env=all_environ)

        # Wait on child and get resource usage
        (_pid, _status, rusage) = os.wait4(proc.pid, 0)
        # Do a subprocess.Popen.wait() to let the Popen instance (and subprocess module) know that
        # we are done with the process, and to get a more friendly return code
        rc = proc.wait()
        return (rc, rusage) 
Example #11
Source File: eintr_tester.py    From android_universal with MIT License 5 votes vote down vote up
def test_wait4(self):
        self._test_wait_single(lambda pid: os.wait4(pid, 0)) 
Example #12
Source File: test_wait4.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def wait_impl(self, cpid):
        for i in range(10):
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, os.WNOHANG)
            if spid == cpid:
                break
            time.sleep(1.0)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
        self.assertTrue(rusage) 
Example #13
Source File: mamaji.py    From procszoo with GNU General Public License v3.0 5 votes vote down vote up
def main():
    args = get_options()

    if args.show_id:
        show_current_users_and_groups()
        sys.exit(0)

    mamaji_data = fetch_mamaji_data(args)
    filter_options(mamaji_data)

    target_cmd = None
    if args.cmd:
        target_cmd = args.cmd

    if not args.do_fork:
        change_users_and_groups(mamaji_data)
        # if target_cmd is None, do nothing
        if target_cmd:
            os.execlp(target_cmd[0], *target_cmd)
        sys.exit(0)

    if args.do_fork and not args.cmd:
        target_cmd = [find_shell()]

    pid = os.fork()
    if pid == -1:
        warn('failed to do fork')
        sys.exit(1)
    elif pid == 0:
        change_users_and_groups(mamaji_data)
        os.execlp(target_cmd[0], *target_cmd)
    else:
        status = os.wait4(pid, 0)[1] >> 8
        sys.exit(status) 
Example #14
Source File: test_wait4.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def wait_impl(self, cpid):
        for i in range(10):
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, os.WNOHANG)
            if spid == cpid:
                break
            time.sleep(1.0)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
        self.assertTrue(rusage) 
Example #15
Source File: eintr_tester.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_wait4(self):
        self._test_wait_single(lambda pid: os.wait4(pid, 0)) 
Example #16
Source File: benchmark.py    From hase with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def main() -> None:

    result_file = Path(args.outdir).joinpath("result.json").resolve()
    results: Dict[str, Any] = {args.name: {"original": [], "hase": []}}

    for i in range(args.n):
        results[args.name]["hase"].append(dict(run=i, result=[], valid=True))
        with TemporaryDirectory() as tempdir:
            temppath = Path(tempdir)
            recording = record(
                target=args.args,
                record_path=temppath,
                log_path=temppath.joinpath("logs"),
                stdout=open(f"{args.outdir}/{args.name}_hase_{i}.out", "w"),
                limit=1,
            )
            if recording:
                rusage = recording.rusage
                if rusage:
                    results[args.name]["hase"][i]["result"].append(list(rusage))

        results[args.name]["original"].append(dict(run=i, result=[], valid=True))

        process = subprocess.Popen(
            args.args, stdout=open(f"{args.outdir}/{args.name}_{i}.out", "w")
        )
        _, _, rusage = os.wait4(process.pid, 0)
        if rusage:
            results[args.name]["original"][i]["result"].append(list(rusage))

    with open(result_file, "w") as file:
        json.dump(results, file, sort_keys=True, indent=4, separators=(",", ": "))
        file.write("\n")

    return 
Example #17
Source File: __init__.py    From hase with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def record_child_pid(
    pid: int, record_paths: "RecordPaths", timeout: Optional[int] = None
) -> Recording:

    if timeout is None:
        options = 0
    else:
        options = os.WNOHANG

    record = RecordProcess(pid, record_paths)

    with record:
        ptrace_detach(pid)
        start = time.time()

        while True:
            pid, exit_code, rusage = os.wait4(pid, options)
            if pid != 0:
                break
            elif timeout is not None and time.time() - start <= 0:
                raise TimeoutExpired(
                    "process did not finish within {} seconds".format(timeout)
                )
            time.sleep(0.10)
        coredump, trace = record.result()
        return Recording(coredump, trace, exit_code, rusage) 
Example #18
Source File: gtp_controller.py    From goreviewpartner with GNU General Public License v3.0 5 votes vote down vote up
def close(self):
        # Errors from closing pipes or wait4() are unlikely, but possible.

        # Ideally would give up waiting after a while and forcibly terminate the
        # subprocess.
        errors = []
        try:
            self.command_pipe.close()
        except EnvironmentError, e:
            errors.append("error closing command pipe:\n%s" % e) 
Example #19
Source File: eintr_tester.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_wait4(self):
        self._test_wait_single(lambda pid: os.wait4(pid, 0)) 
Example #20
Source File: test_wait4.py    From BinderFilter with MIT License 5 votes vote down vote up
def wait_impl(self, cpid):
        for i in range(10):
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, os.WNOHANG)
            if spid == cpid:
                break
            time.sleep(1.0)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
        self.assertTrue(rusage) 
Example #21
Source File: container.py    From benchexec with Apache License 2.0 5 votes vote down vote up
def wait_for_child_and_forward_signals(child_pid, process_name):
    """Wait for a child to terminate and in the meantime forward all signals
    that the current process receives to this child.
    @return a tuple of exit code and resource usage of the child as given by os.waitpid
    """
    block_all_signals()

    while True:
        logging.debug("Waiting for signals")
        signum = signal.sigwait(_ALL_SIGNALS)
        if signum == signal.SIGCHLD:
            pid, exitcode, ru_child = os.wait4(-1, os.WNOHANG)
            while pid != 0:
                if pid == child_pid:
                    return exitcode, ru_child
                else:
                    logging.debug("Received unexpected SIGCHLD for PID %s", pid)
                pid, exitcode, ru_child = os.wait4(-1, os.WNOHANG)

        else:
            _forward_signal(signum, child_pid, process_name)