def NotifyDd(self): """Tells dd(1) to write statistics. """ if self._dd_pid is None: # Can't notify return False if not self._dd_ready: # There's a race condition between starting the program and sending # signals. The signal handler is only registered after some time, so we # have to check whether the program is ready. If it isn't, sending a # signal will invoke the default handler (and usually abort the program). if not utils.IsProcessHandlingSignal(self._dd_pid, DD_INFO_SIGNAL): logging.debug("dd is not yet ready for signal %s", DD_INFO_SIGNAL) return False logging.debug("dd is now handling signal %s", DD_INFO_SIGNAL) self._dd_ready = True logging.debug("Sending signal %s to PID %s", DD_INFO_SIGNAL, self._dd_pid) try: os.kill(self._dd_pid, DD_INFO_SIGNAL) except EnvironmentError, err: if err.errno != errno.ESRCH: raise # Process no longer exists logging.debug("dd exited") self._dd_pid = None
def _TestRealProcess(): signal.signal(signal.SIGUSR1, signal.SIG_DFL) if utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1): raise Exception("SIGUSR1 is handled when it should not be") signal.signal(signal.SIGUSR1, lambda signum, frame: None) if not utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1): raise Exception("SIGUSR1 is not handled when it should be") signal.signal(signal.SIGUSR1, signal.SIG_IGN) if utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1): raise Exception("SIGUSR1 is not handled when it should be") signal.signal(signal.SIGUSR1, signal.SIG_DFL) if utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1): raise Exception("SIGUSR1 is handled when it should not be") return True
def test(self): sp = utils.PathJoin(self.tmpdir, "status") utils.WriteFile(sp, data="\n".join([ "Name: bash", "State: S (sleeping)", "SleepAVG: 98%", "Pid: 22250", "PPid: 10858", "TracerPid: 0", "SigBlk: 0000000000010000", "SigIgn: 0000000000384004", "SigCgt: 000000004b813efb", "CapEff: 0000000000000000", ])) self.assert_(utils.IsProcessHandlingSignal(1234, 10, status_path=sp))
def testNoSuchFile(self): sp = utils.PathJoin(self.tmpdir, "notexist") self.assertFalse(utils.IsProcessHandlingSignal(1234, 10, status_path=sp))