[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [ooni-probe/master] Move check for running process into utility function



commit 8c3a65edc75e6830f4e5c7a6c2e1afd55851de3e
Author: Arturo Filastò <arturo@xxxxxxxxxxx>
Date:   Thu Sep 15 16:22:12 2016 +0200

    Move check for running process into utility function
    
    * More fixes based on review by @bassosimone
---
 ooni/measurements.py            | 18 +++---------------
 ooni/nettest.py                 |  2 +-
 ooni/scripts/ooniprobe_agent.py | 16 ++++------------
 ooni/utils/__init__.py          | 15 +++++++++++++++
 4 files changed, 23 insertions(+), 28 deletions(-)

diff --git a/ooni/measurements.py b/ooni/measurements.py
index 294b182..422fc55 100644
--- a/ooni/measurements.py
+++ b/ooni/measurements.py
@@ -1,9 +1,7 @@
-import os
 import json
-import signal
 
 from twisted.python.filepath import FilePath
-from ooni.utils import log
+from ooni.utils import log, is_process_running
 from ooni.utils.files import directory_usage
 from ooni.settings import config
 
@@ -75,14 +73,11 @@ def get_measurement(measurement_id, compute_size=False):
     stale = False
     if measurement.child("measurements.njson.progress").exists():
         completed = False
-        # XXX this is done quite often around the code, probably should
-        # be moved into some utility function.
         pid = measurement.child("running.pid").open("r").read()
         pid = int(pid)
-        try:
-            os.kill(pid, signal.SIG_DFL)
+        if is_process_running(pid):
             running = True
-        except OSError:
+        else:
             stale = True
 
     if measurement.child("keep").exists():
@@ -136,10 +131,3 @@ def list_measurements(compute_size=False):
         except:
             log.err("Failed to get metadata for measurement {0}".format(measurement_id))
     return measurements
-
-if __name__ == "__main__":
-    import sys
-    if len(sys.argv) != 3:
-        print("Usage: {0} [input_file] [output_file]".format(sys.argv[0]))
-        sys.exit(1)
-    generate_summary(sys.argv[1], sys.argv[2])
diff --git a/ooni/nettest.py b/ooni/nettest.py
index 4ea3329..d6784f4 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -242,7 +242,7 @@ class NetTestLoader(object):
         m = ONION_INPUT_REGEXP.match(filename)
         if m:
             raise e.InvalidInputFile("Input files hosted on hidden services "
-                                     "are not longer supported")
+                                     "are no longer supported")
         else:
             input_file['filename'] = filename
         self.inputFiles.append(input_file)
diff --git a/ooni/scripts/ooniprobe_agent.py b/ooni/scripts/ooniprobe_agent.py
index 3eb1d22..c455f9b 100644
--- a/ooni/scripts/ooniprobe_agent.py
+++ b/ooni/scripts/ooniprobe_agent.py
@@ -8,7 +8,7 @@ import signal
 from twisted.scripts import twistd
 from twisted.python import usage
 
-from ooni.utils import log
+from ooni.utils import log, is_process_running
 from ooni.settings import config
 from ooni.agent.agent import AgentService
 
@@ -102,18 +102,10 @@ def get_running_pidfile():
             continue
         pid = open(pidfile, "r").read()
         pid = int(pid)
-        try:
-            os.kill(pid, signal.SIG_DFL)
+        if is_process_running(pid):
             running_pidfile = pidfile
-            break
-        except OSError as ose:
-            if ose.errno == errno.ESRCH:
-                # Found pid, but isn't running
-                continue
-            elif ose.errno == errno.EPERM:
-                # The process is owned by root. We assume it's running
-                running_pidfile = pidfile
-                break
+        else:
+            continue
     if running_pidfile is None:
         raise NotRunning
     return running_pidfile
diff --git a/ooni/utils/__init__.py b/ooni/utils/__init__.py
index 247758f..a894daf 100644
--- a/ooni/utils/__init__.py
+++ b/ooni/utils/__init__.py
@@ -1,6 +1,8 @@
 import shutil
 import string
 import random
+import signal
+import errno
 import gzip
 import os
 
@@ -160,3 +162,16 @@ def gunzip(file_path):
 def get_ooni_root():
     script = os.path.join(__file__, '..')
     return os.path.dirname(os.path.realpath(script))
+
+def is_process_running(pid):
+    try:
+        os.kill(pid, signal.SIG_DFL)
+        running = True
+    except OSError as ose:
+        if ose.errno == errno.EPERM:
+            running = True
+        elif ose.errno == errno.ESRCH:
+            running = False
+        else:
+            raise
+    return running



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits