-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibs.py
executable file
·89 lines (73 loc) · 2.21 KB
/
ibs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /usr/bin/python -W ignore::: -OO
print "importing required files ...."
import signal
import sys
import os
import syslog
import traceback
from core.debug import thread_debug
from core.event import event
from core import ibs_exceptions
import core.main
RUN_PROFILER=False
PROFILER_STAT_FILENAME="/tmp/profile"
if RUN_PROFILER:
import profile
def termSigHandler(signum,frame):
event.addEvent(0,core.main.mainThreadShutdown,[],100)
def childWaitSigHandler(signum,frame):
if signum==signal.SIGUSR1: #successfully started
print "IBSng started successfully!"
sys.exit(0)
else:
print "IBSng Failed to start!"
sys.exit(1)
def handleUserDefinedSignals(handler):
signal.signal(signal.SIGUSR1,handler)
signal.signal(signal.SIGUSR2,handler)
def mainThreadInitialize():
mainThreadSignalHandlers()
print "Calling Initializer routins"
thread_debug.debug_me()
core.main.init()
def mainThreadSignalHandlers():
handleUserDefinedSignals(signal.SIG_IGN)
signal.signal(signal.SIGTERM,termSigHandler)
signal.signal(signal.SIGHUP,signal.SIG_IGN)
def logToSysLog(err_text):
syslog.openlog("IBSng",syslog.LOG_DAEMON)
syslog.syslog(syslog.LOG_ERR,err_text)
syslog.closelog()
def writePID(pid):
fp = open('/var/run/IBSng.pid','w+')
fp.write(str(pid))
fp.close()
def start():
handleUserDefinedSignals(childWaitSigHandler)
print "forking ..."
pid=os.fork()
print "IBSng started with pid=%d"%pid
if pid == 0:
try:
try:
mainThreadInitialize()
os.kill(os.getppid(),signal.SIGUSR1)
except:
print "Shutting down on error"
os.kill(os.getppid(),signal.SIGUSR2)
raise
except:
err_text=ibs_exceptions.getExceptionText()
sys.stderr.write(err_text)
logToSysLog(err_text)
core.main.mainThreadShutdown()
print "Successfully initialized, entering event loop ..."
writePID(os.getpid())
sys.setcheckinterval(500)
event.startLoop()
else:
signal.pause()
if RUN_PROFILER:
profile.run('start()',PROFILER_STAT_FILENAME)
else:
start()