From 1404bab59181c9ac5962791ea7292d9819009905 Mon Sep 17 00:00:00 2001 From: Andreas Wrede Date: Wed, 2 Sep 2015 14:51:20 -0400 Subject: [PATCH] replace homebrow lock with package lockfile --- hbc | 165 +++++++++++++++++++++--------------------------------------- 1 file changed, 57 insertions(+), 108 deletions(-) diff --git a/hbc b/hbc index 191ebe7..dacffab 100755 --- a/hbc +++ b/hbc @@ -1,5 +1,9 @@ #!/usr/bin/env python # $Id: hbc,v 1.9 2012/03/29 02:08:36 andreas Exp $ + +# requre python-filelock on Linux +# require py27-lockfile on *bsd +# or run sudo easy_install-2.7 lockfile import sys import time import socket @@ -9,6 +13,8 @@ import getopt import string import select import errno +import traceback +from lockfile import FileLock PORT=50003 INTERVAL=10 @@ -21,67 +27,6 @@ class NullDevice: pass - -class Flock: - def __init__(self, lock_file): - self.lock_file = lock_file - self.fd = None - self.opid = None - - def lock(self): - if DBG: print "lock it" - while 1: - self.fd = None - try: - self.fd = os.open(self.lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR) - 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" - os.remove(self.lock_file) - continue - else: - if DBG: print "no pid process??" - - if self.fd: - f=os.fdopen(self.fd, 'w').write("%s" % os.getpid()) - return self.fd - - - def unlock(self): - os.remove(self.lock_file) - self.fd=None - - - 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: - self.opid = '' - if self.opid == '': - self.opid = None - - - def oproc(self): - self.setopid() - if not self.opid: - return False - try: - os.kill(int(self.opid), 0) - return True - except: - pass - return False - def handler(signum, frame): global up if up == 0: @@ -117,6 +62,38 @@ def socksend(msg, tohost): sock.sendto(msg, tohost) if verbose: print "msg %s sent" % msg +def process(): + ackcount=0 + lastT=time.time() + while up: + sleep=(lastT+interval) - time.time() + if verbose: print "sleep %s" % sleep + if sleep > 0: + try: + r=select.select([sock.fileno()],[],[],sleep) + # time.sleep(interval) + except: + break + if verbose: print r + if sock.fileno() in r[0]: + data, addr = sock.recvfrom(1024) + if data == "ACK": + ackcount+=1 + else: + try: + os.system(data) + except: + pass + continue + lastT=time.time() + for hb_host in hb_hosts: + try: + msg="interval=%s;name=%s;time=%s;acks=%s" % (interval, iam, time.time(), ackcount) + if verbose: print "sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port) + socksend(msg, (hb_host, hb_port)) + except: + pass + msgonly=False helpflag=False @@ -259,61 +236,33 @@ if daemon: sys.exit(0) - os.close(0) - os.close(1) - os.close(2) - sys.stdin.close() - sys.stdout = NullDevice() - sys.stderr = NullDevice() + if not DBG: + os.close(0) + os.close(1) + os.close(2) + sys.stdin.close() + sys.stdout = NullDevice() + sys.stderr = NullDevice() os.chdir("/") os.setsid() os.umask(0) -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) - os.kill(lock.opid,15) - time.sleep(1) +lock = FileLock('/tmp/hbc.pid') +lock.acquire() up=1 signal.signal(signal.SIGTERM, handler) signal.signal(signal.SIGHUP, handler) -ackcount=0 -lastT=time.time() -while up: - sleep=(lastT+interval) - time.time() - if verbose: print "sleep %s" % sleep - if sleep > 0: - try: - r=select.select([sock.fileno()],[],[],sleep) -# time.sleep(interval) - except: - break - if verbose: print r - if sock.fileno() in r[0]: - data, addr = sock.recvfrom(1024) - if data == "ACK": - ackcount+=1 - else: - try: - os.system(data) - except: - pass - continue - lastT=time.time() - for hb_host in hb_hosts: - try: - msg="interval=%s;name=%s;time=%s;acks=%s" % (interval, iam, time.time(), ackcount) - if verbose: print "sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port) - socksend(msg, (hb_host, hb_port)) - except: - pass + + + +try: + process() +except: + data='hbc died:\n'+traceback.format_exc() + open("/tmp/hbc.log","a").write(data) + sys.exit(1) up=0 msg="shutdown=1;name=%s;acks=%s" % (iam, ackcount) @@ -322,4 +271,4 @@ for hb_host in hb_hosts: socksend(msg, (hb_host, hb_port)) time.sleep(1) sock.close() -lock.unlock() +lock.release()