diff --git a/onionshare_gui/__init__.py b/onionshare_gui/__init__.py index d083f741..c2605e81 100644 --- a/onionshare_gui/__init__.py +++ b/onionshare_gui/__init__.py @@ -24,6 +24,8 @@ import platform import argparse import signal import json +import psutil +import getpass from PyQt5 import QtCore, QtWidgets from onionshare.common import Common @@ -125,23 +127,38 @@ def main(): with open(common.gui.lock_filename, "r") as f: existing_pid = int(f.read()) - print(f"Opening tab in existing OnionShare window (pid {existing_pid})") - - # Make an event for the existing OnionShare window - if filenames: - obj = {"type": "new_share_tab", "filenames": filenames} + # Is this process actually still running? + still_running = True + if not psutil.pid_exists(existing_pid): + still_running = False else: - obj = {"type": "new_tab"} + for proc in psutil.process_iter(["pid", "name", "username"]): + if proc.pid == existing_pid: + if ( + proc.username() != getpass.getuser() + or "onionshare" not in " ".join(proc.cmdline()).lower() + ): + still_running = False - # Write that event to disk - with open(common.gui.events_filename, "a") as f: - f.write(json.dumps(obj) + "\n") - return + if still_running: + print(f"Opening tab in existing OnionShare window (pid {existing_pid})") - else: - # Write the lock file - with open(common.gui.lock_filename, "w") as f: - f.write(f"{os.getpid()}\n") + # Make an event for the existing OnionShare window + if filenames: + obj = {"type": "new_share_tab", "filenames": filenames} + else: + obj = {"type": "new_tab"} + + # Write that event to disk + with open(common.gui.events_filename, "a") as f: + f.write(json.dumps(obj) + "\n") + return + else: + os.remove(common.gui.lock_filename) + + # Write the lock file + with open(common.gui.lock_filename, "w") as f: + f.write(f"{os.getpid()}\n") # Allow Ctrl-C to smoothly quit the program instead of throwing an exception def signal_handler(s, frame): diff --git a/poetry.lock b/poetry.lock index 7dd361aa..a9780ff4 100644 --- a/poetry.lock +++ b/poetry.lock @@ -312,6 +312,17 @@ version = ">=0.12" [package.extras] dev = ["pre-commit", "tox"] +[[package]] +category = "main" +description = "Cross-platform lib for process and system monitoring in Python." +name = "psutil" +optional = false +python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +version = "5.7.2" + +[package.extras] +test = ["ipaddress", "mock", "unittest2", "enum34", "pywin32", "wmi"] + [[package]] category = "dev" description = "library with cross-python path, ini-parsing, io, code, log facilities" @@ -590,7 +601,7 @@ docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"] testing = ["jaraco.itertools", "func-timeout"] [metadata] -content-hash = "04baf07d49586f9567f78935d759b4245d75ed42cba8bdfe0bdb6a3bc9533aee" +content-hash = "b5a36d265c4247b98b493fac989ecdd3c11920bd4c81d47f2af9e64edb5fd5bb" lock-version = "1.0" python-versions = "^3.7" @@ -755,6 +766,19 @@ pluggy = [ {file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"}, {file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"}, ] +psutil = [ + {file = "psutil-5.7.2-cp27-none-win32.whl", hash = "sha256:f2018461733b23f308c298653c8903d32aaad7873d25e1d228765e91ae42c3f2"}, + {file = "psutil-5.7.2-cp27-none-win_amd64.whl", hash = "sha256:66c18ca7680a31bf16ee22b1d21b6397869dda8059dbdb57d9f27efa6615f195"}, + {file = "psutil-5.7.2-cp35-cp35m-win32.whl", hash = "sha256:5e9d0f26d4194479a13d5f4b3798260c20cecf9ac9a461e718eb59ea520a360c"}, + {file = "psutil-5.7.2-cp35-cp35m-win_amd64.whl", hash = "sha256:4080869ed93cce662905b029a1770fe89c98787e543fa7347f075ade761b19d6"}, + {file = "psutil-5.7.2-cp36-cp36m-win32.whl", hash = "sha256:d8a82162f23c53b8525cf5f14a355f5d1eea86fa8edde27287dd3a98399e4fdf"}, + {file = "psutil-5.7.2-cp36-cp36m-win_amd64.whl", hash = "sha256:0ee3c36428f160d2d8fce3c583a0353e848abb7de9732c50cf3356dd49ad63f8"}, + {file = "psutil-5.7.2-cp37-cp37m-win32.whl", hash = "sha256:ff1977ba1a5f71f89166d5145c3da1cea89a0fdb044075a12c720ee9123ec818"}, + {file = "psutil-5.7.2-cp37-cp37m-win_amd64.whl", hash = "sha256:a5b120bb3c0c71dfe27551f9da2f3209a8257a178ed6c628a819037a8df487f1"}, + {file = "psutil-5.7.2-cp38-cp38-win32.whl", hash = "sha256:10512b46c95b02842c225f58fa00385c08fa00c68bac7da2d9a58ebe2c517498"}, + {file = "psutil-5.7.2-cp38-cp38-win_amd64.whl", hash = "sha256:68d36986ded5dac7c2dcd42f2682af1db80d4bce3faa126a6145c1637e1b559f"}, + {file = "psutil-5.7.2.tar.gz", hash = "sha256:90990af1c3c67195c44c9a889184f84f5b2320dce3ee3acbd054e3ba0b4a7beb"}, +] py = [ {file = "py-1.9.0-py2.py3-none-any.whl", hash = "sha256:366389d1db726cd2fcfc79732e75410e5fe4d31db13692115529d34069a043c2"}, {file = "py-1.9.0.tar.gz", hash = "sha256:9ca6883ce56b4e8da7e79ac18787889fa5206c79dcc67fb065376cd2fe03f342"}, diff --git a/pyproject.toml b/pyproject.toml index 171f7c47..6011b976 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,6 +31,7 @@ Werkzeug = "*" flask-socketio = "^4.3.0" eventlet = "^0.25.2" qrcode = "^6.1" +psutil = "^5.7.2" [tool.poetry.dev-dependencies] atomicwrites = "*"