From 3f1bf216d24621d4468a7b216ed470aa115293e8 Mon Sep 17 00:00:00 2001 From: Bjarte Johansen Date: Thu, 23 May 2013 18:16:06 +0200 Subject: [PATCH] fix pid locking problem on OS X On OS X the lock wasn't released properly. The lock was therefore moved into the config namespace and we can use it from there to also lock it when the pyobjc application terminates. --- selfspy/__init__.py | 9 +++++---- selfspy/config.py | 1 + selfspy/sniff_cocoa.py | 10 ++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/selfspy/__init__.py b/selfspy/__init__.py index 914d9b4..82f59d7 100755 --- a/selfspy/__init__.py +++ b/selfspy/__init__.py @@ -82,8 +82,8 @@ def check_with_encrypter(password): pass lockname = os.path.join(args['data_dir'], cfg.LOCK_FILE) - lock = LockFile(lockname) - if lock.is_locked(): + cfg.LOCK = LockFile(lockname) + if cfg.LOCK.is_locked(): print '%s is locked! I am probably already running.' % lockname print 'If you can find no selfspy process running, it is a stale lock and you can safely remove it.' print 'Shutting down.' @@ -119,14 +119,15 @@ def check_with_encrypter(password): astore = ActivityStore(os.path.join(args['data_dir'], cfg.DBNAME), encrypter, store_text=(not args['no_text'])) - lock.acquire() + cfg.LOCK.acquire() try: astore.run() except SystemExit: astore.close() except KeyboardInterrupt: pass - lock.release() + # In OS X this is has to be released in sniff_cocoa + cfg.LOCK.release() if __name__ == '__main__': main() diff --git a/selfspy/config.py b/selfspy/config.py index d834ee2..23db492 100644 --- a/selfspy/config.py +++ b/selfspy/config.py @@ -20,3 +20,4 @@ DATA_DIR = '~/.selfspy' DBNAME = 'selfspy.sqlite' LOCK_FILE = 'selfspy.pid' +LOCK = None diff --git a/selfspy/sniff_cocoa.py b/selfspy/sniff_cocoa.py index c30b930..ece1973 100644 --- a/selfspy/sniff_cocoa.py +++ b/selfspy/sniff_cocoa.py @@ -30,6 +30,7 @@ NSApplicationActivationPolicyProhibited) from Quartz import CGWindowListCopyWindowInfo, kCGWindowListOptionOnScreenOnly, kCGNullWindowID from PyObjCTools import AppHelper +import config as cfg class Sniffer: def __init__(self): @@ -42,6 +43,7 @@ def createAppDelegate(self): sc = self class AppDelegate(NSObject): + def applicationDidFinishLaunching_(self, notification): mask = (NSKeyDownMask | NSKeyUpMask @@ -53,6 +55,14 @@ def applicationDidFinishLaunching_(self, notification): | NSScrollWheelMask | NSFlagsChangedMask) NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, sc.handler) + + def applicationWillTerminate_(self, application): + # need to release the lock here as when the + # application terminates it does not run the rest the + # original main, only the code that has crossed the + # pyobc bridge. + if cfg.LOCK.is_locked(): + cfg.LOCK.release() return AppDelegate def run(self):