def resume(self):
"""Resume the process by sending a SIGCONT to the child
This method is a no-op on Windows
"""
try:
self.command_process.send_signal(signal.SIGCONT)
except (ValueError, AttributeError, OSError):
pass
python类SIGCONT的实例源码
def resume(self):
"""Resume the process by sending a SIGCONT to the child"""
try:
self.command_process.stdin.write("CONT\n")
self.command_process.stdin.flush()
except IOError as exc:
if exc.errno == 32: # Broken pipe, guard exited
return
raise
item = self.resp_queue.get()
if item[1] != "CONT" and item[1] is not None:
raise SandboxError("Bad response from jailguard after resume, %s"
% (item,))
def terminate(self, force=False):
'''This forces a child process to terminate. It starts nicely with
SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This
returns True if the child was terminated. This returns False if the
child could not be terminated. '''
if not self.isalive():
return True
try:
self.kill(signal.SIGHUP)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
self.kill(signal.SIGCONT)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
self.kill(signal.SIGINT)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
if force:
self.kill(signal.SIGKILL)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
else:
return False
return False
except OSError:
# I think there are kernel timing issues that sometimes cause
# this to happen. I think isalive() reports True, but the
# process is dead to the kernel.
# Make one last attempt to see if the kernel is up to date.
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
else:
return False
def terminate(self, force=False):
'''This forces a child process to terminate. It starts nicely with
SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This
returns True if the child was terminated. This returns False if the
child could not be terminated. '''
if not self.isalive():
return True
try:
self.kill(signal.SIGHUP)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
self.kill(signal.SIGCONT)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
self.kill(signal.SIGINT)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
if force:
self.kill(signal.SIGKILL)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
else:
return False
return False
except OSError:
# I think there are kernel timing issues that sometimes cause
# this to happen. I think isalive() reports True, but the
# process is dead to the kernel.
# Make one last attempt to see if the kernel is up to date.
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
else:
return False
def terminate(self, force=False):
'''This forces a child process to terminate. It starts nicely with
SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This
returns True if the child was terminated. This returns False if the
child could not be terminated. '''
if not self.isalive():
return True
try:
self.kill(signal.SIGHUP)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
self.kill(signal.SIGCONT)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
self.kill(signal.SIGINT)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
if force:
self.kill(signal.SIGKILL)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
else:
return False
return False
except OSError:
# I think there are kernel timing issues that sometimes cause
# this to happen. I think isalive() reports True, but the
# process is dead to the kernel.
# Make one last attempt to see if the kernel is up to date.
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
else:
return False
def terminate(self, force=False):
"""This forces a child process to terminate. It starts nicely with
SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This
returns True if the child was terminated. This returns False if the
child could not be terminated. """
if not self.isalive():
return True
try:
self.kill(signal.SIGHUP)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
self.kill(signal.SIGCONT)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
self.kill(signal.SIGINT)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
if force:
self.kill(signal.SIGKILL)
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
else:
return False
return False
except OSError, e:
# I think there are kernel timing issues that sometimes cause
# this to happen. I think isalive() reports True, but the
# process is dead to the kernel.
# Make one last attempt to see if the kernel is up to date.
time.sleep(self.delayafterterminate)
if not self.isalive():
return True
else:
return False
def signal(self, sig):
"""
Signal the process of an event.
"""
if sig == signal.SIGKILL:
self.kill()
elif sig == signal.SIGTERM:
self.terminate()
elif sig == signal.SIGSTOP:
self.pause()
elif sig == signal.SIGCONT:
self.unpause()
else:
self.on_signal(sig)
def unpause(self):
# continue is a reserved word
"""
Continue the process after it's been paused
"""
self.signal(signal.SIGCONT)
def kill_and_wait(pid, pidfile):
# The NGAS server should nicely shut down itself after receiving this signal
os.kill(pid, signal.SIGTERM)
# We previously checked that the pidfile was gone, but this is not enough
# In some cases the main thread finished correctly, but there are other
# threads (e.g., HTTP servicing threads) that are still running,
# and thus the PID file disappears but the process is still ongoing
tries = 0
max_tries = 20
sleep_period = 1
start = time.time()
while tries < max_tries:
# SIGCONT can be sent many times without fear of side effects
# If we get ESRCH then the process has finished
try:
os.kill(pid, signal.SIGCONT)
except OSError as e:
if e.errno == errno.ESRCH:
# Bingo! the process is gone
break
tries += 1
time.sleep(sleep_period)
sys.stdout.write('.')
sys.stdout.flush()
sys.stdout.write('\n')
sys.stdout.flush()
end = time.time()
ret = 0x00
# We waited a lot but the process is still there, kill it with 9
if tries == max_tries:
err("Process didn't die after %.2f [s], killing it with -9" % (end-start))
os.kill(pid, signal.SIGKILL)
ret += 0x01
# The process should have removed its own pidfile...
if os.path.exists(pidfile):
err("Removing PID file manually, the daemon process didn't remove it by itself")
os.unlink(pidfile)
ret += 0x02
return ret