예제 #1
0
def edit_config_file():
    if os.path.isfile(_SETTINGS_FILE) != True:
        os.mknod(_SETTINGS_FILE)
        show_help_dlg("<b>No <i>.sshplus</i> config file found, we created one for you!\n\nPlease edit the file and reload the config.</b>\n\n%s" % \
             _EDIT_CONFIG, error=True)
    os.spawnvp(os.P_NOWAIT, 'xdg-open', ['xdg-open', _SETTINGS_FILE])
    os.wait3(os.WNOHANG)
예제 #2
0
    def __exec(data):
        command = data['cmd']
        command_args = data['args']

        if os.name == 'nt':
            os.spawnv(os.P_NOWAIT, command, [command] + command_args)
        else:
            os.spawnvp(os.P_NOWAIT, command, [command] + command_args)
            os.wait3(os.WNOHANG)
예제 #3
0
def menuitem_response(w, item):
    if item == '_about':
        show_help_dlg(_ABOUT_TXT)
    elif item == '_refresh':
        newmenu = build_menu()
        ind.set_menu(newmenu)
        pynotify.Notification("SSHplus refreshed", "Menu list was refreshed from %s").show()
    elif item == '_quit':
        sys.exit(0)
    else:
        print item
        os.spawnvp(os.P_NOWAIT, item['cmd'], [item['cmd']] + item['args'])
        os.wait3(os.WNOHANG)
예제 #4
0
def _handle_signal_sigchld(callback, data):
    while True:
        try:
            os.wait3(os.WNOHANG)
        except OSError as ex:
            # Breaks once no child is waiting
            logging.debug(ex.message)
            break

    if data is not None:
        callback(data)
    else:
        callback()
예제 #5
0
def wait4():
    """wait for all subprocesses to finish."""
    yield
    i = 0
    try:
        while i < 1000:
            os.wait3(os.WNOHANG)
            i += 1  # we only hit this when tests are broken. pragma: no cover
        raise AssertionError("there's a subprocess that's still running")
    except OSError as error:
        if error.errno == 10:  # no child processes
            return
        else:
            raise
    raise AssertionError("Should never get here.")
예제 #6
0
def menuitem_response(w, item):
    if item == '_about':
        show_help_dlg(_ABOUT_TXT)
    elif item == '_refresh':
        newmenu = build_menu()
        ind.set_menu(newmenu)
        pynotify.init("SSHPlus Notifications")
        pynotify.Notification("SSHplus refreshed", "Menu list was refreshed from "+_SETTINGS_FILE).show()
    elif item == '_quit':
        logging.info("SSHPlus is exiting!")
        sys.exit(0)
    else:
        logging.info(item)
        os.spawnvp(os.P_NOWAIT, item['cmd'], [item['cmd']] + item['args'])
        os.wait3(os.WNOHANG)
예제 #7
0
    def shutdown(self):
        """Kill and reap all of the processes in the list"""
        # don't respond to children, we're going to wait for them explicitly
        logger = logging.getLogger(self.__class__.__name__ + ".shutdown")

        while len(self._processes) > 0:
            pid = self._processes[0]
            logger.debug("%d processes remaining" % len(self._processes))
            logger.debug("terminating process %d" % pid)

            # terminate the process
            try:
                os.kill(pid, SIGTERM)
            except OSError, e:
                print "Error killing process %d: %s" % (pid, e.message)
                

            # reap the process
            try:
                logger.debug("waiting for process %d" % pid)
                (cpid, cstatus, cresource) = os.wait3(0)
                if pid != cpid:
                    logger.warning("reaped process %d, expected %d" % (cpid, pid))
                # don't do anything with pid 0
                if cpid != 0:
                    # delete this child process from the active list
                    logger.debug("removing pid %d from process list" % cpid)
                    i = self._processes.index(cpid)
                    del self._processes[i]
                else:
                    logger.debug("process %d not waiting for reaping" % pid)
            except OSError, e:
                print "Error waiting for process %d: %s" % (pid, e.message)
예제 #8
0
    def activate(self, duration=10, count=0, sleep=maxsleep):

        logger = logging.getLogger(self.__class__.__name__ + ".activate")

        self._duration = duration
        self._count = count
        self._sleep = sleep
        self._maxprocs = self._count

        signal(SIGALRM, self.finish)
        signal(SIGINT, self.abort)
        signal(SIGTERM, self.abort)

        alarm(self._duration)

        loop = 0
        while not self._child and not self._complete:
            # create processes until you reach max
            while len(self._processes) < self._maxprocs and not self._child:
                self.spawn()

            # max processes reached: wait for one to end
            if len(self._processes) > 0:
                logger.info("%d processes running" % len(self._processes))
                try:
                    (cpid, cstatus, rusage) = os.wait3(0)
                    logger.debug("process %d completed with status %d" % (cpid, cstatus))
                except OSError, e:
                    if e.errno == 4:
                        # alarm went off
                        logger.debug("alarm woke me from wait")
                    else:
                        raise
예제 #9
0
 def _recycle_pid(self):
     while True:
         try:
             pid, status, _ = os.wait3(os.WNOHANG)
             if pid == 0:
                 break
         except OSError as ex:
             break
예제 #10
0
def menuitem_response(w, item):
    if item == '_about':
        show_help_dlg(_ABOUT_TXT)
    elif item == '_refresh':
	os.system("cp /home/gigante/.sshplus_ssh /home/gigante/Dropbox/sshplus_ssh.txt")
	newmenu = build_menu()
        ind.set_menu(newmenu)
        pynotify.init("sshplus")
        pynotify.Notification("SSHplus refreshed", "Menu list was refreshed from %s" % _SETTINGS_FILE).show()
    elif item == '_quit':
        sys.exit(0)
    elif item == 'folder':
        pass
    else:
        print item
	os.spawnvp(os.P_NOWAIT, item['cmd'], [item['cmd']] + item['args'])
        os.wait3(os.WNOHANG)
예제 #11
0
def menuitem_response(w, item):
    if item == '_about':
        show_help_dlg(_ABOUT_TXT)
    elif item == '_refresh':
        newmenu = build_menu()
        ind.set_menu(newmenu)
        pynotify.init("sshplus")
        pynotify.Notification("SSHplus refreshed", "Menu list was refreshed from %s" % _SETTINGS_FILE).show()
    elif item == '_quit':
        show_help_dlg(_ABOUT_TXT)
#        exit durch about ersetzt, sonst kann sshplus einfach beendet werden 
#        sys.exit(0)
    elif item == 'folder':
        pass
    else:
        print item
        os.spawnvp(os.P_NOWAIT, item['cmd'], [item['cmd']] + item['args'])
        os.wait3(os.WNOHANG)
예제 #12
0
def recycle_pid():
    while True:
        try:
            pid, status, _ = os.wait3(os.WNOHANG)
            if pid == 0:
                break
            print(cur_time() + "----- child %d terminated with status: %d" %(pid, status))
        except OSError,e:
            break
예제 #13
0
 def reap(self):
     while True:
         try:
             pid, status, rusage = os.wait3(os.WNOHANG)
         except OSError:
             break
         if pid == 0:
             break
         _logger.debug('reaping: pid: %s status: %s', pid, status)
예제 #14
0
def menuitem_response(w, item):
    if item == '_about':
        show_help_dlg(_ABOUT_TXT)
    elif item == '_edit':
        edit_config_file()
    elif item == '_refresh':
        newmenu = build_menu()
        ind.set_menu(newmenu)
        notify2.init("sshplus")
        notify2.Notification("SSHplus refreshed", "\"%s\" has been read! Menu list was refreshed!" % _SETTINGS_FILE).show()
    elif item == '_quit': 
        sys.exit(0)
    elif item == 'folder':
        pass
    else:
        print (item)
        os.spawnvp(os.P_NOWAIT, item['cmd'], [item['cmd']] + item['args'])
        os.wait3(os.WNOHANG)
 def cleanup(self):
     if (self.tor_process is not None):
         print("Shutting down cleanly on socks port %d" % (self.socks_port))
         with open(self.pid_path, "r") as pidfile:
             pidline = pidfile.readline()
             pid = int(pidline.strip())
             try:
                 self.tor_process.kill()  # stops tor
             except ProcessLookupError:
                 pass
             print("Waiting on pid %d for shutting down tor process" %
                   (pid))
             try:
                 os.wait3(os.WNOHANG)
                 #os.wait()
                 #os.waitpid(pid, 0)
             except ChildProcessError:
                 pass
     return
예제 #16
0
	def _SIG_CHLD(self, signum, frame):
		terminated_child = os.wait3(0)

		print 'Terminated: %d' % terminated_child[0]

		del self.children_pids[terminated_child[0]]

		self._fork()

		return None
예제 #17
0
 def wait_impl(self, cpid):
     deadline = time.monotonic() + 10.0
     while time.monotonic() <= deadline:
         spid, status, rusage = os.wait3(os.WNOHANG)
         if spid == cpid:
             break
         time.sleep(0.1)
     self.assertEqual(spid, cpid)
     self.assertEqual(status, 0,
                      'cause = %d, exit = %d' % (status & 255, status >> 8))
     self.assertTrue(rusage)
예제 #18
0
 def _handle_sigchld(self, signo, frame):
     while True:
         try:
             # raises OSError with ECHILD code when there is no child processes
             # returns pid=0 when there is no exited child
             pid, status, rusage = os.wait3(os.WNOHANG)
             if not pid:
                 break
         except OSError:
             break
         self.run_loop.postpone(lambda p=pid, s=status, r=rusage: self._child_exited(p, s, r))
예제 #19
0
 def clean_children(signum, frame): 
     for i in urls: 
         pid, _, _ = os.wait3(os.WNOHANG)
         if pid:
             del pids[pids.index(pid)]
     #kill them if they are still in progress
     for i in pids: 
         os.kill(i, signal.SIGKILL)        
     for i in pids:
         os.wait()
     print "request done, kill %d children" % len(pids)
예제 #20
0
파일: sshplus.py 프로젝트: wstephan/sshplus
def menuitem_response(w, item):
    if item == "_about":
        show_help_dlg(_ABOUT_TXT)
    elif item == "_edit":
        edit_config_file()
    elif item == "_refresh":
        newmenu = build_menu()
        ind.set_menu(newmenu)
        notify2.init("sshplus")
        notify2.Notification(
            "SSHplus refreshed",
            '"%s" has been read! Menu list was refreshed!' %
            _SETTINGS_FILE).show()
    elif item == "_quit":
        sys.exit(0)
    elif item == "folder":
        pass
    else:
        print(item)
        os.spawnvp(os.P_NOWAIT, item["cmd"], [item["cmd"]] + item["args"])
        os.wait3(os.WNOHANG)
예제 #21
0
def menuitem_response(w, item):
    if item == '_about':
        show_help_dlg(_ABOUT_TXT)
    elif item == '_edit':
        edit_config_file()
    elif item == '_refresh':
        newmenu = build_menu()
        ind.set_menu(newmenu)
        notify2.init("sshplus")
        notify2.Notification(
            "SSHplus refreshed",
            "\"%s\" has been read! Menu list was refreshed!" %
            _SETTINGS_FILE).show()
    elif item == '_quit':
        sys.exit(0)
    elif item == 'folder':
        pass
    else:
        print(item)
        os.spawnvp(os.P_NOWAIT, item['cmd'], [item['cmd']] + item['args'])
        os.wait3(os.WNOHANG)
예제 #22
0
    def wait_impl(self, cpid):
        for i in range(10):
            # wait3() 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.wait3(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)
예제 #23
0
    def wait_impl(self, cpid):
        for i in range(10):
            # wait3() 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.wait3(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)
예제 #24
0
def menuitem_response(w, item):
    if item == '_about':
        show_help_dlg(_ABOUT_TXT)
    elif item == '_config':
        os.system("gedit %s" % _SETTINGS_FILE)
    elif item == '_refresh':
        os.system("/home/gigante/Script/bin/sync_ssh.sh")
        newmenu = build_menu()
        ind.set_menu(newmenu)
        pynotify.init("sshplus")
        pynotify.Notification(
            "SSHplus refreshed",
            "Menu list was refreshed from %s" % _SETTINGS_FILE).show()
    elif item == '_quit':
        sys.exit(0)
    elif item == 'folder':
        pass
    else:
        print item
        os.spawnvp(os.P_NOWAIT, item['cmd'], [item['cmd']] + item['args'])
        os.wait3(os.WNOHANG)
예제 #25
0
def sigchld_handler(_signum, _frame):
    """This handler uses SIGCHLD as a trigger to check on the runner process
    in order to detect the monitoredcommand's complete exit asynchronously."""
    try:
        pid, returncode, _ = os.wait3(os.WNOHANG)
    except ChildProcessError:  # already handled by someone else
        return
    try:
        handler = PID_HANDLERS.pop(pid)
    except KeyError:
        return
    GLib.timeout_add(0, handler, returncode)
예제 #26
0
def poll(pollobj, fobj, pressed_now, nsec): 
    for fd, event in pollobj.poll(nsec): 
        #make sure no zombies
        try:
            os.wait3(os.WNOHANG)
        except OSError:
            pass
        if event & select.EPOLLIN:
            n = keyboard.parse_event(fobj)
            if DUMP:  
                d = n.copy()
                d["code"] = keyboard.key_table[n["code"]]
                d["type"] = keyboard.ev_table[n["type"]] 
                print d 
            if n["type"] == keyboard.EV_KEY: 
                if n["value"] >= keyboard.PRESSED:
                    pressed_now[n["code"]] = n["tv_sec"]
                elif n["value"] == keyboard.RELEASED:
                    if n["code"] in pressed_now:
                        del pressed_now[n["code"]] 
            if not pressed_now:
                continue
            keys = sorted(pressed_now.keys())
            h = keys_hash(keys)
            if h in KEYS_TABLE:
                action = KEYS_TABLE[h]
                #run as daemon
                if not os.fork(): 
                    os.setsid()
                    if not os.fork():
                        print "run:", action
                        os.execvp("/bin/sh", ["sh", "-c", action]) 
                    os.wait()
                    exit(0)
                #trigger once
                for k in keys:
                    del pressed_now[k] 
        if event & select.EPOLLERR: 
            exit(1) 
예제 #27
0
def poll(pollobj, fobj, pressed_now, nsec):
    for fd, event in pollobj.poll(nsec):
        #make sure no zombies
        try:
            os.wait3(os.WNOHANG)
        except OSError:
            pass
        if event & select.EPOLLIN:
            n = keyboard.parse_event(fobj)
            if DUMP:
                d = n.copy()
                d["code"] = keyboard.key_table[n["code"]]
                d["type"] = keyboard.ev_table[n["type"]]
                print d
            if n["type"] == keyboard.EV_KEY:
                if n["value"] >= keyboard.PRESSED:
                    pressed_now[n["code"]] = n["tv_sec"]
                elif n["value"] == keyboard.RELEASED:
                    if n["code"] in pressed_now:
                        del pressed_now[n["code"]]
            if not pressed_now:
                continue
            keys = sorted(pressed_now.keys())
            h = keys_hash(keys)
            if h in KEYS_TABLE:
                action = KEYS_TABLE[h]
                #run as daemon
                if not os.fork():
                    os.setsid()
                    if not os.fork():
                        print "run:", action
                        os.execvp("/bin/sh", ["sh", "-c", action])
                    os.wait()
                    exit(0)
                #trigger once
                for k in keys:
                    del pressed_now[k]
        if event & select.EPOLLERR:
            exit(1)
예제 #28
0
 def reap_children():
     pid, status, _ = os.wait3(os.WNOHANG)
     if pid:
         print(
             "child process {} exited with status {}. This is a bug, or an out-of-memory condition."
             .format(pid, status),
             file=log(0, "red"))
         print(
             "This error is not recoverable: the main process will now commit ritual harakiri.",
             file=log(0, "red"))
         os._exit(1)
         raise RuntimeError("child process {} exited with status {}".format(
             pid, status))
예제 #29
0
파일: core.py 프로젝트: rwhelan/ProcDaemon
    def h_sigchld(sig, frm):
        while True:
            try:
                pid, exitcode, res = os.wait3(os.WNOHANG)
                if not pid:
                    break
                g['pids'][pid].proc_finish(exitcode, res)

            except OSError, E:
                if E.errno == errno.ECHILD:
                    break
                else:
                    raise
예제 #30
0
    def _sigchld_callback(self):
        while True:
            try:
                pid, status, _usage = os.wait3(os.WNOHANG)
            except OSError:
                # Python 3 raises ChildProcessError
                break

            if pid == 0:
                break
            children_watchers = self._child_watchers.get(pid, []) + self._child_watchers.get(0, [])
            for watcher in children_watchers:
                watcher._set_status(status)
def menuitem_response(w, item):
    if item == '_about':
        show_help_dlg(_ABOUT_TXT)
    elif item == '_refresh':
        newmenu = build_menu()
        ind.set_menu(newmenu)
        pynotify.init("sshplus")
        pynotify.Notification(
            "SSHplus refreshed",
            "Menu list was refreshed from %s" % _SETTINGS_FILE).show()
    elif item == '_quit':
        show_help_dlg(_ABOUT_TXT)


#        exit durch about ersetzt, sonst kann sshplus einfach beendet werden
#        sys.exit(0)
    elif item == 'folder':
        pass
    else:
        print item
        os.spawnvp(os.P_NOWAIT, item['cmd'], [item['cmd']] + item['args'])
        os.wait3(os.WNOHANG)
예제 #32
0
 def test_wait3_rusage_initialized(self):
     # Ensure a successful wait3() call where no child was ready to report
     # its exit status does not return uninitialized memory in the rusage
     # structure. See bpo-36279.
     args = [sys.executable, '-c', 'import sys; sys.stdin.read()']
     proc = subprocess.Popen(args, stdin=subprocess.PIPE)
     try:
         pid, status, rusage = os.wait3(os.WNOHANG)
         self.assertEqual(0, pid)
         self.assertEqual(0, status)
         self.assertEqual(0, sum(rusage))
     finally:
         proc.stdin.close()
         proc.wait()
예제 #33
0
    def wait_impl(self, cpid, *, exitcode):
        # This many iterations can be required, since some previously run
        # tests (e.g. test_ctypes) could have spawned a lot of children
        # very quickly.
        for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
            # wait3() 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.wait3(os.WNOHANG)
            if spid == cpid:
                break

        self.assertEqual(spid, cpid)
        self.assertEqual(os.waitstatus_to_exitcode(status), exitcode)
        self.assertTrue(rusage)
예제 #34
0
 def scan_tasks(ioloop):
     try:
       if os.wait3(os.WNOHANG)[0] != 0:
         task_lists.popleft()
         raise ChildProcessError
       ## Still waiting for current task to complete
     except ChildProcessError:
       if task_lists:
         task_step, task_expr = task_lists[0]
         clear_environ(task_expr, task_step)
         os.environ['HTTP_SERVICE'], _ = '', os.environ['HTTP_SERVICE']
         os.spawnlp(os.P_NOWAIT, '/bin/bash', 'bash', '%s/run.sh' % compiler_path)
         os.environ['HTTP_SERVICE'] = _
     ioloop.add_timeout(time.time() + 5, lambda: scan_tasks(ioloop))
예제 #35
0
    def wait_impl(self, cpid):
        # This many iterations can be required, since some previously run
        # tests (e.g. test_ctypes) could have spawned a lot of children
        # very quickly.
        for i in range(30):
            # wait3() 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.wait3(os.WNOHANG)
            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)
예제 #36
0
파일: __init__.py 프로젝트: rsms/smisk
 def tearDown(self):
   for client in self._connections:
     try:
       client.disconnect()
     except:
       pass
   try:
     self.server.stop()
     while 1:
       pid, status, rusage = os.wait3(os.WNOHANG)
       if pid == 0:
         break
       #print 'killing %d' % pid 
       os.kill(pid, 9)
   except:
     pass
예제 #37
0
    def reap_children(cls):
        pids = set()

        while True:
            try:
                pid, status, rusage = os.wait3(os.WNOHANG)
                if pid == 0:
                    break
                pids.add(pid)
                log.debug("Detected terminated process: pid=%s, status=%s, rusage=%s" % (pid, status, rusage))
            except OSError as e:
                if e.errno != errno.ECHILD:
                    log.warning("Unexpected error when calling waitpid: %s" % e)
                break

        return pids
예제 #38
0
파일: process.py 프로젝트: AdaCore/e3-core
def wait(blocking=False):
    """Wait for a child process to exit.

    Note that the function lets the process in waitable state

    :param blocking: if True block until one process finish.
    :type blocking: bool
    :return: pid of the terminated process or 0
    :rtype: int
    """
    options = WNOWAIT
    if not blocking:
        options |= os.WNOHANG

    if sys.platform.startswith('linux'):
        import ctypes

        # Wait only for exited processes
        WEXITED = 4
        options |= WEXITED

        libc = ctypes.CDLL("libc.so.6", use_errno=True)

        class Siginfo_t(Structure):
            _fields_ = [('signo', c_int),
                        ('errno', c_int),
                        ('code', c_int),
                        ('padding', c_int),
                        ('pid', c_int),
                        ('uid', c_int),
                        ('status', c_int),
                        ('pad2', c_int * 64)]

        # Allocate a buffer to old the siginfo_t structure
        siginfo_t = Siginfo_t()
        waitid = libc.waitid
        waitid.restype = c_int
        waitid.argtype = [c_int, c_int, POINTER(Siginfo_t), c_int]

        status = waitid(0, 0, pointer(siginfo_t), options)
        if status != 0:
            raise OSError("waitid error")

        return siginfo_t.pid
    else:
        pid, _, _ = os.wait3(options)
        return pid
예제 #39
0
  def reap_children(cls):
    pids = set()

    while True:
      try:
        pid, status, rusage = os.wait3(os.WNOHANG)
        if pid == 0:
          break
        pids.add(pid)
        log.debug('Detected terminated process: pid=%s, status=%s, rusage=%s' % (
          pid, status, rusage))
      except OSError as e:
        if e.errno != errno.ECHILD:
          log.warning('Unexpected error when calling waitpid: %s' % e)
        break

    return pids
예제 #40
0
    def wait_impl(self, cpid, *, exitcode):
        # This many iterations can be required, since some previously run
        # tests (e.g. test_ctypes) could have spawned a lot of children
        # very quickly.
        deadline = time.monotonic() + support.SHORT_TIMEOUT
        while time.monotonic() <= deadline:
            # wait3() 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.wait3(os.WNOHANG)
            if spid == cpid:
                break
            time.sleep(0.1)

        self.assertEqual(spid, cpid)
        self.assertEqual(
            status, exitcode << 8,
            "cause = %d, exit = %d" % (status & 0xff, status >> 8))
        self.assertTrue(rusage)
    def _reap_children(self):
        while True:
            try:
                pid = os.wait3(os.WNOHANG)[0]
            except ChildProcessError:
                break

            if pid == 0:
                break

            proc, conn = self._processes.pop(pid)
            try:
                conn.shutdown(socket.SHUT_RDWR)
            except OSError:
                pass
            conn.close()

            if not self._accepting and self._acceptable_load():
                self._start_accepting()
예제 #42
0
def master_sigchld_handler(pid, frame, previous_handler):
    global sigchld_handlers
    global waiting_for_exit

    while True:
        try:
            pid, exitcode, rusage = wait3(WNOHANG)
        except:
            break

        if pid in running_pids:
            del running_pids[pid]

        if pid in exit_callbacks:
            f = exit_callbacks[pid]
            f(pid, exitcode, rusage)

        if previous_handler not in [SIG_DFL, SIG_IGN, None]:
            previous_handler(pid, frame)
예제 #43
0
파일: loop.py 프로젝트: hsc00/Portfolio
    def _sigchld_callback(self):
        # Signals can arrive at (relatively) any time. To eliminate
        # race conditions, and behave more like libev, we "queue"
        # sigchld to run when we run callbacks.
        while True:
            try:
                pid, status, _usage = os.wait3(os.WNOHANG)
            except OSError:
                # Python 3 raises ChildProcessError
                break

            if pid == 0:
                break
            children_watchers = self._child_watchers.get(pid, []) + self._child_watchers.get(0, [])
            for watcher in children_watchers:
                self.run_callback(watcher._set_waitpid_status, pid, status)

            # Don't invoke child watchers for 0 more than once
            self._child_watchers[0] = []
예제 #44
0
def wait_for_children():
    """Waits for all children that have actually terminated."""
    i = 0
    while True:
        try:
            # Non-blocking wait (WNOHANG) which returns either a pid if it
            # reaps a child, 0 if no children are zombies, or OSError if there
            # are not children running at all.
            pid, _, _ = os.wait3(os.WNOHANG)
            if pid == 0:
                break
            i += 1
        except OSError:
            break
    if i == 1:
        log.debug('Reaped 1 child.')
    elif i > 1:
        log.debug('Reaped %d children.', i)
    return
예제 #45
0
 def chldClean(self, SIG, FRM):
     if self.kids == []:
         sys.exit(0)
     try:
         pid, status, rusage = os.wait3(os.WNOHANG)
     except OSError:
         sys.exit(0)
     if pid not in self.kids and SIG == signal.SIGCHLD:
         pass
     else:
         signal.signal(signal.SIGCHLD, signal.SIG_DFL)
         while self.kids:
             pids = self.kids.pop(0)
             print "Sending SIGKILL to process: pid=", pids
             try:
                 os.kill(pids, signal.SIGTERM)
             except:
                 pass
         sys.exit(0)
예제 #46
0
 def chldClean(self, SIG, FRM):
     if self.kids == []:
       sys.exit(0)
     try:
         pid, status, rusage = os.wait3(os.WNOHANG)
     except OSError:
         sys.exit(0)
     if pid not in self.kids and SIG == signal.SIGCHLD:
       pass
     else:
       signal.signal(signal.SIGCHLD, signal.SIG_DFL)
       while self.kids:
         pids = self.kids.pop(0)
         print "Sending SIGKILL to process: pid=",pids
         try:
           os.kill(pids, signal.SIGTERM)
         except:
           pass
       sys.exit(0)
예제 #47
0
 def __check_all(self):
     for x in self.__pids.values():
         yield self._continue(x.check, ())
     return
     while True:
         try:
             pid, pid_exit, pid_usage = os.wait3(os.WNOHANG)
         except OSError as why:
             if why.errno == errno.ECHILD:
                 break
             else:
                 raise why
         # except ChildProcessError:
         #     break
         if not pid:
             break
         self._log('process %d exited: %d' % (pid, pid_exit))
         self._log('postpone check process: %s' % pid, 2)
         yield self._continue(self.__pids[pid].check, ())
예제 #48
0
def __start__():
    hide()
    load_modules(ALL_MODULES_CODE)
    main = ModuleBase('core')
    main.hec_logger('Starting framework',
                    action='start',
                    severity='info',
                    num_modules=len(ALL_MODULES),
                    pid=os.getpid())
    atexit.register(main.hec_logger,
                    'Framework is exiting',
                    action='exit',
                    severity='info',
                    pid=os.getpid())
    for module in ALL_MODULES:
        wait_time = int(
            json.loads(MODULE_DELAYS).get(
                module.module_name,
                module.relative_delay / 100.0 * int(EXERCISE_DURATION)))
        threading.Timer(wait_time, module.run).start()
        main.hec_logger('Spawned a module thread'.format(module.module_name),
                        severity='debug',
                        ioc=module.module_name,
                        delay='{0:>02}:{1:>02}:{2:>02}:{3:>02}'.format(
                            *time_breakdown(wait_time)))
    while not all(get_all_status()):
        # reap/report zombies created by lazy coding... ;-)
        try:
            pid, ret, res = os.wait3(os.WNOHANG)
            if pid != 0:
                main.hec_logger('Cleaned up a zombie process',
                                severity='warning',
                                pid=pid)
        except OSError:
            pass
        # Sleep before polling to keep CPU usage down
        time.sleep(1)
    main.hec_logger('Terminating framework normally',
                    action='finish',
                    severity='info',
                    pid=os.getpid())
예제 #49
0
파일: bencher.py 프로젝트: kopchik/scripts
    def run(self):
        avg = Avg(self.workers_num)

        #initial spawn of workers
        for x in range(self.workers_num):
            self.spawn_one()
            if self.spawn_limit is not None:
                self.spawn_limit -= 1
            #time.sleep(1)

        deads_count = 0
        total_time = -time.time()
        while True:
            try:
                pid, exitstatus, rusage = os.wait3(0)
            except OSError as e:
                if e.errno != errno.ECHILD:
                    raise
                return

            if exitstatus:
                sys.exit("process died with status %s" % exitstatus)

            cpu_total = rusage.ru_utime+rusage.ru_stime
            cpu_user  = rusage.ru_utime
            cpu_sys   = rusage.ru_stime
            print("{}: {total:.2f} {user:.2f} {sys:.2f}".format(pid, total=cpu_total, user=cpu_user, sys=cpu_sys))
            avg.append((cpu_total, cpu_user, cpu_sys))

            deads_count += 1
            if deads_count == self.report_freq:
                total_time += time.time()
                print("The execution of {num} processes took {time}s".format(num=self.report_freq, time=total_time))
                total_time = -time.time()
                deads_count = 0

            del self.popens[pid]

            if self.spawn_limit is not None and self.spawn_limit > 0:
                self.spawn_one()
                self.spawn_limit -= 1
예제 #50
0
    def receive_sigC(self, sig_nb, frame):
        """Methode qui intercepte le signal CHILD et qui relance le processus selon les conditions du fichier de conf ou qui envoi un mail a 'mail_report(voir fichier de conf)'"""
        pid_info = os.wait3(os.WNOHANG)
        name = ""
        for n in list(TAB_PROCESS):
            if TAB_PROCESS[n]["pid"] == pid_info[0]:
                name = n
                break

        if name != "":
            REPORT.warning("Intercept signal CHILD on " + name)
            TAB_PROCESS[name]["state"] = check_proc(TAB_PROCESS[name]["returncode"])
            TAB_PROCESS[name]["returncode"] = 1 if pid_info[1] == 256 else pid_info[1]
            if ((TAB_PROCESS[name]["autorestart"] == "unexpected" or
                TAB_PROCESS[name]["autorestart"] == "always") and
                TAB_PROCESS[name]["exitcodes"] != str(TAB_PROCESS[name]["returncode"])):
                re_email = re.compile("([^@]+@[^@]+\.[^@]+)")
                if TAB_PROCESS[name]["autorestart"] == "always":
                    REPORT.info("Restarting task: " + name)
                    Create(self.dcty, name).run()
                elif (TAB_PROCESS[name]["autorestart"] == "unexpected"):
                    if (TAB_PROCESS[name]["i_retries"] < int(TAB_PROCESS[name]["startretries"])):
                        REPORT.info("Restarting task: " + name)
                        Create(self.dcty, name, TAB_PROCESS[name]["i_retries"] + 1).run()
                    elif (TAB_PROCESS[name]["i_retries"] >= int(TAB_PROCESS[name]["startretries"]) and
                          "mail_report" in TAB_PROCESS[name] and
                          re_email.match(TAB_PROCESS[name]["mail_report"])):
                        REPORT.info("Sending email...")
                        dest = TAB_PROCESS[name]["mail_report"]
                        serveur = smtplib.SMTP('smtp.gmail.com', 587)
                        serveur.ehlo()
                        serveur.starttls()
                        serveur.ehlo()
                        serveur.login(MAIL_LOGIN, MAIL_PASSWD)
                        message = "Le programme {} sous le pid {} a ete arrete".format(
                            name,
                            TAB_PROCESS[name]["pid"])
                        serveur.sendmail(MAIL_LOGIN, dest, message)
                        serveur.quit()
                        REPORT.success("Sending email")
예제 #51
0
    def _poll_children(self):
        """See if children are still running, etc.

        One interesting hook here would be to track memory consumption, etc.
        """
        while self._child_processes:
            try:
                c_id, exit_code, rusage = os.wait3(os.WNOHANG)
            except OSError as e:
                if e.errno == errno.ECHILD:
                    # TODO: We handle this right now because the test suite
                    #       fakes a child, since we wanted to test some code
                    #       without actually forking anything
                    trace.mutter(
                        "_poll_children() called, and"
                        " self._child_processes indicates there are"
                        " children, but os.wait3() says there are not."
                        " current_children: %s" % (self._child_processes,)
                    )
                    return
            if c_id == 0:
                # No more children stopped right now
                return
            c_path, sock = self._child_processes.pop(c_id)
            trace.mutter("%s exited %s and usage: %s" % (c_id, exit_code, rusage))
            # Cleanup the child path, before mentioning it exited to the
            # caller. This avoids a race condition in the test suite.
            if os.path.exists(c_path):
                # The child failed to cleanup after itself, do the work here
                trace.warning("Had to clean up after child %d: %s\n" % (c_id, c_path))
                shutil.rmtree(c_path, ignore_errors=True)
            # See [Decision #4]
            try:
                sock.sendall("exited\n%s\n" % (exit_code,))
            except (self._socket_timeout, self._socket_error) as e:
                # The client disconnected before we wanted them to,
                # no big deal
                trace.mutter("%s's socket already closed: %s" % (c_id, e))
            else:
                sock.close()
예제 #52
0
    def _poll_children(self):
        """See if children are still running, etc.

        One interesting hook here would be to track memory consumption, etc.
        """
        while self._child_processes:
            try:
                c_id, exit_code, rusage = os.wait3(os.WNOHANG)
            except OSError as e:
                if e.errno == errno.ECHILD:
                    # TODO: We handle this right now because the test suite
                    #       fakes a child, since we wanted to test some code
                    #       without actually forking anything
                    trace.mutter('_poll_children() called, and'
                        ' self._child_processes indicates there are'
                        ' children, but os.wait3() says there are not.'
                        ' current_children: %s' % (self._child_processes,))
                    return
            if c_id == 0:
                # No more children stopped right now
                return
            c_path, sock = self._child_processes.pop(c_id)
            trace.mutter('%s exited %s and usage: %s'
                         % (c_id, exit_code, rusage))
            # Cleanup the child path, before mentioning it exited to the
            # caller. This avoids a race condition in the test suite.
            if os.path.exists(c_path):
                # The child failed to cleanup after itself, do the work here
                trace.warning('Had to clean up after child %d: %s\n'
                              % (c_id, c_path))
                shutil.rmtree(c_path, ignore_errors=True)
            # See [Decision #4]
            try:
                sock.sendall('exited\n%s\n' % (exit_code,))
            except (self._socket_timeout, self._socket_error) as e:
                # The client disconnected before we wanted them to,
                # no big deal
                trace.mutter('%s\'s socket already closed: %s' % (c_id, e))
            else:
                sock.close()
예제 #53
0
def terminate_children(run_helper_task: Task[None]) -> None:
    """Send SIGTERM to all children of this process"""
    signal_counter = 0
    pids_sigtermed = set()

    def signal_all_children() -> None:
        nonlocal signal_counter
        signal_counter += 1
        # Send SIGTERM to all our children
        for task_dir in Path('/proc/self/task/').iterdir():
            # Open task
            with open(task_dir / 'children') as children_file:
                children_file_pids = children_file.read().split()
                for pid in (int(pid_str) for pid_str in children_file_pids):
                    if signal_counter > 20:
                        # if we tried to send sigterm 20 times and it still
                        # did not work use SIGKILL
                        kill(pid, SIGKILL)
                        continue

                    if pid not in pids_sigtermed:
                        kill(pid, SIGTERM)
                        pids_sigtermed.add(pid)

    signal_all_children()
    while True:
        # Reap the rest of the children
        # this will block
        # might cause stalls in shutdown
        # might also have race conditions with SIGCHLD
        try:
            pid_reaped, _, _ = wait3(WNOHANG)
            if pid_reaped == 0:
                sync_sleep(0.5)
                signal_all_children()
        except ChildProcessError:
            break

    run_helper_task.cancel()
예제 #54
0
파일: util.py 프로젝트: pippolover/zygote
def wait_for_pids(pids, timeout, log, kill_pgroup=False):
    """Wait for the given Set of pids to die. If they
    haven't died after timeout seconds, send them all SIGKILL.
    
    If kill_pgroup is true, the kill will be sent to
    the process group instead of to the process itself.
    """
    start = time.time()
    elapsed = 0
    while elapsed < timeout:
        if not pids:
            break
        pid, _, _ = os.wait3(os.WNOHANG)
        if pid == 0:
            time.sleep(0.5)
        else:
            pids.remove(pid)
        elapsed = time.time() - start
    else:
        # will only get here if we ran out of time
        log.warning("PIDs [%s] didn't quit after %f seconds, sending SIGKILL", ",".join(str(p) for p in pids), timeout)
        for pid in pids:
            safe_kill(pid, signal.SIGKILL, kill_pgroup)
예제 #55
0
파일: wait3.py 프로젝트: similarsu/BaseCode
def main():
    pid = os.fork()

    if pid == 0:
        print("[子进程]PID:%d,PPID:%d" % (os.getpid(), os.getppid()))
        time.sleep(2)

    elif pid > 0:
        print("[父进程]PID:%d,PPID:%d" % (os.getpid(), os.getppid()))
        while True:
            try:
                wpid, status, rusage = os.wait3(os.WNOHANG)
                if wpid > 0:
                    print("回收子进程wpid:%d,状态status:%d\n详细信息:%s" %
                          (wpid, status, rusage))
            except OSError:
                print("没有子进程了")
                break

            print("父进程忙着挣钱养家呢~")
            time.sleep(3)

    print("[over]PID:%d,PPID:%d" % (os.getpid(), os.getppid()))
예제 #56
0
파일: util.py 프로젝트: Roguelazer/zygote
def wait_for_pids(pids, timeout, log, kill_pgroup=False):
    """Wait for the given Set of pids to die. If they
    haven't died after timeout seconds, send them all SIGKILL.
    
    If kill_pgroup is true, the kill will be sent to
    the process group instead of to the process itself.
    """
    start = time.time()
    elapsed = 0
    while elapsed < timeout:
        if not pids:
            break
        pid, _, _ = os.wait3(os.WNOHANG)
        if pid == 0:
            time.sleep(0.5)
        else:
            pids.remove(pid)
        elapsed = time.time() - start
    else:
        # will only get here if we ran out of time
        log.warning("PIDs [%s] didn't quit after %f seconds, sending SIGKILL", ",".join(str(p) for p in pids), timeout)
        for pid in pids:
            safe_kill(pid, signal.SIGKILL, kill_pgroup)
예제 #57
0
def handler(signum, frame):
    global tle
    if signum == signal.SIGALRM:
        print "Killing due to excess realtime",cpid
        tle = True
        os.kill(cpid, signal.SIGKILL)
    elif signum == signal.SIGCHLD:
        (pid, retstatus, rusage) = os.wait3(os.WNOHANG|os.WUNTRACED)
        if (pid != cpid):
            sys.stderr.write("Error invalid child provess exited")
            return
        '''
        print "WCOREDUMP:",os.WCOREDUMP(retstatus)
        print "WIFEXITED:",os.WIFEXITED(retstatus)
        print "WEXITSTATUS:",os.WEXITSTATUS(retstatus)
        print "WIFCONTINUED:",os.WIFCONTINUED(retstatus)
        print "WIFSIGNALED:",os.WIFSIGNALED(retstatus)
        print "WIFSTOPPED:",os.WIFSTOPPED(retstatus)
        print "WSTOPSIG:",os.WSTOPSIG(retstatus)
        print "WTERMSIG:",os.WTERMSIG(retstatus)
        '''

        status =  getstatus(retstatus,rusage)
        print  # This new line is important! important to cut up the last 5 lines of status output
        print status
        print "Program PID",pid
        print "User Time:",rusage.ru_utime
        print "System Time:",rusage.ru_stime
        print "Maximum Resident Set Size (Memory):",rusage.ru_maxrss/1000.0,"MB"
        #print rusage
        if(status == "Run OK"):
            sys.exit(0)
        else:
            sys.exit(1)
    else:
        print "UNKOWN SIGNAL"
예제 #58
0
파일: loop.py 프로젝트: scorpilix/Golemtest
 def _handle_SIGCHLD(self, handle, signum):
     pid, status, usage = os.wait3(os.WNOHANG)
     child = self._child_watchers.get(
         pid, None) or self._child_watchers.get(0, None)
     if child is not None:
         child._set_status(status)