Salome HOME
Merge branch 'master' into V8_5_BR
[modules/yacs.git] / bin / salome_utils.py
index 2dbe6b3645fd48a286a7e16670bf6945fe910d81..4f4936f97cf96b99315d7783f9959ebe43a35fc5 100644 (file)
@@ -1,5 +1,5 @@
 #  -*- coding: iso-8859-1 -*-
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -148,7 +148,6 @@ def getHostName():
     3. if fails, try HOST environment variable
     4. if fails, return 'unknown' as default host name
     """
     3. if fails, try HOST environment variable
     4. if fails, return 'unknown' as default host name
     """
-    import os
     try:
         import socket
         host = socket.gethostname()
     try:
         import socket
         host = socket.gethostname()
@@ -158,6 +157,11 @@ def getHostName():
     if not host: host = os.getenv("HOSTNAME")
     if not host: host = os.getenv("HOST")
     if not host: host = "unknown"           # 'unknown' is default host name
     if not host: host = os.getenv("HOSTNAME")
     if not host: host = os.getenv("HOST")
     if not host: host = "unknown"           # 'unknown' is default host name
+    try:
+        socket.gethostbyname(host)
+    except:
+        host = "localhost"
+    pass
     return host
 
 # ---
     return host
 
 # ---
@@ -485,21 +489,61 @@ def setVerbose(level):
     return
 # --
 
     return
 # --
 
-def killpid(pid):
+import signal
+def killpid(pid, sig = 9):
     """
     """
-    Kill process by pid.
+    Send signal sig to the process by pid.
+
+    Parameters:
+    - pid : PID of process
+    - sig : signal for sending
+            Possible values of signals: 
+            9 means kill the process
+            0 only check existing of the process
+            NOTE: Other values are not processed on Windows
+    Returns:
+     1 Success
+     0 Fail, no such process
+    -1 Fail, another reason
+
     """
     if not pid: return
     """
     if not pid: return
-    import os,sys,signal
-    if verbose(): print "######## killpid pid = ", pid
-    if sys.platform == "win32":
-        import ctypes
-        handle = ctypes.windll.kernel32.OpenProcess(1, False, int(pid))
-        ctypes.windll.kernel32.TerminateProcess(handle, -1)
-        ctypes.windll.kernel32.CloseHandle(handle)
-    else:
-        os.kill(int(pid),signal.SIGKILL)
+    import os, sys
+    if sig != 0:
+        if verbose(): print "######## killpid pid = ", pid
+    try:
+        if sys.platform == "win32":
+            import ctypes
+            if sig == 0:
+                # PROCESS_QUERY_INFORMATION (0x0400)   Required to retrieve certain information about a process
+                handle = ctypes.windll.kernel32.OpenProcess(0x0400, False, int(pid))
+                if handle: 
+                    ret = 1
+                    ctypes.windll.kernel32.CloseHandle(handle)
+                else:
+                    ret = 0
+            if sig == 9:
+                # PROCESS_TERMINATE (0x0001)   Required to terminate a process using TerminateProcess.
+                handle = ctypes.windll.kernel32.OpenProcess(0x0001, False, int(pid))
+                ret = ctypes.windll.kernel32.TerminateProcess(handle, -1)
+                ctypes.windll.kernel32.CloseHandle(handle)
+                pass
+            pass
+        else:
+            # Default: signal.SIGKILL = 9
+            os.kill(int(pid),sig)
+            ret = 1
+            pass
         pass
         pass
+    except OSError, e:
+        # errno.ESRCH == 3 is 'No such process'
+        if e.errno == 3:
+            ret = 0
+        else:
+            ret = -1
+            pass
+        pass
+    return ret
 # --
 
 def getOmniNamesPid(port):
 # --
 
 def getOmniNamesPid(port):