Python os.waitpid() Examples

The following are code examples for showing how to use os.waitpid(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: hostapd-mana   Author: adde88   File: forkedfunc.py    (license) View Source Project 8 votes vote down vote up
def waitfinish(self, waiter=os.waitpid):
        pid, systemstatus = waiter(self.pid, 0)
        if systemstatus:
            if os.WIFSIGNALED(systemstatus):
                exitstatus = os.WTERMSIG(systemstatus) + 128
            else:
                exitstatus = os.WEXITSTATUS(systemstatus)
        else:
            exitstatus = 0
        signal = systemstatus & 0x7f
        if not exitstatus and not signal:
            retval = self.RETVAL.open('rb')
            try:
                retval_data = retval.read()
            finally:
                retval.close()
            retval = marshal.loads(retval_data)
        else:
            retval = None
        stdout = self.STDOUT.read()
        stderr = self.STDERR.read()
        self._removetemp()
        return Result(exitstatus, signal, retval, stdout, stderr) 
Example 2
Project: spoon   Author: SpamExperts   File: server.py    (GNU General Public License v2.0) View Source Project 7 votes vote down vote up
def serve_forever(self, poll_interval=0.1):
        """Fork the current process and wait for all children to finish."""
        if self.prefork is None or self.prefork <= 1:
            return super(_SporkMixIn, self).serve_forever(
                poll_interval=poll_interval)
        pids = []
        for dummy in range(self.prefork):
            pid = os.fork()
            if not pid:
                super(_SporkMixIn, self).serve_forever(
                    poll_interval=poll_interval)
                os._exit(0)
            else:
                self.log.info("Forked worker %s", pid)
                pids.append(pid)
        self.pids = pids
        for pid in self.pids:
            _eintr_retry(os.waitpid, pid, 0) 
Example 3
Project: kinect-2-libras   Author: inessadl   File: subprocess.py    (Apache License 2.0) View Source Project 7 votes vote down vote up
def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
                _WNOHANG=os.WNOHANG, _os_error=os.error):
            """Check if child process has terminated.  Returns returncode
            attribute.

            This method is called by __del__, so it cannot reference anything
            outside of the local scope (nor can any methods it calls).

            """
            if self.returncode is None:
                try:
                    pid, sts = _waitpid(self.pid, _WNOHANG)
                    if pid == self.pid:
                        self._handle_exitstatus(sts)
                except _os_error:
                    if _deadstate is not None:
                        self.returncode = _deadstate
            return self.returncode 
Example 4
Project: core-framework   Author: RedhawkSDR   File: CommandWrapperNestedSPDDep.py    (license) View Source Project 6 votes vote down vote up
def stop(self):
        self._log.debug("stop()")
        if self.get_commandAlive() == True:
            for sig, timeout in self.STOP_SIGNALS:
                try:
                    os.kill(self._pid, sig)
                except OSError:
                    self._pid = None
                    return
                if timeout != None:
                    giveup_time = time.time() + timeout
                    while os.waitpid(self._pid, os.WNOHANG) == (0,0):
                        time.sleep(0.1)
                        if time.time() > giveup_time:
                            break
                else:
                    # Wait until there is a response
                    os.waitpid(self._pid, 0)
            self._pid = None

    ######################################
    # Implement specific property setters/getters 
Example 5
Project: core-framework   Author: RedhawkSDR   File: CommandWrapperNestedSPDDep.py    (license) View Source Project 6 votes vote down vote up
def get_commandAlive(self):
        if self._pid != None:
            try:
                os.kill(self._pid, 0)
                if os.waitpid(self._pid, os.WNOHANG) == (0,0):
                    return True
                else:
                    return False
            except OSError:
                pass
        return False 
Example 6
Project: core-framework   Author: RedhawkSDR   File: CommandWrapperSPDDep.py    (license) View Source Project 6 votes vote down vote up
def stop(self):
        self._log.debug("stop()")
        if self.get_commandAlive() == True:
            for sig, timeout in self.STOP_SIGNALS:
                try:
                    os.kill(self._pid, sig)
                except OSError:
                    self._pid = None
                    return
                if timeout != None:
                    giveup_time = time.time() + timeout
                    while os.waitpid(self._pid, os.WNOHANG) == (0,0):
                        time.sleep(0.1)
                        if time.time() > giveup_time:
                            break
                else:
                    # Wait until there is a response
                    os.waitpid(self._pid, 0)
            self._pid = None

    ######################################
    # Implement specific property setters/getters 
Example 7
Project: core-framework   Author: RedhawkSDR   File: CommandWrapperBad.py    (license) View Source Project 6 votes vote down vote up
def stop(self):
        self._log.debug("%s.stop()", self.naming_service_name)
        if self.query_commandAlive() == True:
            for sig, timeout in self.STOP_SIGNALS:
                try:
                    os.kill(self._pid, sig)
                except OSError:
                    self._pid = None
                    return
                if timeout != None:
                    giveup_time = time.time() + timeout
                    while os.waitpid(self._pid, os.WNOHANG) == (0,0):
                        time.sleep(0.1)
                        if time.time() > giveup_time:
                            break
                else:
                    # Wait until there is a response
                    os.waitpid(self._pid, 0)
            self._pid = None 
Example 8
Project: core-framework   Author: RedhawkSDR   File: CommandWrapper.py    (license) View Source Project 6 votes vote down vote up
def stop(self):
        self._log.debug("stop()")
        if self.query_commandAlive() == True:
            for sig, timeout in self.STOP_SIGNALS:
                try:
                    os.kill(self._pid, sig)
                except OSError:
                    self._pid = None
                    return
                if timeout != None:
                    giveup_time = time.time() + timeout
                    while os.waitpid(self._pid, os.WNOHANG) == (0,0):
                        time.sleep(0.1)
                        if time.time() > giveup_time:
                            break
                else:
                    # Wait until there is a response
                    os.waitpid(self._pid, 0)
            self._pid = None

    ######################################
    # Implement specific property setters/getters 
Example 9
Project: core-framework   Author: RedhawkSDR   File: CommandWrapper.py    (license) View Source Project 6 votes vote down vote up
def stop(self):
        self._log.debug("stop()")
        if self.query_commandAlive() == True:
            for sig, timeout in self.STOP_SIGNALS:
                try:
                    os.kill(self._pid, sig)
                except OSError:
                    self._pid = None
                    return
                if timeout != None:
                    giveup_time = time.time() + timeout
                    while os.waitpid(self._pid, os.WNOHANG) == (0,0):
                        time.sleep(0.1)
                        if time.time() > giveup_time:
                            break
                else:
                    # Wait until there is a response
                    os.waitpid(self._pid, 0)
            self._pid = None

    ######################################
    # Implement specific property setters/getters 
Example 10
Project: core-framework   Author: RedhawkSDR   File: CommandWrapper.py    (license) View Source Project 6 votes vote down vote up
def stop(self):
        self._log.debug("stop()")
        try:
            if self.get_commandAlive() == True:
                for sig, timeout in self.STOP_SIGNALS:
                    try:
                        os.kill(self._pid, sig)
                    except OSError:
                        self._pid = None
                        return
                    if timeout != None:
                        giveup_time = time.time() + timeout
                        while os.waitpid(self._pid, os.WNOHANG) == (0,0):
                            time.sleep(0.1)
                            if time.time() > giveup_time:
                                break
                    else:
                        # Wait until there is a response
                        os.waitpid(self._pid, 0)
                self._pid = None
        finally:
            Resource.stop(self)

    # CF::LifeCycle 
Example 11
Project: core-framework   Author: RedhawkSDR   File: CommandWrapper.py    (license) View Source Project 6 votes vote down vote up
def stop(self):
        self._log.debug("stop()")
        try:
            if self.get_commandAlive() == True:
                for sig, timeout in self.STOP_SIGNALS:
                    try:
                        os.kill(self._pid, sig)
                    except OSError:
                        self._pid = None
                        return
                    if timeout != None:
                        giveup_time = time.time() + timeout
                        while os.waitpid(self._pid, os.WNOHANG) == (0,0):
                            time.sleep(0.1)
                            if time.time() > giveup_time:
                                break
                    else:
                        # Wait until there is a response
                        os.waitpid(self._pid, 0)
                self._pid = None
        finally:
            Resource.stop(self)

    # CF::LifeCycle 
Example 12
Project: core-framework   Author: RedhawkSDR   File: CommandWrapper.py    (license) View Source Project 6 votes vote down vote up
def stop(self):
        self._log.debug("stop()")
        if self.query_prop_commandAlive() == True:
            for sig, timeout in self.STOP_SIGNALS:
                try:
                    os.kill(self._pid, sig)
                except OSError:
                    self._pid = None
                    return
                if timeout != None:
                    giveup_time = time.time() + timeout
                    while os.waitpid(self._pid, os.WNOHANG) == (0,0):
                        time.sleep(0.1)
                        if time.time() > giveup_time:
                            break
                else:
                    # Wait until there is a response
                    os.waitpid(self._pid, 0)
            self._pid = None

    ######################################
    # Implement specific property setters/getters 
Example 13
Project: seb   Author: second-reality   File: gdb.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
def kill(self):
        if not self.qemu:
            return

        try:
            self.qemu = None
            os.kill(self.current_qemu_pid, signal.SIGKILL)
            os.waitpid(self.current_qemu_pid, 0)
            self.current_qemu_pid = -1
        except OSError: # process may be finished by kill already
            pass

        LOG.debug('let gdb notice process was killed')
        try:
            execute_gdb_command('detach')
            raise RuntimeError('gdb should have disconnected and raise gdb.error')
        except gdb.error as e:
            LOG.debug('catch expected exception: ' + str(e)) 
Example 14
Project: ave   Author: sonyxperiadev   File: android_handset.py    (BSD 3-Clause "New" or "Revised" License) View Source Project 6 votes vote down vote up
def kill_cmd(self):
        if self.cmd_pid > 1:
            try:
                os.close(self.cmd_fd)
            except OSError, e:
                if e.errno == errno.EBADF:
                    pass # already closed
                else:
                    raise e
            try:
                os.kill(self.cmd_pid, signal.SIGKILL)
                os.waitpid(self.cmd_pid, 0)
            except OSError, e:
                if e.errno not in [errno.ECHILD, errno.ESRCH]:
                    raise Exception('unhandled errno: %d' % e.errno)
            self.cmd_pid = -1 
Example 15
Project: ave   Author: sonyxperiadev   File: android_handset.py    (BSD 3-Clause "New" or "Revised" License) View Source Project 6 votes vote down vote up
def kill_cmd(self):
        if self.cmd_pid > 1:
            try:
                os.close(self.cmd_fd)
            except OSError, e:
                if e.errno == errno.EBADF:
                    pass # already closed
                else:
                    raise e
            try:
                os.kill(self.cmd_pid, signal.SIGKILL)
                os.waitpid(self.cmd_pid, 0)
            except OSError, e:
                if e.errno not in [errno.ECHILD, errno.ESRCH]:
                    raise Exception('unhandled errno: %d' % e.errno)
            self.cmd_pid = -1 
Example 16
Project: ave   Author: sonyxperiadev   File: events.py    (BSD 3-Clause "New" or "Revised" License) View Source Project 6 votes vote down vote up
def _kill_ssh(self):
        if self.ssh_pid > 1:
            try:
                os.kill(self.ssh_pid, signal.SIGTERM)
                os.waitpid(self.ssh_pid, 0)
            except OSError, e:
                if e.errno not in [errno.ECHILD, errno.ESRCH]:
                    raise Exception('unhandled errno: %d' % e.errno)
            self.self_pid = -1
            try:
                os.close(self.ssh_fd)
            except OSError, e:
                if e.errno == errno.EBADF:
                    pass # already closed
                else:
                    print 'WHAT?', e
                    raise e 
Example 17
Project: ave   Author: sonyxperiadev   File: cmd.py    (BSD 3-Clause "New" or "Revised" License) View Source Project 6 votes vote down vote up
def t4():
    pretty = '%s t4' % __file__
    print(pretty)

    pid, fd = ave.cmd.run_bg('echo hello')
    poller  = select.poll()
    poller.register(fd, select.POLLIN)
    events  = poller.poll(1000) # milliseconds
    tmp     = ''
    for e in events:
        if not (e[1] & select.POLLIN):
            print('FAIL %s: unexpected poll event: %d' % (pretty, e[1]))
            os.kill(pid, signal.SIGKILL)
        tmp += os.read(fd, 1024)
    if not tmp.startswith('hello'):
        print('FAIL %s: wrong result: "%s"' % (pretty, tmp))

    os.kill(pid, signal.SIGKILL)
    os.waitpid(pid, 0)
    return True

# check that return value from executed program is correct 
Example 18
Project: ave   Author: sonyxperiadev   File: cmd.py    (BSD 3-Clause "New" or "Revised" License) View Source Project 6 votes vote down vote up
def t4():
    pretty = '%s t4' % __file__
    print(pretty)

    pid, fd = ave.cmd.run_bg('echo hello')
    poller  = select.poll()
    poller.register(fd, select.POLLIN)
    events  = poller.poll(1000) # milliseconds
    tmp     = ''
    for e in events:
        if not (e[1] & select.POLLIN):
            print('FAIL %s: unexpected poll event: %d' % (pretty, e[1]))
            os.kill(pid, signal.SIGKILL)
        tmp += os.read(fd, 1024)
    if not tmp.startswith('hello'):
        print('FAIL %s: wrong result: "%s"' % (pretty, tmp))

    os.kill(pid, signal.SIGKILL)
    os.waitpid(pid, 0)
    return True

# check that return value from executed program is correct 
Example 19
Project: darkc0de-old-stuff   Author: tuwid   File: debugger.py    (GNU General Public License v3.0) View Source Project 6 votes vote down vote up
def _waitpid(self, wanted_pid):
        if wanted_pid:
            if wanted_pid not in self.dict:
                raise PtraceError("Unknown PID: %r" % wanted_pid, pid=wanted_pid)

            debug("Wait process %s" % wanted_pid)
            try:
                pid, status = waitpid(wanted_pid, 0)
            except OSError, err:
                if err.errno == ECHILD:
                    process = self[wanted_pid]
                    raise process.processTerminated()
                else:
                    raise err
        else:
            debug("Wait any process")
            pid, status = waitpid(-1, 0)

        if wanted_pid and pid != wanted_pid:
            raise PtraceError("Unwanted PID: %r (instead of %s)"
                % (pid, wanted_pid), pid=pid)
        return pid, status 
Example 20
Project: hostapd-mana   Author: adde88   File: process.py    (license) View Source Project 6 votes vote down vote up
def reapProcess(self):
        """Try to reap a process (without blocking) via waitpid.

        This is called when sigchild is caught or a Process object loses its
        "connection" (stdout is closed) This ought to result in reaping all
        zombie processes, since it will be called twice as often as it needs
        to be.

        (Unfortunately, this is a slightly experimental approach, since
        UNIX has no way to be really sure that your process is going to
        go away w/o blocking.  I don't want to block.)
        """
        try:
            pid, status = os.waitpid(self.pid, os.WNOHANG)
        except:
            log.msg('Failed to reap %d:' % self.pid)
            log.err()
            pid = None
        if pid:
            self.processEnded(status)
            unregisterReapProcessHandler(pid, self) 
Example 21
Project: hostapd-mana   Author: adde88   File: subprocess.py    (license) View Source Project 6 votes vote down vote up
def wait(self):
            """Wait for child process to terminate.  Returns returncode
            attribute."""
            while self.returncode is None:
                try:
                    pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
                except OSError as e:
                    if e.errno != errno.ECHILD:
                        raise
                    # This happens if SIGCLD is set to be ignored or waiting
                    # for child processes has otherwise been disabled for our
                    # process.  This child is dead, we can't get the status.
                    pid = self.pid
                    sts = 0
                # Check the pid and loop as waitpid has been known to return
                # 0 even without WNOHANG in odd situations.  issue14396.
                if pid == self.pid:
                    self._handle_exitstatus(sts)
            return self.returncode 
Example 22
Project: hostapd-mana   Author: adde88   File: forking.py    (license) View Source Project 6 votes vote down vote up
def poll(self, flag=os.WNOHANG):
            if self.returncode is None:
                while True:
                    try:
                        pid, sts = os.waitpid(self.pid, flag)
                    except os.error as e:
                        if e.errno == errno.EINTR:
                            continue
                        # Child process not yet created. See #1731717
                        # e.errno == errno.ECHILD == 10
                        return None
                    else:
                        break
                if pid == self.pid:
                    if os.WIFSIGNALED(sts):
                        self.returncode = -os.WTERMSIG(sts)
                    else:
                        assert os.WIFEXITED(sts)
                        self.returncode = os.WEXITSTATUS(sts)
            return self.returncode 
Example 23
Project: rubber-docker   Author: Fewbytes   File: rd.py    (license) View Source Project 6 votes vote down vote up
def run(command):
    # TODO: replace this with fork()
    #       (https://docs.python.org/2/library/os.html#os.fork)
    pid = 0
    if pid == 0:
        # This is the child, we'll try to do some containment here
        try:
            contain(command)
        except Exception:
            traceback.print_exc()
            os._exit(1)  # something went wrong in contain()

    # This is the parent, pid contains the PID of the forked process
    # wait for the forked child and fetch the exit status
    _, status = os.waitpid(pid, 0)
    print('{} exited with status {}'.format(pid, status)) 
Example 24
Project: rubber-docker   Author: Fewbytes   File: rd.py    (license) View Source Project 6 votes vote down vote up
def run(image_name, image_dir, container_dir, command):
    container_id = str(uuid.uuid4())

    pid = os.fork()
    if pid == 0:
        # This is the child, we'll try to do some containment here
        try:
            contain(command, image_name, image_dir, container_id,
                    container_dir)
        except Exception:
            traceback.print_exc()
            os._exit(1)  # something went wrong in contain()

    # This is the parent, pid contains the PID of the forked process
    # wait for the forked child, fetch the exit status
    _, status = os.waitpid(pid, 0)
    print('{} exited with status {}'.format(pid, status)) 
Example 25
Project: rubber-docker   Author: Fewbytes   File: rd.py    (license) View Source Project 6 votes vote down vote up
def run(image_name, image_dir, container_dir, command):
    container_id = str(uuid.uuid4())
    pid = os.fork()
    if pid == 0:
        # This is the child, we'll try to do some containment here
        try:
            contain(command, image_name, image_dir, container_id,
                    container_dir)
        except Exception:
            traceback.print_exc()
            os._exit(1)  # something went wrong in contain()

    # This is the parent, pid contains the PID of the forked process
    # wait for the forked child, fetch the exit status
    _, status = os.waitpid(pid, 0)
    print('{} exited with status {}'.format(pid, status)) 
Example 26
Project: rubber-docker   Author: Fewbytes   File: rd.py    (license) View Source Project 6 votes vote down vote up
def run(image_name, image_dir, container_dir, command):
    container_id = str(uuid.uuid4())

    pid = os.fork()
    if pid == 0:
        # This is the child, we'll try to do some containment here
        try:
            contain(command, image_name, image_dir, container_id,
                    container_dir)
        except Exception:
            traceback.print_exc()
            os._exit(1)  # something went wrong in contain()

    # This is the parent, pid contains the PID of the forked process
    # wait for the forked child, fetch the exit status
    _, status = os.waitpid(pid, 0)
    print('{} exited with status {}'.format(pid, status)) 
Example 27
Project: rubber-docker   Author: Fewbytes   File: rd.py    (license) View Source Project 6 votes vote down vote up
def run(image_name, image_dir, container_dir, command):
    container_id = str(uuid.uuid4())

    pid = os.fork()
    if pid == 0:
        # This is the child, we'll try to do some containment here
        try:
            contain(command, image_name, image_dir, container_id, container_dir)
        except Exception:
            traceback.print_exc()
            os._exit(1)  # something went wrong in contain()

    # This is the parent, pid contains the PID of the forked process
    # wait for the forked child, fetch the exit status
    _, status = os.waitpid(pid, 0)
    print('{} exited with status {}'.format(pid, status)) 
Example 28
Project: rubber-docker   Author: Fewbytes   File: rd.py    (license) View Source Project 6 votes vote down vote up
def run(memory, memory_swap, cpu_shares, user, image_name, image_dir,
        container_dir, command):
    container_id = str(uuid.uuid4())

    # linux.clone(callback, flags, callback_args) is modeled after the Glibc
    # version. see: "man 2 clone"
    flags = (linux.CLONE_NEWPID | linux.CLONE_NEWNS | linux.CLONE_NEWUTS |
             linux.CLONE_NEWNET)
    callback_args = (command, image_name, image_dir, container_id,
                     container_dir, cpu_shares, memory, memory_swap, user)
    pid = linux.clone(contain, flags, callback_args)

    # This is the parent, pid contains the PID of the forked process
    # Wait for the forked child, fetch the exit status
    _, status = os.waitpid(pid, 0)
    print('{} exited with status {}'.format(pid, status)) 
Example 29
Project: rubber-docker   Author: Fewbytes   File: rd.py    (license) View Source Project 6 votes vote down vote up
def run(image_name, image_dir, container_dir, command):
    container_id = str(uuid.uuid4())

    pid = os.fork()
    if pid == 0:
        # This is the child, we'll try to do some containment here
        try:
            contain(command, image_name, image_dir, container_id, container_dir)
        except Exception:
            traceback.print_exc()
            os._exit(1)  # something went wrong in contain()

    # This is the parent, pid contains the PID of the forked process
    # wait for the forked child, fetch the exit status
    _, status = os.waitpid(pid, 0)
    print('{} exited with status {}'.format(pid, status)) 
Example 30
Project: rubber-docker   Author: Fewbytes   File: rd.py    (license) View Source Project 6 votes vote down vote up
def run(memory, memory_swap, cpu_shares, image_name, image_dir, container_dir,
        command):
    container_id = str(uuid.uuid4())

    # linux.clone(callback, flags, callback_args) is modeled after the Glibc
    # version. see: "man 2 clone"
    flags = (linux.CLONE_NEWPID | linux.CLONE_NEWNS | linux.CLONE_NEWUTS |
             linux.CLONE_NEWNET)
    callback_args = (command, image_name, image_dir, container_id,
                     container_dir, cpu_shares, memory, memory_swap)
    pid = linux.clone(contain, flags, callback_args)

    # This is the parent, pid contains the PID of the forked process
    # Wait for the forked child, fetch the exit status
    _, status = os.waitpid(pid, 0)
    print('{} exited with status {}'.format(pid, status)) 
Example 31
Project: rubber-docker   Author: Fewbytes   File: rd.py    (license) View Source Project 6 votes vote down vote up
def run(image_name, image_dir, container_dir, command):
    container_id = str(uuid.uuid4())

    # TODO: Switching to a new PID namespace (using unshare) would only affect
    #       the children of a process (because we can't change the PID of a
    #       running process), so we'll have to unshare here OR replace
    #       os.fork() with linux.clone()

    pid = os.fork()
    if pid == 0:
        # This is the child, we'll try to do some containment here
        try:
            contain(command, image_name, image_dir, container_id,
                    container_dir)
        except Exception:
            traceback.print_exc()
            os._exit(1)  # something went wrong in contain()

    # This is the parent, pid contains the PID of the forked process
    # wait for the forked child, fetch the exit status
    _, status = os.waitpid(pid, 0)
    print('{} exited with status {}'.format(pid, status)) 
Example 32
Project: deb-python-functools32   Author: openstack   File: test_support27.py    (license) View Source Project 6 votes vote down vote up
def reap_children():
    """Use this function at the end of test_main() whenever sub-processes
    are started.  This will help ensure that no extra children (zombies)
    stick around to hog resources and create problems when looking
    for refleaks.
    """

    # Reap all our dead child processes so we don't leave zombies around.
    # These hog resources and might be causing some of the buildbots to die.
    if hasattr(os, 'waitpid'):
        any_process = -1
        while True:
            try:
                # This will raise an exception on Windows.  That's ok.
                pid, status = os.waitpid(any_process, os.WNOHANG)
                if pid == 0:
                    break
            except:
                break 
Example 33
Project: spichi   Author: ifkite   File: autoreload.py    (license) View Source Project 6 votes vote down vote up
def wait_child(pid):
    while True:
        try:
            # wait for child process
            wpid, sts = os.waitpid(pid, 0)
        except KeyboardInterrupt:
            # handle exceptions when parent is waiting
            handle_parent_exit(pid)

        # if child process stopped
        if os.WIFSTOPPED(sts):
            continue
        # if receive keybord interuption or kill signal
        elif os.WIFSIGNALED(sts):
            return sts
        # seems not work
        elif os.WIFEXITED(sts):
            return sts
        else:
            raise "Not stopped, signaled or exited???" 
Example 34
Project: annotated-py-asyncio   Author: hhstore   File: test_unix_events.py    (license) View Source Project 6 votes vote down vote up
def waitpid_mocks(func):
        def wrapped_func(self):
            def patch(target, wrapper):
                return mock.patch(target, wraps=wrapper,
                                  new_callable=mock.Mock)

            with patch('os.WTERMSIG', self.WTERMSIG) as m_WTERMSIG, \
                 patch('os.WEXITSTATUS', self.WEXITSTATUS) as m_WEXITSTATUS, \
                 patch('os.WIFSIGNALED', self.WIFSIGNALED) as m_WIFSIGNALED, \
                 patch('os.WIFEXITED', self.WIFEXITED) as m_WIFEXITED, \
                 patch('os.waitpid', self.waitpid) as m_waitpid:
                func(self, WaitPidMocks(m_waitpid,
                                        m_WIFEXITED, m_WIFSIGNALED,
                                        m_WEXITSTATUS, m_WTERMSIG,
                                        ))
        return wrapped_func 
Example 35
Project: annotated-py-asyncio   Author: hhstore   File: test_unix_events.py    (license) View Source Project 6 votes vote down vote up
def test_sigchld_unhandled_exception(self, m):
        callback = mock.Mock()

        # register a child
        with self.watcher:
            self.running = True
            self.watcher.add_child_handler(57, callback)

        # raise an exception
        m.waitpid.side_effect = ValueError

        with mock.patch.object(log.logger,
                               'error') as m_error:

            self.assertEqual(self.watcher._sig_chld(), None)
            self.assertTrue(m_error.called) 
Example 36
Project: watchmen   Author: lycclsltt   File: ptyprocess.py    (license) View Source Project 6 votes vote down vote up
def __init__(self, pid, fd):
        _make_eof_intr()  # Ensure _EOF and _INTR are calculated
        self.pid = pid
        self.fd = fd
        self.fileobj = io.open(fd, 'r+b', buffering=0)

        self.terminated = False
        self.closed = False
        self.exitstatus = None
        self.signalstatus = None
        # status returned by os.waitpid
        self.status = None
        self.flag_eof = False
        # Used by close() to give kernel time to update process status.
        # Time in seconds.
        self.delayafterclose = 0.1
        # Used by terminate() to give kernel time to update process status.
        # Time in seconds.
        self.delayafterterminate = 0.1 
Example 37
Project: nojs   Author: chrisdickinson   File: xvfb.py    (license) View Source Project 6 votes vote down vote up
def Start(self):
    """Start Xvfb and set an appropriate DISPLAY environment.  Linux only.

    Copied from tools/code_coverage/coverage_posix.py
    """
    if not _IsLinux():
      return
    proc = subprocess.Popen(['Xvfb', ':9', '-screen', '0', '1024x768x24',
                             '-ac'],
                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    self._pid = proc.pid
    if not self._pid:
      raise Exception('Could not start Xvfb')
    os.environ['DISPLAY'] = ':9'

    # Now confirm, giving a chance for it to start if needed.
    for _ in range(10):
      proc = subprocess.Popen('xdpyinfo >/dev/null', shell=True)
      _, retcode = os.waitpid(proc.pid, 0)
      if retcode == 0:
        break
      time.sleep(0.25)
    if retcode != 0:
      raise Exception('Could not confirm Xvfb happiness') 
Example 38
Project: ssha   Author: claranet   File: ssh.py    (license) View Source Project 6 votes vote down vote up
def run(command):
    child_pid = os.fork()
    if child_pid == 0:
        os.execlp(command[0], *command)
    else:
        while True:
            try:
                os.waitpid(child_pid, 0)
            except OSError as error:
                if error.errno == errno.ECHILD:
                    # No child processes.
                    # It has exited already.
                    break
                elif error.errno == errno.EINTR:
                    # Interrupted system call.
                    # This happens when resizing the terminal.
                    pass
                else:
                    # An actual error occurred.
                    raise 
Example 39
Project: Intranet-Penetration   Author: yuxiaokui   File: subprocess.py    (license) View Source Project 6 votes vote down vote up
def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
                _WNOHANG=os.WNOHANG, _os_error=os.error, _ECHILD=errno.ECHILD):
            """Check if child process has terminated.  Returns returncode
            attribute.

            This method is called by __del__, so it cannot reference anything
            outside of the local scope (nor can any methods it calls).

            """
            if self.returncode is None:
                try:
                    pid, sts = _waitpid(self.pid, _WNOHANG)
                    if pid == self.pid:
                        self._handle_exitstatus(sts)
                except _os_error as e:
                    if _deadstate is not None:
                        self.returncode = _deadstate
                    if e.errno == _ECHILD:
                        # This happens if SIGCLD is set to be ignored or
                        # waiting for child processes has otherwise been
                        # disabled for our process.  This child is dead, we
                        # can't get the status.
                        # http://bugs.python.org/issue15756
                        self.returncode = 0
            return self.returncode 
Example 40
Project: Intranet-Penetration   Author: yuxiaokui   File: subprocess.py    (license) View Source Project 6 votes vote down vote up
def wait(self):
            """Wait for child process to terminate.  Returns returncode
            attribute."""
            while self.returncode is None:
                try:
                    pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
                except OSError as e:
                    if e.errno != errno.ECHILD:
                        raise
                    # This happens if SIGCLD is set to be ignored or waiting
                    # for child processes has otherwise been disabled for our
                    # process.  This child is dead, we can't get the status.
                    pid = self.pid
                    sts = 0
                # Check the pid and loop as waitpid has been known to return
                # 0 even without WNOHANG in odd situations.  issue14396.
                if pid == self.pid:
                    self._handle_exitstatus(sts)
            return self.returncode 
Example 41
Project: MKFQ   Author: maojingios   File: subprocess.py    (license) View Source Project 6 votes vote down vote up
def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
                _WNOHANG=os.WNOHANG, _os_error=os.error, _ECHILD=errno.ECHILD):
            """Check if child process has terminated.  Returns returncode
            attribute.

            This method is called by __del__, so it cannot reference anything
            outside of the local scope (nor can any methods it calls).

            """
            if self.returncode is None:
                try:
                    pid, sts = _waitpid(self.pid, _WNOHANG)
                    if pid == self.pid:
                        self._handle_exitstatus(sts)
                except _os_error as e:
                    if _deadstate is not None:
                        self.returncode = _deadstate
                    if e.errno == _ECHILD:
                        # This happens if SIGCLD is set to be ignored or
                        # waiting for child processes has otherwise been
                        # disabled for our process.  This child is dead, we
                        # can't get the status.
                        # http://bugs.python.org/issue15756
                        self.returncode = 0
            return self.returncode 
Example 42
Project: MKFQ   Author: maojingios   File: subprocess.py    (license) View Source Project 6 votes vote down vote up
def wait(self):
            """Wait for child process to terminate.  Returns returncode
            attribute."""
            while self.returncode is None:
                try:
                    pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
                except OSError as e:
                    if e.errno != errno.ECHILD:
                        raise
                    # This happens if SIGCLD is set to be ignored or waiting
                    # for child processes has otherwise been disabled for our
                    # process.  This child is dead, we can't get the status.
                    pid = self.pid
                    sts = 0
                # Check the pid and loop as waitpid has been known to return
                # 0 even without WNOHANG in odd situations.  issue14396.
                if pid == self.pid:
                    self._handle_exitstatus(sts)
            return self.returncode 
Example 43
Project: hakkuframework   Author: 4shadoww   File: support.py    (license) View Source Project 6 votes vote down vote up
def reap_children():
    """Use this function at the end of test_main() whenever sub-processes
    are started.  This will help ensure that no extra children (zombies)
    stick around to hog resources and create problems when looking
    for refleaks.
    """

    # Reap all our dead child processes so we don't leave zombies around.
    # These hog resources and might be causing some of the buildbots to die.
    if hasattr(os, 'waitpid'):
        any_process = -1
        while True:
            try:
                # This will raise an exception on Windows.  That's ok.
                pid, status = os.waitpid(any_process, os.WNOHANG)
                if pid == 0:
                    break
            except:
                break 
Example 44
Project: oa_qian   Author: sunqb   File: os.py    (license) View Source Project 6 votes vote down vote up
def _reap_children(timeout=60):
            # Remove all the dead children that haven't been waited on
            # for the *timeout* seconds.
            # Some platforms queue delivery of SIGCHLD for all children that die;
            # in that case, a well-behaved application should call waitpid() for each
            # signal.
            # Some platforms (linux) only guarantee one delivery if multiple children
            # die. On that platform, the well-behave application calls waitpid() in a loop
            # until it gets back -1, indicating no more dead children need to be waited for.
            # In either case, waitpid should be called the same number of times as dead children,
            # thus removing all the watchers when a SIGCHLD arrives. The (generous) timeout
            # is to work with applications that neglect to call waitpid and prevent "unlimited"
            # growth.
            # Note that we don't watch for the case of pid wraparound. That is, we fork a new
            # child with the same pid as an existing watcher, but the child is already dead,
            # just not waited on yet.
            now = time.time()
            oldest_allowed = now - timeout
            dead = [pid for pid, val
                    in _watched_children.items()
                    if isinstance(val, tuple) and val[2] < oldest_allowed]
            for pid in dead:
                del _watched_children[pid] 
Example 45
Project: chromium-build   Author: discordapp   File: xvfb.py    (license) View Source Project 6 votes vote down vote up
def Start(self):
    """Start Xvfb and set an appropriate DISPLAY environment.  Linux only.

    Copied from tools/code_coverage/coverage_posix.py
    """
    if not _IsLinux():
      return
    proc = subprocess.Popen(['Xvfb', ':9', '-screen', '0', '1024x768x24',
                             '-ac'],
                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    self._pid = proc.pid
    if not self._pid:
      raise Exception('Could not start Xvfb')
    os.environ['DISPLAY'] = ':9'

    # Now confirm, giving a chance for it to start if needed.
    for _ in range(10):
      proc = subprocess.Popen('xdpyinfo >/dev/null', shell=True)
      _, retcode = os.waitpid(proc.pid, 0)
      if retcode == 0:
        break
      time.sleep(0.25)
    if retcode != 0:
      raise Exception('Could not confirm Xvfb happiness') 
Example 46
Project: RealtimePythonChat   Author: quangtqag   File: os.py    (license) View Source Project 6 votes vote down vote up
def _reap_children(timeout=60):
            # Remove all the dead children that haven't been waited on
            # for the *timeout* seconds.
            # Some platforms queue delivery of SIGCHLD for all children that die;
            # in that case, a well-behaved application should call waitpid() for each
            # signal.
            # Some platforms (linux) only guarantee one delivery if multiple children
            # die. On that platform, the well-behave application calls waitpid() in a loop
            # until it gets back -1, indicating no more dead children need to be waited for.
            # In either case, waitpid should be called the same number of times as dead children,
            # thus removing all the watchers when a SIGCHLD arrives. The (generous) timeout
            # is to work with applications that neglect to call waitpid and prevent "unlimited"
            # growth.
            # Note that we don't watch for the case of pid wraparound. That is, we fork a new
            # child with the same pid as an existing watcher, but the child is already dead,
            # just not waited on yet.
            now = time.time()
            oldest_allowed = now - timeout
            dead = [pid for pid, val
                    in _watched_children.items()
                    if isinstance(val, tuple) and val[2] < oldest_allowed]
            for pid in dead:
                del _watched_children[pid] 
Example 47
Project: leetcode   Author: thomasyimgit   File: ptyprocess.py    (license) View Source Project 6 votes vote down vote up
def __init__(self, pid, fd):
        _make_eof_intr()  # Ensure _EOF and _INTR are calculated
        self.pid = pid
        self.fd = fd
        readf = io.open(fd, 'rb', buffering=0)
        writef = io.open(fd, 'wb', buffering=0, closefd=False)
        self.fileobj = io.BufferedRWPair(readf, writef)

        self.terminated = False
        self.closed = False
        self.exitstatus = None
        self.signalstatus = None
        # status returned by os.waitpid
        self.status = None
        self.flag_eof = False
        # Used by close() to give kernel time to update process status.
        # Time in seconds.
        self.delayafterclose = 0.1
        # Used by terminate() to give kernel time to update process status.
        # Time in seconds.
        self.delayafterterminate = 0.1 
Example 48
Project: zippy   Author: securesystemslab   File: subprocess.py    (license) View Source Project 6 votes vote down vote up
def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
                _WNOHANG=os.WNOHANG, _os_error=os.error):
            """Check if child process has terminated.  Returns returncode
            attribute.

            This method is called by __del__, so it cannot reference anything
            outside of the local scope (nor can any methods it calls).

            """
            if self.returncode is None:
                try:
                    pid, sts = _waitpid(self.pid, _WNOHANG)
                    if pid == self.pid:
                        self._handle_exitstatus(sts)
                except _os_error:
                    if _deadstate is not None:
                        self.returncode = _deadstate
            return self.returncode 
Example 49
Project: zippy   Author: securesystemslab   File: support.py    (license) View Source Project 6 votes vote down vote up
def reap_children():
    """Use this function at the end of test_main() whenever sub-processes
    are started.  This will help ensure that no extra children (zombies)
    stick around to hog resources and create problems when looking
    for refleaks.
    """

    # Reap all our dead child processes so we don't leave zombies around.
    # These hog resources and might be causing some of the buildbots to die.
    if hasattr(os, 'waitpid'):
        any_process = -1
        while True:
            try:
                # This will raise an exception on Windows.  That's ok.
                pid, status = os.waitpid(any_process, os.WNOHANG)
                if pid == 0:
                    break
            except:
                break 
Example 50
Project: zippy   Author: securesystemslab   File: test_uuid.py    (license) View Source Project 6 votes vote down vote up
def testIssue8621(self):
        import os
        import sys
        if os.name != 'posix':
            return

        # On at least some versions of OSX uuid.uuid4 generates
        # the same sequence of UUIDs in the parent and any
        # children started using fork.
        fds = os.pipe()
        pid = os.fork()
        if pid == 0:
            os.close(fds[0])
            value = uuid.uuid4()
            os.write(fds[1], value.hex.encode('latin1'))
            os._exit(0)

        else:
            os.close(fds[1])
            parent_value = uuid.uuid4().hex
            os.waitpid(pid, 0)
            child_value = os.read(fds[0], 100).decode('latin1')

            self.assertNotEqual(parent_value, child_value)