mitmproxy/pathod/utils.py

47 lines
1.0 KiB
Python
Raw Normal View History

import os
2014-10-24 04:21:28 +00:00
import sys
import netlib.utils
2012-04-29 00:05:38 +00:00
2016-10-17 04:29:45 +00:00
class MemBool:
2015-06-18 16:12:11 +00:00
"""
Truth-checking with a memory, for use in chained if statements.
"""
2015-05-30 00:03:13 +00:00
def __init__(self):
self.v = None
def __call__(self, v):
self.v = v
return bool(v)
data = netlib.utils.Data(__name__)
2015-05-30 00:03:13 +00:00
def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): # pragma: no cover
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
2015-05-30 00:03:13 +00:00
except OSError as e:
sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
sys.exit(1)
os.chdir("/")
os.umask(0)
os.setsid()
try:
pid = os.fork()
if pid > 0:
sys.exit(0)
2015-05-30 00:03:13 +00:00
except OSError as e:
sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
sys.exit(1)
si = open(stdin, 'rb')
so = open(stdout, 'a+b')
se = open(stderr, 'a+b', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())