]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
Fix for the bug 0023165: [CEA 1590] Salome 7.6.0 on Windows does not liberate the...
authorana <ana@opencascade.com>
Wed, 18 Nov 2015 15:11:37 +0000 (18:11 +0300)
committerana <ana@opencascade.com>
Wed, 18 Nov 2015 15:11:37 +0000 (18:11 +0300)
bin/killSalomeWithPort.py
bin/salome_utils.py
src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx

index 95497f0dbefe80091c4591fdd648211e5516122e..67d205c4ace71333e709a045167a7e0767b321f1 100755 (executable)
@@ -229,102 +229,22 @@ def __killMyPort(port, filedict):
 
     try:
         with open(filedict, 'r') as fpid:
-            #
-            from salome_utils import generateFileName, getLogDir
-            fpidomniNames = generateFileName(getLogDir(),
-                                             prefix="",
-                                             suffix="Pid_omniNames",
-                                             extension="log",
-                                             with_port=port)
-            if not sys.platform == 'win32':
-                cmd = 'pid=$(ps -eo pid,command | egrep "[0-9] omniNames -start {0}") ; echo $pid > {1}'.format(port, fpidomniNames )
-                subprocess.call(cmd, shell=True)
-                pass
-            try:
-                with open(fpidomniNames) as fpidomniNamesFile:
-                    lines = fpidomniNamesFile.readlines()
-
-                os.remove(fpidomniNames)
-                for l in lines:
+            process_ids=pickle.load(fpid)
+            for process_id in process_ids:
+                for pid, cmd in process_id.items():
+                    if verbose(): print "stop process %s : %s"% (pid, cmd[0])
                     try:
-                        pidfield = l.split()[0] # pid should be at the first position
-                        if verbose(): print 'stop process '+pidfield+' : omniNames'
-                        if sys.platform == "win32":
-                            from salome_utils import win32killpid
-                            win32killpid(int(pidfield))
-                        else:
-                            os.kill(int(pidfield),signal.SIGKILL)
-                            pass
-                        pass
+                        from salome_utils import killpid
+                        killpid(int(pid))
                     except:
+                        if verbose(): print "  ------------------ process %s : %s not found"% (pid, cmd[0])
                         pass
-                    pass
-                pass
-            except:
-                pass
-            #
-            try:
-                process_ids=pickle.load(fpid)
-                for process_id in process_ids:
-                    for pid, cmd in process_id.items():
-                        if verbose(): print "stop process %s : %s"% (pid, cmd[0])
-                        if cmd[0] == "omniNames":
-                            if not sys.platform == 'win32':
-                                proc1 = subprocess.Popen(shlex.split('ps -eo pid,command'),stdout=subprocess.PIPE)
-                                proc2 = subprocess.Popen(shlex.split('egrep "[0-9] omniNames -start"'),stdin=proc1.stdout, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
-                                proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
-                                out,_ = proc2.communicate()
-                                # out looks like: PID omniNames -start PORT <other args>
-
-                                # extract omninames pid and port number
-                                try:
-                                    import re
-                                    omniNamesPid, omniNamesPort = re.search('(.+?) omniNames -start (.+?) ', out).group(1, 2)
-                                    if omniNamesPort == port:
-                                        if verbose():
-                                            print "stop omniNames [pid=%s] on port %s"%(omniNamesPid, omniNamesPort)
-                                        appliCleanOmniOrbConfig(omniNamesPort)
-                                        from PortManager import releasePort
-                                        releasePort(omniNamesPort)
-                                        os.kill(int(omniNamesPid),signal.SIGKILL)
-                                except (ImportError, AttributeError, OSError):
-                                    pass
-                                except:
-                                    import traceback
-                                    traceback.print_exc()
-
-                        try:
-                            if sys.platform == "win32":
-                                from salome_utils import win32killpid
-                                win32killpid(int(pid))
-                            else:
-                                os.kill(int(pid),signal.SIGKILL)
-                                pass
-                            pass
-                        except:
-                            if verbose(): print "  ------------------ process %s : %s not found"% (pid, cmd[0])
-                            pass
-                        pass # for pid, cmd ...
-                    pass # for process_id ...
-                pass # try...
-            except:
-                pass
-        # end with
-        #
-        os.remove(filedict)
-        cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
-#        pid = subprocess.check_output(shlex.split(cmd))
-        pid = commands.getoutput(cmd)
-        a = ""
-        while pid and len(a.split()) < 2:
-            a = commands.getoutput("kill -9 " + pid)
-            pid = commands.getoutput(cmd)
-            pass
-        pass
+                    pass # for pid ...
+                pass # for process_id ...
+            # end with
     except:
         print "Cannot find or open SALOME PIDs file for port", port
         pass
-    #
 #
 
 def __guessPiDictFilename(port):
index cf87ef39d3c055ee6bcacd2498865018456d44b3..b3efa87570f8312dc234d3df1bf9f9204caea7ae 100644 (file)
@@ -483,17 +483,62 @@ def setVerbose(level):
     global _verbose
     _verbose = level
     return
+# --
 
+def killpid(pid):
+    """
+    Kill process by pid.
+    """
+    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)
+        pass
 # --
 
-def win32killpid(pid):
+def getOmniNamesPid(port):
     """
-    Kill process by pid on windows platform.
+    Return OmniNames pid by port number.
     """
-    if verbose(): print "######## win32killpid pid = ", pid
-    import ctypes
-    handle = ctypes.windll.kernel32.OpenProcess(1, False, pid)
-    ret = ctypes.windll.kernel32.TerminateProcess(handle, -1)
-    ctypes.windll.kernel32.CloseHandle(handle)
-    return ret
+    import sys,subprocess,re
+    if sys.platform == "win32":
+        # Get process list by WMI Command Line Utility(WMIC)
+        # Output is formatted with each value listed on a separate line and with the name of the property:
+        #   ...
+        #   Caption=<caption0>
+        #   CommandLine=<commandline0>
+        #   ProcessId=<processid0>
+        #
+        #
+        #
+        #   Caption=<caption1>
+        #   CommandLine=<commandline1>
+        #   ProcessId=<processid1>
+        #   ...
+        cmd = 'WMIC PROCESS get Caption,Commandline,Processid /VALUE'
+        proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
+        # Get stdout
+        allProc = proc.communicate()[0]
+        # find Pid of omniNames
+        pid = re.findall(r'Caption=.*omniNames.*\n?CommandLine=.*omniNames.*\D%s\D.*\n?ProcessId=(\d*)'%(port),allProc)[0]
+    else:        
+        cmd = r"ps -eo pid,command | grep -v grep | grep -E \"omniNames.*%s\""%(port)
+        proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
+        pid = proc.communicate()[0]
+        pass
+
+    return pid
 # --
+
+def killOmniNames(port):
+    """
+    Kill OmniNames process by port number.
+    """
+    pid = getOmniNamesPid(port)
+    killpid(pid)
+# --
\ No newline at end of file
index 58d64b4b325cfb5b794fddd6355f5252df9f49da..e6db97ac997c719f5ae49a4a61535f2fdcf92ba8 100644 (file)
@@ -597,23 +597,6 @@ void SALOME_LifeCycleCORBA::shutdownServers()
 void SALOME_LifeCycleCORBA::killOmniNames()
 {
   std::string portNumber (::getenv ("NSPORT") );
-  if ( !portNumber.empty() )
-  {
-#ifdef WIN32
-#else
-    std::string cmd ;
-    cmd = std::string( "ps -eo pid,command | grep -v grep | grep -E \"omniNames.*")
-      + portNumber
-      + std::string("\" | awk '{cmd=sprintf(\"kill -9 %s\",$1); system(cmd)}'" );
-    MESSAGE(cmd);
-    try {
-      system ( cmd.c_str() );
-    }
-    catch ( ... ) {
-    }
-#endif
-  }
-
   std::string python_exe;
 
   python_exe = std::string("python");
@@ -625,7 +608,13 @@ void SALOME_LifeCycleCORBA::killOmniNames()
 
   if ( !portNumber.empty() )
   {
-    std::string cmd = ("from killSalomeWithPort import cleanApplication; ");
+    std::string cmd = ("from salome_utils import killOmniNames; ");
+    cmd += std::string("killOmniNames(") + portNumber + "); ";
+    cmd  = python_exe + std::string(" -c \"") + cmd +"\"";
+    MESSAGE(cmd);
+    system( cmd.c_str() );
+
+    cmd = ("from killSalomeWithPort import cleanApplication; ");
     cmd += std::string("cleanApplication(") + portNumber + "); ";
     //cmd  = python_exe + std::string(" -c \"") + cmd +"\" > /dev/null 2> /dev/null";
     cmd  = python_exe + std::string(" -c \"") + cmd +"\"";