This repository has been archived on 2024-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-05/src/singleinstance.py

112 lines
3.5 KiB
Python
Raw Normal View History

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