这是我的其他职位Installing signal handler with Python的后续行动。简而言之,除非Init为特定信号安装了信号处理程序,否则Linux将阻止所有信号到PID 1(包括SIGKILL);以防止有人将终止信号发送到PID1时发生内核恐慌。我一直有的问题是,Python中的signal
模块似乎没有以系统识别的方式安装信号处理程序。我的Python Init脚本似乎完全忽略了所有信号,因为我认为它们被阻止。Linux阻止信号到Python初始化
我似乎找到了解决办法;使用在libc中安装带signal()
函数的信号处理程序(在本例中为uClibc)。下面是一个基于python的测试init。它在TTY2上打开一个shell,从中我可以发送信号到PID1进行测试。它似乎在用于测试的KVM im中工作(我愿意与任何感兴趣的人共享虚拟机)
这是解决此问题的最佳方法吗?在没有信号模块的情况下安装信号处理器有没有更好的方法? (我一点也不关心)
这是Python中的错误吗?
#!/usr/bin/python
import os
import sys
import time
from ctypes import *
def SigHUP():
print "Caught SIGHUP"
return 0
def SigCHLD():
print "Caught SIGCHLD"
return 0
SIGFUNC = CFUNCTYPE(c_int)
SigHUPFunc = SIGFUNC(SigHUP)
SigCHLDFunc = SIGFUNC(SigCHLD)
libc = cdll.LoadLibrary('libc.so.0')
libc.signal(1, SigHUPFunc) # 1 = SIGHUP
libc.signal(17, SigCHLDFunc) # 17 = SIGCHLD
print "Mounting Proc: %s" % libc.mount(None, "/proc", "proc", 0, None)
print "forking for ash"
cpid = os.fork()
if cpid == 0:
os.closerange(0, 4)
sys.stdin = open('/dev/tty2', 'r')
sys.stdout = open('/dev/tty2', 'w')
sys.stderr = open('/dev/tty2', 'w')
os.execv('/bin/ash', ('ash',))
print "ash started on tty2"
print "sleeping"
while True:
time.sleep(0.01)
这实际上属于codereview.SE,但是对于在Python中实现'init'的酷想法+1。 – 2011-04-30 20:25:17
我认为这可能是我应该发布的地方,但我没有设置这种做法,并认为可能有很多想法我没有想过。 – tMC 2011-04-30 20:30:45