Skip to content

Commit 35116a7

Browse files
committed
Improve daemon packaging
1 parent 2e7e3be commit 35116a7

File tree

2 files changed

+110
-111
lines changed

2 files changed

+110
-111
lines changed

tor2web/t2w.py

Lines changed: 107 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
from twisted.internet.task import LoopingCall
5050
from tor2web import __version__
5151
from tor2web.utils.config import Config
52-
from tor2web.utils.daemon import T2WDaemon, set_pdeathsig, set_proctitle
52+
from tor2web.utils.daemon import Daemon, set_pdeathsig, set_proctitle
5353
from tor2web.utils.hostsmap import HostsMap
5454
from tor2web.utils.lists import LimitedSizeDict, List, TorExitNodeList
5555
from tor2web.utils.mail import sendmail, sendexceptionmail
@@ -1216,6 +1216,108 @@ def registerProtocol(self, p):
12161216
except Exception:
12171217
pass
12181218

1219+
def open_listenin_socket(ip, port):
1220+
try:
1221+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1222+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1223+
s.setblocking(False)
1224+
s.bind((ip, port))
1225+
s.listen(1024)
1226+
return s
1227+
except Exception as e:
1228+
print "Tor2web Startup Failure: error while binding on %s %s (%s)" % (ip, port, e)
1229+
exit(1)
1230+
1231+
1232+
class T2WDaemon(Daemon):
1233+
def daemon_init(self):
1234+
self.quitting = False
1235+
self.subprocesses = []
1236+
1237+
self.rpc_server = T2WRPCServer(self.config)
1238+
1239+
self.socket_rpc = open_listenin_socket('127.0.0.1', 8789)
1240+
1241+
self.childFDs = {0: 0, 1: 1, 2: 2}
1242+
1243+
self.fds = []
1244+
1245+
self.fds_https = self.fds_http = ''
1246+
1247+
i_https = i_http = 0
1248+
1249+
for ip in [ipv4, ipv6]:
1250+
if ip is None:
1251+
continue
1252+
1253+
if self.config.transport in ('HTTPS', 'BOTH'):
1254+
if i_https:
1255+
self.fds_https += ','
1256+
1257+
s = open_listenin_socket(ip, self.config.listen_port_https)
1258+
self.fds.append(s)
1259+
self.childFDs[s.fileno()] = s.fileno()
1260+
self.fds_https += str(s.fileno())
1261+
i_https += 1
1262+
1263+
if self.config.transport in ('HTTP', 'BOTH'):
1264+
if i_http:
1265+
self.fds_http += ','
1266+
1267+
s = open_listenin_socket(ip, self.config.listen_port_http)
1268+
self.fds.append(s)
1269+
self.childFDs[s.fileno()] = s.fileno()
1270+
self.fds_http += str(s.fileno())
1271+
i_http += 1
1272+
1273+
def daemon_main(self):
1274+
if self.config.logreqs:
1275+
self.logfile_access = logfile.DailyLogFile.fromFullPath(os.path.join(self.config.datadir, 'logs', 'access.log'))
1276+
else:
1277+
self.logfile_access = log.NullFile()
1278+
1279+
if self.config.debugmode:
1280+
if self.config.debugtostdout and self.config.nodaemon:
1281+
self.logfile_debug = sys.stdout
1282+
else:
1283+
self.logfile_debug = logfile.DailyLogFile.fromFullPath(
1284+
os.path.join(self.config.datadir, 'logs', 'debug.log'))
1285+
else:
1286+
self.logfile_debug = log.NullFile()
1287+
1288+
log.startLogging(self.logfile_debug)
1289+
1290+
reactor.listenTCPonExistingFD = listenTCPonExistingFD
1291+
1292+
reactor.listenUNIX(os.path.join(self.config.rundir, 'rpc.socket'), factory=pb.PBServerFactory(self.rpc_server))
1293+
1294+
if not self.config.disable_gettor:
1295+
LoopingCall(getTorTask, self.config).start(3600)
1296+
1297+
for i in range(self.config.processes):
1298+
subprocess = spawnT2W(self, self.childFDs, self.fds_https, self.fds_http)
1299+
self.subprocesses.append(subprocess.pid)
1300+
1301+
def MailException(etype, value, tb):
1302+
sendexceptionmail(self.config, etype, value, tb)
1303+
1304+
if self.config.smtpmailto_exceptions:
1305+
# if self.config.smtp_mail is configured we change the excepthook
1306+
sys.excepthook = MailException
1307+
1308+
reactor.run()
1309+
1310+
def daemon_reload(self):
1311+
self.rpc_server.load_lists()
1312+
1313+
def daemon_shutdown(self):
1314+
self.quitting = True
1315+
1316+
for pid in self.subprocesses:
1317+
os.kill(pid, signal.SIGINT)
1318+
1319+
self.subprocesses = []
1320+
12191321

12201322
def start_worker():
12211323
LoopingCall(updateListsTask).start(600)
@@ -1430,122 +1532,20 @@ def test_file_access(f):
14301532

14311533
ports = []
14321534

1535+
def nullStartedConnecting(self, connector):
1536+
pass
1537+
14331538
pool = client.HTTPConnectionPool(reactor, True)
14341539
pool.maxPersistentPerHost = config.sockmaxpersistentperhost
14351540
pool.cachedConnectionTimeout = config.sockcachedconnectiontimeout
14361541
pool.retryAutomatically = config.sockretryautomatically
1437-
def nullStartedConnecting(self, connector): pass
14381542
pool._factory.startedConnecting = nullStartedConnecting
14391543

1544+
14401545
if 'T2W_FDS_HTTPS' not in os.environ and 'T2W_FDS_HTTP' not in os.environ:
14411546
set_proctitle("tor2web")
14421547

1443-
def open_listenin_socket(ip, port):
1444-
try:
1445-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1446-
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1447-
s.setblocking(False)
1448-
s.bind((ip, port))
1449-
s.listen(1024)
1450-
return s
1451-
except Exception as e:
1452-
print "Tor2web Startup Failure: error while binding on %s %s (%s)" % (ip, port, e)
1453-
exit(1)
1454-
1455-
def daemon_init(self):
1456-
self.quitting = False
1457-
self.subprocesses = []
1458-
1459-
self.socket_rpc = open_listenin_socket('127.0.0.1', 8789)
1460-
1461-
self.childFDs = {0: 0, 1: 1, 2: 2}
1462-
1463-
self.fds = []
1464-
1465-
self.fds_https = self.fds_http = ''
1466-
1467-
i_https = i_http = 0
1468-
1469-
for ip in [ipv4, ipv6]:
1470-
if ip is None:
1471-
continue
1472-
1473-
if config.transport in ('HTTPS', 'BOTH'):
1474-
if i_https:
1475-
self.fds_https += ','
1476-
1477-
s = open_listenin_socket(ip, config.listen_port_https)
1478-
self.fds.append(s)
1479-
self.childFDs[s.fileno()] = s.fileno()
1480-
self.fds_https += str(s.fileno())
1481-
i_https += 1
1482-
1483-
if config.transport in ('HTTP', 'BOTH'):
1484-
if i_http:
1485-
self.fds_http += ','
1486-
1487-
s = open_listenin_socket(ip, config.listen_port_http)
1488-
self.fds.append(s)
1489-
self.childFDs[s.fileno()] = s.fileno()
1490-
self.fds_http += str(s.fileno())
1491-
i_http += 1
1492-
1493-
1494-
def daemon_main(self):
1495-
if config.logreqs:
1496-
self.logfile_access = logfile.DailyLogFile.fromFullPath(os.path.join(config.datadir, 'logs', 'access.log'))
1497-
else:
1498-
self.logfile_access = log.NullFile()
1499-
1500-
if config.debugmode:
1501-
if config.debugtostdout and config.nodaemon:
1502-
self.logfile_debug = sys.stdout
1503-
else:
1504-
self.logfile_debug = logfile.DailyLogFile.fromFullPath(
1505-
os.path.join(config.datadir, 'logs', 'debug.log'))
1506-
else:
1507-
self.logfile_debug = log.NullFile()
1508-
1509-
log.startLogging(self.logfile_debug)
1510-
1511-
reactor.listenTCPonExistingFD = listenTCPonExistingFD
1512-
1513-
reactor.listenUNIX(os.path.join(config.rundir, 'rpc.socket'), factory=pb.PBServerFactory(self.rpc_server))
1514-
1515-
if not config.disable_gettor:
1516-
gtt = LoopingCall(getTorTask, config)
1517-
gtt.start(3600)
1518-
1519-
for i in range(config.processes):
1520-
subprocess = spawnT2W(self, self.childFDs, self.fds_https, self.fds_http)
1521-
self.subprocesses.append(subprocess.pid)
1522-
1523-
def MailException(etype, value, tb):
1524-
sendexceptionmail(config, etype, value, tb)
1525-
1526-
if config.smtpmailto_exceptions:
1527-
# if config.smtp_mail is configured we change the excepthook
1528-
sys.excepthook = MailException
1529-
1530-
reactor.run()
1531-
1532-
def daemon_reload(self):
1533-
self.rpc_server.load_lists()
1534-
1535-
def daemon_shutdown(self):
1536-
self.quitting = True
1537-
1538-
for pid in self.subprocesses:
1539-
os.kill(pid, signal.SIGINT)
1540-
1541-
self.subprocesses = []
1542-
15431548
t2w_daemon = T2WDaemon(config)
1544-
t2w_daemon.daemon_init = daemon_init
1545-
t2w_daemon.daemon_main = daemon_main
1546-
t2w_daemon.daemon_reload = daemon_reload
1547-
t2w_daemon.daemon_shutdown = daemon_shutdown
1548-
t2w_daemon.rpc_server = T2WRPCServer(config)
15491549

15501550
t2w_daemon.run()
15511551

tor2web/utils/daemon.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ def flush(self, s):
3737
pass
3838

3939

40-
class T2WDaemon(object):
40+
class Daemon(object):
4141
def __init__(self, config):
4242
self.config = config
4343

4444
def become_daemon(self):
45-
46-
if os.fork() != 0: # launch child and ...
47-
os._exit(0) # kill off parent
45+
if os.fork() != 0: # launch child and kill the parent
46+
os._exit(0)
4847

4948
os.setsid()
5049
os.chdir(self.config.rundir)

0 commit comments

Comments
 (0)