From: ana Date: Wed, 18 Nov 2015 15:11:37 +0000 (+0300) Subject: Fix for the bug 0023165: [CEA 1590] Salome 7.6.0 on Windows does not liberate the... X-Git-Tag: V8_0_0a1~7 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=6f502b5e327a160efe7f1acd8573461bc9576cc0;p=modules%2Fkernel.git Fix for the bug 0023165: [CEA 1590] Salome 7.6.0 on Windows does not liberate the ports --- diff --git a/bin/killSalomeWithPort.py b/bin/killSalomeWithPort.py index 95497f0db..67d205c4a 100755 --- a/bin/killSalomeWithPort.py +++ b/bin/killSalomeWithPort.py @@ -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 - - # 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): diff --git a/bin/salome_utils.py b/bin/salome_utils.py index cf87ef39d..b3efa8757 100644 --- a/bin/salome_utils.py +++ b/bin/salome_utils.py @@ -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= + # CommandLine= + # ProcessId= + # + # + # + # Caption= + # CommandLine= + # ProcessId= + # ... + 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 diff --git a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx index 58d64b4b3..e6db97ac9 100644 --- a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx +++ b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx @@ -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 +"\"";