From 302759a996e82fb8761b3a789154a5108a41773f Mon Sep 17 00:00:00 2001 From: Narayan Desai Date: Fri, 30 Nov 2007 18:59:26 +0000 Subject: Improve agent scripts and daemonization support [bugfix] git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@4013 ce84e21b-d406-0410-9b95-82705330c041 --- src/sbin/bcfg2 | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/sbin') diff --git a/src/sbin/bcfg2 b/src/sbin/bcfg2 index 333048535..20bfc6a46 100755 --- a/src/sbin/bcfg2 +++ b/src/sbin/bcfg2 @@ -32,6 +32,45 @@ def cb_sigint_handler(signum, frame): '''Exit upon CTRL-C''' os._exit(1) +def daemonize(filename): + '''Do the double fork/setsession dance''' + # Check if the pid is active + try: + pidfile = open(filename, "r") + oldpid = int(pidfile.readline()) + # getpgid() will retun an IO error if all fails + os.getpgid(oldpid) + pidfile.close() + + # If we got this far without exceptions, there is another instance + # running. Exit gracefully. + print "PID File (%s) exists and listed PID (%d) is active." % \ + (filename, oldpid) + raise SystemExit, 1 + except OSError: + pidfile.close() + except IOError: + # pid file doesn't + pass + + # Fork once + if os.fork() != 0: + os._exit(0) + os.setsid() # Create new session + pid = os.fork() + if pid != 0: + pidfile = open(filename, "w") + pidfile.write("%i" % pid) + pidfile.close() + os._exit(0) + os.chdir("/") + os.umask(0) + + null = open("/dev/null", "w+") + + os.dup2(null.fileno(), sys.__stdin__.fileno()) + os.dup2(null.fileno(), sys.__stdout__.fileno()) + os.dup2(null.fileno(), sys.__stderr__.fileno()) class Client: ''' The main bcfg2 client class ''' @@ -91,6 +130,8 @@ class Client: False, False, False, True), 'agent-port': (('-g', '', 'the port on which to bind for agent mode'), False, ('communication', 'agent-port'), '6789', False), + 'agent-background': (('-i', '', "Daemonize the agent"), + False, False, False, False), 'key': (('-K', '', 'ssl cert + private key for agent mode xmlrpc server'), False, ('communication', 'key'), False, False), } @@ -379,6 +420,8 @@ if __name__ == '__main__': spid = os.getpid() if client.setup["agent"]: agent = Agent(client) + if client.setup["agent-background"]: + daemonize(client.setup["agent-background"]) while not agent.shut: try: agent.serve_forever() -- cgit v1.2.3-1-g7c22