-
Notifications
You must be signed in to change notification settings - Fork 0
/
selfnetd
143 lines (120 loc) · 4.3 KB
/
selfnetd
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-
import os
import sys
import shutil
import tempfile
import logging
import logging.handlers
import argparse
import signal
from daemon import daemon
from daemon import pidfile
from gi.repository import GLib
from dbus.mainloop.glib import DBusGMainLoop
sys.path.append('/usr/lib/selfnetd')
sys.path.append('/usr/lib/selfnetd/modules') # fixme
from sn_util import SnUtil
from sn_param import SnParam
from sn_manager_config import SnConfigManager
from sn_manager_local import SnLocalManager
from sn_manager_peer import SnPeerManager
from sn_dbus import DbusMainObject
def parseArgs():
argParser = argparse.ArgumentParser()
argParser.add_argument("--no-daemon", dest='daemonize', action="store_false", default=True,
help="Do not daemonize.")
argParser.add_argument("--pid-file", dest='pid_file', help="Specify location of a PID file.")
argParser.add_argument("-d", "--debug-level", dest='debug_level',
choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'], default="WARNING",
help="Set output debug message level")
return argParser.parse_args()
def sighandler_hup(signum):
global param
if param.disposeFlag == 0:
logging.debug("selfnetd: SIGHUP occured")
else:
logging.debug("selfnetd: SIGHUP ignored, in disposing")
def sighandler_int(signum):
global param
if param.disposeFlag == 0:
logging.debug("selfnetd: SIGINT occured")
SnUtil.idleInvoke(_dispose)
else:
logging.debug("selfnetd: SIGINT ignored, already in disposing")
def sighandler_term(signum):
global param
if param.disposeFlag == 0:
logging.debug("selfnetd: SIGTERM occured")
SnUtil.idleInvoke(_dispose)
else:
logging.debug("selfnetd: SIGTERM ignored, already in disposing")
def _dispose():
global param
if param.disposeFlag == 0:
param.dbusMainObject.release()
param.disposeFlag = 1
param.peerManager.dispose(_dispose)
elif param.disposeFlag == 1:
param.disposeFlag = 2
param.localManager.dispose(_dispose)
elif param.disposeFlag == 2:
param.mainloop.quit()
else:
assert False
################################################################################
parseResult = parseArgs()
# initialize SnParam
param = SnParam()
if parseResult.pid_file is not None:
param.pidFile = parseResult.pid_file
param.disposeFlag = 0
param.logLevel = parseResult.debug_level
try:
# create directory
SnUtil.mkDir(param.logDir)
SnUtil.mkDirAndClear(param.runDir)
param.tmpDir = tempfile.mkdtemp(prefix="selfnetd-")
# set logging parameter
if parseResult.daemonize:
logging.getLogger().addHandler(logging.FileHandler(param.logFile))
logging.getLogger().addHandler(logging.handlers.SysLogHandler())
else:
logging.getLogger().addHandler(logging.StreamHandler(sys.stderr))
logging.getLogger().setLevel(SnUtil.getLoggingLevel(param.logLevel))
# daemonize
dc = None
if parseResult.daemonize:
pidf = pidfile.PIDLockFile(param.pidFile)
dc = daemon.DaemonContext(pidfile=pidf)
dc.open()
try:
# create main loop
DBusGMainLoop(set_as_default=True)
param.mainloop = GLib.MainLoop()
# create managers
param.configManager = SnConfigManager(param)
param.localManager = SnLocalManager(param)
param.peerManager = SnPeerManager(param)
# create dbus root object
param.dbusMainObject = DbusMainObject(param)
# add signal handlers
GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGINT, sighandler_int, None)
GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGTERM, sighandler_term, None)
# start main loop
logging.info("selfnetd: Mainloop begins")
param.mainloop.run()
logging.info("selfnetd: Mainloop exits")
finally:
logging.shutdown()
if dc is not None:
dc.close()
except Exception as e:
logging.exception(e)
raise
finally:
# shouldn't remove log directory
if param.tmpDir is not None and os.path.exists(param.tmpDir):
shutil.rmtree(param.tmpDir)
if os.path.exists(param.runDir):
shutil.rmtree(param.runDir)