From 7cbe3329feadba3681a4b9dd4078877f34ff1500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergiusz=20Baza=C5=84ski?= Date: Mon, 8 Apr 2013 15:47:36 +0200 Subject: [PATCH] Add foreground running. --- adsl.py | 3 ++- bouncer.py | 50 ++++++++++++++++++++++++++------------------------ 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/adsl.py b/adsl.py index 3b43e93..4826550 100644 --- a/adsl.py +++ b/adsl.py @@ -16,6 +16,7 @@ def _get_process_list(): def _start_pppd(): + print " ".join(config.PPPD) p = subprocess.Popen(config.PPPD, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() if p.returncode != 0: @@ -27,7 +28,7 @@ def _start_pppd(): def _get_pppd_pid(): - for process, pid in _get_process_list(): + for pid, process in _get_process_list(): if process.startswith("pppd"): return pid diff --git a/bouncer.py b/bouncer.py index 4817051..6851657 100644 --- a/bouncer.py +++ b/bouncer.py @@ -1,7 +1,6 @@ #!/usr/bin/env python2 import os -import time import sys import argparse @@ -67,35 +66,38 @@ def doublefork(pidfile, logfile): if __name__ == "__main__": parser = argparse.ArgumentParser(description="Make sure the ADSL network is up and display info on the FWA fron panel.") - parser.add_argument("--pidfile", default="/var/run/adsl-bouncer.pid", help="Path of file to write dameon PID to.") - parser.add_argument("--logfile", default="/var/log/adsl-bouncer.log", help="Path of log file.") + parser.add_argument("--daemonize", default="true", help="Whether we should daemonize the bouncer.") + parser.add_argument("--pidfile", default="/var/run/adsl-bouncer.pid", help="When daemonized, the path of file to write dameon PID to.") + parser.add_argument("--logfile", default="/var/log/adsl-bouncer.log", help="When daemonized, the path of log file.") args = parser.parse_args() - # see if daemon is already running - if os.path.exists(args.pidfile): - with open(args.pidfile, "r") as f: - try: - pid = int(f.read()) - except: - pass - else: + daemonize = True if args.daemonize.lower() in ["true", "t", "yes", "y"] else False + if daemonize: + # see if daemon is already running + if os.path.exists(args.pidfile): + with open(args.pidfile, "r") as f: try: - os.kill(pid, 0) - except OSError: + pid = int(f.read()) + except: pass else: - # daemon is running - raise Exception("Daemon is already running on PID %i!" % pid) - # try to open logfile - try: - f = open(args.logfile, "aw") - f.write("Daemon starting...\n") - f.close() - except IOError as e: - raise Exception("Could not open logfile %s! %s" % (args.logfile, str(e))) + try: + os.kill(pid, 0) + except OSError: + pass + else: + # daemon is running + raise Exception("Daemon is already running on PID %i!" % pid) + # try to open logfile + try: + f = open(args.logfile, "aw") + f.write("Daemon starting...\n") + f.close() + except IOError as e: + raise Exception("Could not open logfile %s! %s" % (args.logfile, str(e))) - if not doublefork(args.pidfile, args.logfile): - raise Exception("Could not doublefork! See logfile for details.") + if not doublefork(args.pidfile, args.logfile): + raise Exception("Could not doublefork! See logfile for details.") sys.stderr.write("Daemon running! PID %i.\n" % os.getpid()) adsl.loop()