only allow a single instance of solaar to run at a time

This commit is contained in:
Daniel Pavel 2013-01-07 21:34:47 +02:00
parent 738d43fd83
commit bb52c13f9a
1 changed files with 34 additions and 1 deletions

View File

@ -122,8 +122,41 @@ def main():
_require('gi.repository', 'python-gi')
_require('gi.repository.Gtk', 'gir1.2-gtk-3.0')
args = _parse_arguments()
_run(args)
# ensure no more than a single instance runs at a time
import os.path as _path
import os as _os
lock_fd = None
for p in _os.environ.get('XDG_RUNTIME_DIR'), '/run/lock', '/var/lock', _os.environ.get('TMPDIR', '/tmp'):
if p and _path.isdir(p) and _os.access(p, _os.W_OK):
lock_path = _path.join(p, 'solaar.single-instance.%d' % _os.getuid())
try:
lock_fd = open(lock_path, 'wb')
# print ("Single instance lock file is %s" % lock_path)
break
except:
pass
if lock_fd:
import fcntl as _fcntl
try:
_fcntl.flock(lock_fd, _fcntl.LOCK_EX | _fcntl.LOCK_NB)
except IOError as e:
if e.errno == 11:
import sys
sys.exit("solaar: error: Solaar is already running.")
else:
raise
else:
import sys
print ("solaar: warning: failed to create single instance lock file, ignoring.", file=sys.stderr)
try:
_run(args)
finally:
if lock_fd:
_fcntl.flock(lock_fd, _fcntl.LOCK_UN)
lock_fd.close()
if __name__ == '__main__':
main()