Radek Novacek 375cedb
commit ac15c128524cd317623044d7fbaedb4eef3b557e
Radek Novacek 375cedb
Author: Radek Novacek <rnovacek@redhat.com>
Radek Novacek 375cedb
Date:   Thu Oct 4 12:06:36 2012 +0200
Radek Novacek 375cedb
Radek Novacek 375cedb
    Create PID file as soon as possible
Radek Novacek 375cedb
    
Radek Novacek 375cedb
    PID file was created too late causing service stop to be unsuccessfull
Radek Novacek 375cedb
    if call too soon after service start.
Radek Novacek 375cedb
    
Radek Novacek 375cedb
    Now PID file is create immediatelly when service is started and
Radek Novacek 375cedb
    recreated after fork.
Radek Novacek 375cedb
Radek Novacek 375cedb
diff --git a/virt-who.py b/virt-who.py
Radek Novacek 375cedb
index 0c8babf..7b15f14 100644
Radek Novacek 375cedb
--- a/virt-who.py
Radek Novacek 375cedb
+++ b/virt-who.py
Radek Novacek 375cedb
@@ -286,7 +286,7 @@ def daemonize(debugMode):
Radek Novacek 375cedb
     os.chdir("/")
Radek Novacek 375cedb
     return True
Radek Novacek 375cedb
 
Radek Novacek 375cedb
-def createPidFile(logger):
Radek Novacek 375cedb
+def createPidFile(logger=None):
Radek Novacek 375cedb
     atexit.register(cleanup)
Radek Novacek 375cedb
     signal.signal(signal.SIGINT, cleanup)
Radek Novacek 375cedb
     signal.signal(signal.SIGTERM, cleanup)
Radek Novacek 375cedb
@@ -297,7 +297,8 @@ def createPidFile(logger):
Radek Novacek 375cedb
         f.write("%d" % os.getpid())
Radek Novacek 375cedb
         f.close()
Radek Novacek 375cedb
     except Exception, e:
Radek Novacek 375cedb
-        logger.error("Unable to create pid file: %s" % str(e))
Radek Novacek 375cedb
+        if logger is not None:
Radek Novacek 375cedb
+            logger.error("Unable to create pid file: %s" % str(e))
Radek Novacek 375cedb
 
Radek Novacek 375cedb
 def cleanup(sig=None, stack=None):
Radek Novacek 375cedb
     try:
Radek Novacek 375cedb
@@ -312,6 +313,7 @@ def main():
Radek Novacek 375cedb
     if os.access(PIDFILE, os.F_OK):
Radek Novacek 375cedb
         print >>sys.stderr, "virt-who seems to be already running. If not, remove %s" % PIDFILE
Radek Novacek 375cedb
         sys.exit(1)
Radek Novacek 375cedb
+    createPidFile()
Radek Novacek 375cedb
 
Radek Novacek 375cedb
     parser = OptionParserEpilog(description="Agent for reporting virtual guest IDs to subscription-manager",
Radek Novacek 375cedb
                                 epilog="virt-who also reads enviromental variables. They have the same name as command line arguments but uppercased, with underscore instead of dash and prefixed with VIRTWHO_ (e.g. VIRTWHO_ONE_SHOT). Empty variables are considered as disabled, non-empty as enabled")
Radek Novacek 375cedb
@@ -425,6 +427,7 @@ def main():
Radek Novacek 375cedb
         # Do a double-fork and other daemon initialization
Radek Novacek 375cedb
         if not daemonize(options.debug):
Radek Novacek 375cedb
             logger.error("Unable to fork, continuing in foreground")
Radek Novacek 375cedb
+        createPidFile(logger)
Radek Novacek 375cedb
 
Radek Novacek 375cedb
     if not options.oneshot:
Radek Novacek 375cedb
         if options.background and options.virtType == "libvirt":
Radek Novacek 375cedb
@@ -444,8 +447,6 @@ def main():
Radek Novacek 375cedb
     except Exception:
Radek Novacek 375cedb
         pass
Radek Novacek 375cedb
 
Radek Novacek 375cedb
-    createPidFile(logger)
Radek Novacek 375cedb
-
Radek Novacek 375cedb
     logger.debug("Virt-who is running in %s mode" % options.virtType)
Radek Novacek 375cedb
 
Radek Novacek 375cedb
     if options.oneshot: