2019-10-16 13:42:16 +02:00
|
|
|
"""
|
|
|
|
This is based upon the singleton class from
|
|
|
|
`tendo <https://github.com/pycontribs/tendo>`_
|
|
|
|
which is under the Python Software Foundation License version 2
|
|
|
|
"""
|
2017-01-10 21:15:35 +01:00
|
|
|
|
|
|
|
import atexit
|
|
|
|
import os
|
|
|
|
import sys
|
2019-10-16 13:42:16 +02:00
|
|
|
|
2017-01-11 17:00:00 +01:00
|
|
|
import state
|
2017-01-10 21:15:35 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
import fcntl # @UnresolvedImport
|
2017-09-27 17:25:14 +02:00
|
|
|
except ImportError:
|
2017-01-10 21:15:35 +01:00
|
|
|
pass
|
|
|
|
|
2017-09-27 17:25:14 +02:00
|
|
|
|
2019-10-22 16:24:20 +02:00
|
|
|
class singleinstance(object):
|
2017-01-10 21:15:35 +01:00
|
|
|
"""
|
2017-09-27 17:25:14 +02:00
|
|
|
Implements a single instance application by creating a lock file
|
|
|
|
at appdata.
|
2017-01-10 21:15:35 +01:00
|
|
|
"""
|
|
|
|
def __init__(self, flavor_id="", daemon=False):
|
|
|
|
self.initialized = False
|
|
|
|
self.counter = 0
|
|
|
|
self.daemon = daemon
|
|
|
|
self.lockPid = None
|
2017-09-27 17:25:14 +02:00
|
|
|
self.lockfile = os.path.normpath(
|
|
|
|
os.path.join(state.appdata, 'singleton%s.lock' % flavor_id))
|
2017-01-10 21:15:35 +01:00
|
|
|
|
2018-04-16 09:00:23 +02:00
|
|
|
if state.enableGUI and not self.daemon and not state.curses:
|
2017-01-10 21:15:35 +01:00
|
|
|
# Tells the already running (if any) application to get focus.
|
|
|
|
import bitmessageqt
|
|
|
|
bitmessageqt.init()
|
|
|
|
|
2018-01-01 13:10:19 +01:00
|
|
|
self.lock()
|
2017-01-10 21:15:35 +01:00
|
|
|
|
|
|
|
self.initialized = True
|
|
|
|
atexit.register(self.cleanup)
|
|
|
|
|
2018-01-01 13:08:12 +01:00
|
|
|
def lock(self):
|
2019-10-22 16:24:20 +02:00
|
|
|
"""Obtain single instance lock"""
|
2017-01-10 21:15:35 +01:00
|
|
|
if self.lockPid is None:
|
|
|
|
self.lockPid = os.getpid()
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
try:
|
2017-09-27 17:25:14 +02:00
|
|
|
# file already exists, we try to remove
|
|
|
|
# (in case previous execution was interrupted)
|
2017-01-10 21:15:35 +01:00
|
|
|
if os.path.exists(self.lockfile):
|
|
|
|
os.unlink(self.lockfile)
|
2017-09-27 17:25:14 +02:00
|
|
|
self.fd = os.open(
|
|
|
|
self.lockfile,
|
|
|
|
os.O_CREAT | os.O_EXCL | os.O_RDWR | os.O_TRUNC
|
|
|
|
)
|
2019-10-22 16:24:20 +02:00
|
|
|
except OSError as e:
|
2017-01-10 21:15:35 +01:00
|
|
|
if e.errno == 13:
|
2017-09-27 17:25:14 +02:00
|
|
|
print(
|
|
|
|
'Another instance of this application'
|
|
|
|
' is already running'
|
|
|
|
)
|
2017-01-10 21:15:35 +01:00
|
|
|
sys.exit(-1)
|
|
|
|
print(e.errno)
|
|
|
|
raise
|
2017-07-28 09:39:49 +02:00
|
|
|
else:
|
|
|
|
pidLine = "%i\n" % self.lockPid
|
2017-08-06 18:29:08 +02:00
|
|
|
os.write(self.fd, pidLine)
|
2017-01-10 21:15:35 +01:00
|
|
|
else: # non Windows
|
2017-08-15 12:25:38 +02:00
|
|
|
self.fp = open(self.lockfile, 'a+')
|
2017-01-10 21:15:35 +01:00
|
|
|
try:
|
|
|
|
if self.daemon and self.lockPid != os.getpid():
|
2017-09-27 17:25:14 +02:00
|
|
|
# wait for parent to finish
|
|
|
|
fcntl.lockf(self.fp, fcntl.LOCK_EX)
|
2017-01-10 21:15:35 +01:00
|
|
|
else:
|
|
|
|
fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
|
|
self.lockPid = os.getpid()
|
|
|
|
except IOError:
|
2021-08-26 17:30:05 +02:00
|
|
|
print('Another instance of this application is already running')
|
2017-01-10 21:15:35 +01:00
|
|
|
sys.exit(-1)
|
2017-07-28 09:39:49 +02:00
|
|
|
else:
|
|
|
|
pidLine = "%i\n" % self.lockPid
|
2018-01-01 13:08:12 +01:00
|
|
|
self.fp.truncate(0)
|
|
|
|
self.fp.write(pidLine)
|
|
|
|
self.fp.flush()
|
2017-01-10 21:15:35 +01:00
|
|
|
|
|
|
|
def cleanup(self):
|
2019-10-22 16:24:20 +02:00
|
|
|
"""Release single instance lock"""
|
2017-01-10 21:15:35 +01:00
|
|
|
if not self.initialized:
|
|
|
|
return
|
|
|
|
if self.daemon and self.lockPid == os.getpid():
|
|
|
|
# these are the two initial forks while daemonizing
|
2018-01-01 13:08:12 +01:00
|
|
|
try:
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
if hasattr(self, 'fd'):
|
|
|
|
os.close(self.fd)
|
|
|
|
else:
|
|
|
|
fcntl.lockf(self.fp, fcntl.LOCK_UN)
|
2019-10-22 16:24:20 +02:00
|
|
|
except Exception:
|
2018-01-01 13:08:12 +01:00
|
|
|
pass
|
|
|
|
|
2017-01-10 21:15:35 +01:00
|
|
|
return
|
2021-08-26 17:30:05 +02:00
|
|
|
print("Cleaning up lockfile")
|
2017-01-10 21:15:35 +01:00
|
|
|
try:
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
if hasattr(self, 'fd'):
|
|
|
|
os.close(self.fd)
|
|
|
|
os.unlink(self.lockfile)
|
|
|
|
else:
|
|
|
|
fcntl.lockf(self.fp, fcntl.LOCK_UN)
|
|
|
|
if os.path.isfile(self.lockfile):
|
|
|
|
os.unlink(self.lockfile)
|
2017-09-27 17:25:14 +02:00
|
|
|
except Exception:
|
2017-01-10 21:15:35 +01:00
|
|
|
pass
|