Skip to content

Commit

Permalink
fix pid locking problem on OS X
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ljos committed May 23, 2013
1 parent 7e90305 commit 3f1bf21
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
9 changes: 5 additions & 4 deletions selfspy/__init__.py
Expand Up @@ -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.'
Expand Down Expand Up @@ -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()
1 change: 1 addition & 0 deletions selfspy/config.py
Expand Up @@ -20,3 +20,4 @@
DATA_DIR = '~/.selfspy'
DBNAME = 'selfspy.sqlite'
LOCK_FILE = 'selfspy.pid'
LOCK = None
10 changes: 10 additions & 0 deletions selfspy/sniff_cocoa.py
Expand Up @@ -30,6 +30,7 @@
NSApplicationActivationPolicyProhibited)
from Quartz import CGWindowListCopyWindowInfo, kCGWindowListOptionOnScreenOnly, kCGNullWindowID
from PyObjCTools import AppHelper
import config as cfg

class Sniffer:
def __init__(self):
Expand All @@ -42,6 +43,7 @@ def createAppDelegate(self):
sc = self

class AppDelegate(NSObject):

def applicationDidFinishLaunching_(self, notification):
mask = (NSKeyDownMask
| NSKeyUpMask
Expand All @@ -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):
Expand Down

0 comments on commit 3f1bf21

Please sign in to comment.