summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRobert Gogolok <gogo@cs.uni-sb.de>2007-12-28 10:26:58 +0000
committerRobert Gogolok <gogo@cs.uni-sb.de>2007-12-28 10:26:58 +0000
commit70a9fa998fb9ded34d74614d0d31219ae279b84c (patch)
treeed89043f5aca2924c0ee3c19bfa0607a1a7c4fb0 /src
parenteec4aa89b3a1713125d0117f7b15fb2a1ce78265 (diff)
downloadbcfg2-70a9fa998fb9ded34d74614d0d31219ae279b84c.tar.gz
bcfg2-70a9fa998fb9ded34d74614d0d31219ae279b84c.tar.bz2
bcfg2-70a9fa998fb9ded34d74614d0d31219ae279b84c.zip
move daemonize support to own module
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@4113 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src')
-rw-r--r--src/lib/Daemon.py44
-rwxr-xr-xsrc/sbin/bcfg243
-rwxr-xr-xsrc/sbin/bcfg2-server46
3 files changed, 49 insertions, 84 deletions
diff --git a/src/lib/Daemon.py b/src/lib/Daemon.py
new file mode 100644
index 000000000..1886f5569
--- /dev/null
+++ b/src/lib/Daemon.py
@@ -0,0 +1,44 @@
+'''Bcfg2 daemon support'''
+__revision__ = '$Revision'
+
+import os, sys
+
+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())
diff --git a/src/sbin/bcfg2 b/src/sbin/bcfg2
index 20bfc6a46..a25bab65f 100755
--- a/src/sbin/bcfg2
+++ b/src/sbin/bcfg2
@@ -16,6 +16,7 @@ import Bcfg2.Options
import Bcfg2.Client.XML
import Bcfg2.Client.Frame
import Bcfg2.Client.Tools
+import Bcfg2.Daemon
from Bcfg2.Component import *
from Bcfg2.tlslite.Checker import Checker
from Bcfg2.tlslite.errors import *
@@ -32,46 +33,6 @@ 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 '''
@@ -421,7 +382,7 @@ if __name__ == '__main__':
if client.setup["agent"]:
agent = Agent(client)
if client.setup["agent-background"]:
- daemonize(client.setup["agent-background"])
+ Bcfg2.Daemon.daemonize(client.setup["agent-background"])
while not agent.shut:
try:
agent.serve_forever()
diff --git a/src/sbin/bcfg2-server b/src/sbin/bcfg2-server
index 1ead18718..e6a00efbc 100755
--- a/src/sbin/bcfg2-server
+++ b/src/sbin/bcfg2-server
@@ -9,51 +9,11 @@ from Bcfg2.Server.Core import Core, CoreInitError
from xmlrpclib import Fault
from lxml.etree import XML, Element, tostring
-import logging, os, select, signal, socket, sys
-import Bcfg2.Logging, Bcfg2.Options, Bcfg2.Component
+import logging, select, signal, socket, sys
+import Bcfg2.Logging, Bcfg2.Options, Bcfg2.Component, Bcfg2.Daemon
logger = logging.getLogger('bcfg2-server')
-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.
- logger.critical("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())
-
def critical_error(operation):
'''Log and err, traceback and return an xmlrpc fault to client'''
logger.error(operation, exc_info=1)
@@ -234,7 +194,7 @@ if __name__ == '__main__':
else:
Bcfg2.Logging.setup_logging('bcfg2-server', level=level)
if SSETUP['daemon']:
- daemonize(SSETUP['daemon'])
+ Bcfg2.Daemon.daemonize(SSETUP['daemon'])
try:
BSERV = Bcfg2Serv(SSETUP)
except SetupError: