[virt-who] Do double-fork when starting as daemon
by Radek Novacek
commit 831c424a3ba1adf467470dca83a90260e2d26ebb
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Fri Mar 30 10:42:58 2012 +0200
Do double-fork when starting as daemon
Also clean up file descriptors and redirect std* to /dev/null.
virt-who.py | 109 +++++++++++++++++++++++++++++++++++++++++++++++-----------
1 files changed, 88 insertions(+), 21 deletions(-)
---
diff --git a/virt-who.py b/virt-who.py
index ce5ce2e..e5e083b 100644
--- a/virt-who.py
+++ b/virt-who.py
@@ -21,6 +21,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import sys
import os
import time
+import atexit
from virt import Virt, VirtError
from vdsm import VDSM
@@ -40,6 +41,8 @@ RetryInterval = 60 # One minute
# Default interval for sending list of UUIDs
DefaultInterval = 3600 # Once per hour
+PIDFILE = "/var/run/virt-who.pid"
+
class VirtWho(object):
def __init__(self, logger, options):
"""
@@ -190,7 +193,82 @@ class VirtWho(object):
return False
return self.virt.ping()
+def daemonize():
+ """ Perform double-fork and close stdout and stderr file descriptors """
+ # Close all file descriptors (except std*)
+ try:
+ fds = os.listdir("/proc/self/fd")
+ except:
+ fds = []
+ for fd in fds:
+ if int(fd) > 2:
+ try:
+ os.close(int(fd))
+ except Exception:
+ # fd wasn't open, ignore
+ pass
+
+ # First fork
+ try:
+ pid = os.fork()
+ except OSError:
+ return False
+
+ if pid > 0:
+ # Parent process
+ os._exit(0)
+
+ # First child process
+
+ # Create session and set process group ID
+ os.setsid()
+
+ # Second fork
+ try:
+ pid = os.fork()
+ except OSError:
+ return False
+
+ if pid > 0:
+ # Parent process
+ os._exit(0)
+
+ # Second child process
+
+ # Redirect std* to /dev/null
+ devnull = os.open("/dev/null", os.O_RDWR)
+ os.dup2(devnull, 0)
+ os.dup2(devnull, 1)
+ os.dup2(devnull, 2)
+
+ # Reset file creation mask
+ os.umask(0)
+ # Forget current working directory
+ os.chdir("/")
+ return True
+
+def createPidFile():
+ atexit.register(cleanup)
+
+ # Write pid to pidfile
+ try:
+ f = open(PIDFILE, "w")
+ f.write("%d" % os.getpid())
+ f.close()
+ except Exception, e:
+ logger.error("Unable to create pid file: %s" % str(e))
+
+def cleanup():
+ try:
+ os.remove(PIDFILE)
+ except Exception:
+ pass
+
if __name__ == '__main__':
+ if os.access(PIDFILE, os.F_OK):
+ print >>sys.stderr, "virt-who seems to be already running. If not, remove %s" % PIDFILE
+ sys.exit(1)
+
log.init_logger()
logger = logging.getLogger("rhsm-app." + __name__)
@@ -278,32 +356,21 @@ if __name__ == '__main__':
# (e.g. libvirtd restart)
options.interval = DefaultInterval
- if options.background and options.virtType != "libvirt":
- logger.warning("Listening for events is not available in VDSM or ESX mode")
-
if options.background:
- try:
- pid = os.fork()
- except OSError:
- logger.error("Unable to fork, continuing in foreground")
- pid = 0
-
- if pid > 0:
- # Parent process
- sys.exit(0)
-
- # Write pid to pidfile
- try:
- f = open("/var/run/virt-who.pid", "w")
- f.write("%d" % os.getpid())
- f.close()
- except Exception, e:
- logger.error("Unable to create pid file: %s" % str(e))
-
if options.virtType == "libvirt":
virEventLoopPureStart()
+ else:
+ logger.warning("Listening for events is not available in VDSM or ESX mode")
virtWho = VirtWho(logger, options)
+ virtWho.checkConnections()
+
+ if options.background:
+ # Do a double-fork and other daemon initialization
+ if not daemonize():
+ logger.error("Unable to fork, continuing in foreground")
+
+ createPidFile()
logger.debug("Virt-who is running in %s mode" % options.virtType)
12 years, 1 month
[virt-who] Bump version to 0.6
by Radek Novacek
commit 0de3b93edeb30f05cd20e7facc84faaa804741ae
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Mon Feb 13 13:45:52 2012 +0100
Bump version to 0.6
Makefile | 2 +-
virt-who.spec | 5 ++++-
2 files changed, 5 insertions(+), 2 deletions(-)
---
diff --git a/Makefile b/Makefile
index 9a29099..e7d890c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
name = virt-who
-version = 0.5
+version = 0.6
.PHONY: pack check install srpm rpm rpmlint upload
diff --git a/virt-who.spec b/virt-who.spec
index f57b8b6..aa206fa 100644
--- a/virt-who.spec
+++ b/virt-who.spec
@@ -1,5 +1,5 @@
Name: virt-who
-Version: 0.5
+Version: 0.6
Release: 1%{?dist}
Summary: Agent for reporting virtual guest IDs to subscription-manager
@@ -69,6 +69,9 @@ fi
%changelog
+* Mon Feb 13 2012 Radek Novacek <rnovacek(a)redhat.com> 0.6-1
+- Version 0.6
+
* Fri Dec 09 2011 Radek Novacek <rnovacek(a)redhat.com> 0.5-1
- VSphere support
- Req: python-suds
12 years, 1 month