Lock file fixes
- daemon mode lock file didn't work correctly in the last commit
This commit is contained in:
parent
4865659d72
commit
d3e8771aed
|
@ -255,10 +255,13 @@ class Main:
|
||||||
def daemonize(self):
|
def daemonize(self):
|
||||||
if os.fork():
|
if os.fork():
|
||||||
exit(0)
|
exit(0)
|
||||||
|
shared.thisapp.lock() # relock
|
||||||
os.umask(0)
|
os.umask(0)
|
||||||
os.setsid()
|
os.setsid()
|
||||||
if os.fork():
|
if os.fork():
|
||||||
exit(0)
|
exit(0)
|
||||||
|
shared.thisapp.lock() # relock
|
||||||
|
shared.thisapp.lockPid = None # indicate we're the final child
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
si = file('/dev/null', 'r')
|
si = file('/dev/null', 'r')
|
||||||
|
|
|
@ -23,6 +23,7 @@ class singleinstance:
|
||||||
self.initialized = False
|
self.initialized = False
|
||||||
self.counter = 0
|
self.counter = 0
|
||||||
self.daemon = daemon
|
self.daemon = daemon
|
||||||
|
self.lockPid = None
|
||||||
self.lockfile = os.path.normpath(os.path.join(shared.appdata, 'singleton%s.lock' % flavor_id))
|
self.lockfile = os.path.normpath(os.path.join(shared.appdata, 'singleton%s.lock' % flavor_id))
|
||||||
|
|
||||||
if not self.daemon and not shared.curses:
|
if not self.daemon and not shared.curses:
|
||||||
|
@ -30,6 +31,14 @@ class singleinstance:
|
||||||
import bitmessageqt
|
import bitmessageqt
|
||||||
bitmessageqt.init()
|
bitmessageqt.init()
|
||||||
|
|
||||||
|
self.lock()
|
||||||
|
|
||||||
|
self.initialized = True
|
||||||
|
atexit.register(self.cleanup)
|
||||||
|
|
||||||
|
def lock(self):
|
||||||
|
if self.lockPid is None:
|
||||||
|
self.lockPid = os.getpid()
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
try:
|
try:
|
||||||
# file already exists, we try to remove (in case previous execution was interrupted)
|
# file already exists, we try to remove (in case previous execution was interrupted)
|
||||||
|
@ -46,18 +55,19 @@ class singleinstance:
|
||||||
else: # non Windows
|
else: # non Windows
|
||||||
self.fp = open(self.lockfile, 'w')
|
self.fp = open(self.lockfile, 'w')
|
||||||
try:
|
try:
|
||||||
|
if self.daemon and self.lockPid != os.getpid():
|
||||||
|
fcntl.lockf(self.fp, fcntl.LOCK_EX) # wait for parent to finish
|
||||||
|
else:
|
||||||
fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||||
|
self.lockPid = os.getpid()
|
||||||
except IOError:
|
except IOError:
|
||||||
print 'Another instance of this application is already running'
|
print 'Another instance of this application is already running'
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
self.initialized = True
|
|
||||||
atexit.register(self.cleanup)
|
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
if not self.initialized:
|
if not self.initialized:
|
||||||
return
|
return
|
||||||
self.counter += 1
|
if self.daemon and self.lockPid == os.getpid():
|
||||||
if self.daemon and self.counter < 3:
|
|
||||||
# these are the two initial forks while daemonizing
|
# these are the two initial forks while daemonizing
|
||||||
return
|
return
|
||||||
print "Cleaning up lockfile"
|
print "Cleaning up lockfile"
|
||||||
|
|
Reference in New Issue
Block a user