From a59dd3b13826d9395ee621b2679366891d94542e Mon Sep 17 00:00:00 2001 From: Andreas Wrede Date: Tue, 1 Sep 2015 09:50:53 -0400 Subject: [PATCH] make lock work if pid file is empty --- hbc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/hbc b/hbc index 1937e0b..191ebe7 100755 --- a/hbc +++ b/hbc @@ -12,9 +12,7 @@ import errno PORT=50003 INTERVAL=10 -False=0 -True=1 -DBG = False +DBG = True sock=None @@ -31,6 +29,7 @@ class Flock: self.opid = None def lock(self): + if DBG: print "lock it" while 1: self.fd = None try: @@ -38,9 +37,10 @@ class Flock: except OSError, e: if e.errno != errno.EEXIST: raise + if DBG: print "create error %s" % e.errno except: raise - + if DBG: print "lock() - self.fd = '%s'" % self.fd if not self.fd: if self.oproc(): if DBG: print "process is alive" @@ -62,8 +62,13 @@ class Flock: def setopid(self): try: self.opid=open(self.lock_file).readline() + if DBG: print "setopid = '%s'" % self.opid + if self.opid == '': + os.remove(self.lock_file) except: - pass + self.opid = '' + if self.opid == '': + self.opid = None def oproc(self): @@ -110,6 +115,7 @@ def socksend(msg, tohost): if sock == None: sock=getsock(tohost[0]) sock.sendto(msg, tohost) + if verbose: print "msg %s sent" % msg msgonly=False @@ -238,6 +244,7 @@ if len(msgboot) > 0: else: break +if verbose: print "msgboot done msgonly=%s" % msgonly if msgonly: sys.exit(0) @@ -265,7 +272,9 @@ if daemon: while True: lock=Flock('/tmp/hbc.pid') + if DBG: print "get lock" if lock.lock(): + if DBG: print "got lock" break if not lock.oproc(): sys.exit(1)