Commit df3e8fac authored by Vangelis Koukis's avatar Vangelis Koukis Committed by Faidon Liambotis

Handle pidfile properly, redirect stderr in daemon

Handle pidfile creation properly, as part of daemonization process.
Parse config file and setup logging before daemonization.
Redirect stderr to logfile upon daemonization, otherwise numerous
unexpected exceptions get lost.
parent feca7bb9
......@@ -31,6 +31,7 @@ import traceback
import subprocess
import daemon
import daemon.pidlockfile
import nfqueue
import pyinotify
......@@ -826,11 +827,6 @@ if __name__ == "__main__":
opts, args = parser.parse_args()
if opts.daemonize:
d = daemon.DaemonContext()
d.umask = 0022
d.open()
try:
config = ConfigObj(opts.config_file, configspec=config_spec)
except ConfigObjError, err:
......@@ -850,16 +846,33 @@ if __name__ == "__main__":
", ".join(section_list))
sys.exit(1)
pidfile = open(config["general"]["pidfile"], "w")
pidfile.write("%s" % os.getpid())
pidfile.close()
logger = logging.getLogger()
if opts.debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
if opts.daemonize:
logfile = os.path.join(config["general"]["logdir"], LOG_FILENAME)
handler = logging.handlers.RotatingFileHandler(logfile,
maxBytes=2097152)
else:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(LOG_FORMAT))
logger.addHandler(handler)
if opts.daemonize:
pidfile = daemon.pidlockfile.TimeoutPIDLockFile
config["general"]["pidfile"], 10)
d = daemon.DaemonContext(pidfile=pidfile,
stdout=handler.stream,
stderr=handler.stream,
files_preserve=[handler.stream])
d.umask = 0022
d.open()
logging.info("Starting up")
proxy_opts = {}
......@@ -902,16 +915,6 @@ if __name__ == "__main__":
capng.capng_change_id(uid.pw_uid, uid.pw_gid,
capng.CAPNG_DROP_SUPP_GRP|capng.CAPNG_CLEAR_BOUNDING)
if opts.daemonize:
logfile = os.path.join(config["general"]["logdir"], LOG_FILENAME)
handler = logging.handlers.RotatingFileHandler(logfile,
maxBytes=2097152)
else:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(LOG_FORMAT))
logger.addHandler(handler)
logging.info("Ready to serve requests")
try:
proxy.serve()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment