2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 ## \file killSalomeWithPort.py
26 # Stop all %SALOME servers from given sessions by killing them
28 # The sessions are indicated by their ports on the command line as in :
30 # killSalomeWithPort.py 2811 2815
34 import os, sys, pickle, signal, commands,glob
37 from salome_utils import verbose
40 def getPiDict(port,appname='salome',full=True,hidden=True,hostname=None):
42 Get file with list of SALOME processes.
43 This file is located in the user's home directory
44 and named .<user>_<host>_<port>_SALOME_pidict
52 - appname : application name (default is 'SALOME')
53 - full : if True, full path to the file is returned, otherwise only file name is returned
54 - hidden : if True, file name is prefixed with . (dot) symbol; this internal parameter is used
55 to support compatibility with older versions of SALOME
57 # bug fix: ensure port is an integer
58 # Note: this function is also called with port='#####' !!!
64 from salome_utils import generateFileName, getLogDir
67 hostname = os.getenv("NSHOST")
68 if hostname: hostname = hostname.split(".")[0]
71 # full path to the pidict file is requested
73 # new-style dot-prefixed pidict files
74 # are in the system-dependant temporary diretory
77 # old-style non-dot-prefixed pidict files
78 # are in the user's home directory
79 dir = os.getenv("HOME")
83 return generateFileName(dir,
87 with_hostname=hostname or True,
89 with_app=appname.upper())
91 def appliCleanOmniOrbConfig(port):
93 Remove omniorb config files related to the port in SALOME application:
94 - ${OMNIORB_USER_PATH}/.omniORB_${USER}_${HOSTNAME}_${NSPORT}.cfg
95 - ${OMNIORB_USER_PATH}/.omniORB_${USER}_last.cfg
96 the last is removed only if the link points to the first file.
99 print "clean OmniOrb config for port %s"%port
101 from salome_utils import generateFileName, getUserName
102 omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
103 if omniorbUserPath is None:
104 #Run outside application context
107 omniorb_config = generateFileName(omniorbUserPath, prefix="omniORB",
113 last_running_config = generateFileName(omniorbUserPath, prefix="omniORB",
118 if os.access(last_running_config,os.F_OK):
119 if not sys.platform == 'win32':
120 pointedPath = os.readlink(last_running_config)
121 if pointedPath[0] != '/':
122 pointedPath=os.path.join(os.path.dirname(last_running_config), pointedPath)
124 if pointedPath == omniorb_config:
125 os.unlink(last_running_config)
129 os.remove(last_running_config)
133 if os.access(omniorb_config,os.F_OK):
134 os.remove(omniorb_config)
137 if os.path.lexists(last_running_config):return
139 #try to relink last.cfg to an existing config file if any
140 files = glob.glob(os.path.join(omniorbUserPath,".omniORB_"+getUserName()+"_*.cfg"))
145 if stat.st_atime > current:
146 current=stat.st_atime
149 if sys.platform == "win32":
151 shutil.copyfile(os.path.normpath(current_config), last_running_config)
154 os.symlink(os.path.normpath(current_config), last_running_config)
160 ########## kills all salome processes with the given port ##########
162 def shutdownMyPort(port, cleanup=True):
164 Shutdown SALOME session running on the specified port.
169 # bug fix: ensure port is an integer
173 from PortManager import releasePort
178 from salome_utils import generateFileName
180 # set OMNIORB_CONFIG variable to the proper file
181 omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
183 if omniorbUserPath is not None:
184 kwargs["with_username"]=True
186 omniorbUserPath = os.path.realpath(os.path.expanduser('~'))
187 omniorb_config = generateFileName(omniorbUserPath, prefix="omniORB",
193 os.environ['OMNIORB_CONFIG'] = omniorb_config
194 os.environ['NSPORT'] = str(port)
196 # give the chance to the servers to shutdown properly
199 from omniORB import CORBA
201 from LifeCycleCORBA import LifeCycleCORBA
203 orb = CORBA.ORB_init([''], CORBA.ORB_ID)
204 lcc = LifeCycleCORBA(orb) # see (1)
205 print "Terminating SALOME on port %s..."%(port)
206 lcc.shutdownServers()
207 # give some time to shutdown to complete
219 # (1) If --shutdown-servers option is set to 1, session close procedure is
220 # called twice: first explicitely by salome command, second by automatic
221 # atexit to handle Ctrl-C. During second call, LCC does not exist anymore and
222 # a RuntimeError is raised; we explicitely exit this function with code 0 to
223 # prevent parent thread from crashing.
225 def __killMyPort(port, filedict):
226 # bug fix: ensure port is an integer
231 with open(filedict, 'r') as fpid:
233 from salome_utils import generateFileName, getLogDir
234 fpidomniNames = generateFileName(getLogDir(),
236 suffix="Pid_omniNames",
239 if not sys.platform == 'win32':
240 cmd = 'pid=$(ps -eo pid,command | egrep "[0-9] omniNames -start {0}") ; echo $pid > {1}'.format(port, fpidomniNames )
241 subprocess.call(cmd, shell=True)
244 with open(fpidomniNames) as fpidomniNamesFile:
245 lines = fpidomniNamesFile.readlines()
247 os.remove(fpidomniNames)
250 pidfield = l.split()[0] # pid should be at the first position
251 if sys.platform == "win32":
252 import win32pm #@UnresolvedImport
253 if verbose(): print 'stop process '+pidfield+' : omniNames'
254 win32pm.killpid(int(pidfield),0)
256 if verbose(): print 'stop process '+pidfield+' : omniNames'
257 os.kill(int(pidfield),signal.SIGKILL)
268 process_ids=pickle.load(fpid)
269 for process_id in process_ids:
270 for pid, cmd in process_id.items():
271 if verbose(): print "stop process %s : %s"% (pid, cmd[0])
272 if cmd[0] == "omniNames":
273 if not sys.platform == 'win32':
274 proc1 = subprocess.Popen(shlex.split('ps -eo pid,command'),stdout=subprocess.PIPE)
275 proc2 = subprocess.Popen(shlex.split('egrep "[0-9] omniNames -start"'),stdin=proc1.stdout, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
276 proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
277 out,_ = proc2.communicate()
278 # out looks like: PID omniNames -start PORT <other args>
280 # extract omninames pid and port number
283 omniNamesPid, omniNamesPort = re.search('(.+?) omniNames -start (.+?) ', out).group(1, 2)
284 if omniNamesPort == port:
286 print "stop omniNames [pid=%s] on port %s"%(omniNamesPid, omniNamesPort)
287 appliCleanOmniOrbConfig(omniNamesPort)
288 from PortManager import releasePort
289 releasePort(omniNamesPort)
290 os.kill(int(omniNamesPid),signal.SIGKILL)
291 except (ImportError, AttributeError, OSError):
295 traceback.print_exc()
298 if sys.platform == "win32":
299 import win32pm #@UnresolvedImport @Reimport
300 win32pm.killpid(int(pid),0)
302 os.kill(int(pid),signal.SIGKILL)
306 if verbose(): print " ------------------ process %s : %s not found"% (pid, cmd[0])
308 pass # for pid, cmd ...
309 pass # for process_id ...
316 cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
317 # pid = subprocess.check_output(shlex.split(cmd))
318 pid = commands.getoutput(cmd)
320 while pid and len(a.split()) < 2:
321 a = commands.getoutput("kill -9 " + pid)
322 pid = commands.getoutput(cmd)
326 print "Cannot find or open SALOME PIDs file for port", port
331 def __guessPiDictFilename(port):
332 from salome_utils import getShortHostName, getHostName
334 # new-style dot-prefixed pidict file
335 getPiDict(port, hidden=True),
336 # provide compatibility with old-style pidict file (not dot-prefixed)
337 getPiDict(port, hidden=False),
338 # provide compatibility with old-style pidict file (short hostname)
339 getPiDict(port, hidden=True, hostname=getShortHostName()),
340 # provide compatibility with old-style pidict file (not dot-prefixed, short hostname
341 getPiDict(port, hidden=False, hostname=getShortHostName()),
342 # provide compatibility with old-style pidict file (long hostname)
343 getPiDict(port, hidden=True, hostname=getHostName()),
344 # provide compatibility with old-style pidict file (not dot-prefixed, long hostname)
345 getPiDict(port, hidden=False, hostname=getHostName())
349 for filedict in filedicts:
350 log_msg += "Trying %s..."%filedict
351 if os.path.exists(filedict):
352 log_msg += " ... OK\n"
355 log_msg += " ... not found\n"
363 def killMyPort(port):
365 Kill SALOME session running on the specified port.
369 # bug fix: ensure port is an integer
374 import PortManager # do not remove! Test for PortManager availability!
375 filedict = getPiDict(port)
376 if not os.path.isfile(filedict): # removed by previous call, see (1)
381 # try to shutdown session normally
382 import threading, time
383 threading.Thread(target=shutdownMyPort, args=(port,False)).start()
384 time.sleep(3) # wait a little, then kill processes (should be done if shutdown procedure hangs up)
387 import PortManager # do not remove! Test for PortManager availability!
388 filedict = getPiDict(port)
389 #filedict = __guessPiDictFilename(port)
391 all_files = glob.glob("%s*"%filedict)
393 __killMyPort(port, f)
395 filedict = __guessPiDictFilename(port)
396 __killMyPort(port, filedict)
399 appliCleanOmniOrbConfig(port)
402 def cleanApplication(port):
404 Clean application running on the specified port.
408 # bug fix: ensure port is an integer
413 filedict=getPiDict(port)
417 #traceback.print_exc()
420 appliCleanOmniOrbConfig(port)
422 def killMyPortSpy(pid, port):
425 if sys.platform == "win32":
426 from win32pm import killpid #@UnresolvedImport
427 if killpid(int(pid), 0) != 0:
438 from time import sleep
441 filedict = getPiDict(port, hidden=True)
442 if not os.path.exists(filedict):
446 orb = omniORB.CORBA.ORB_init(sys.argv, omniORB.CORBA.ORB_ID)
447 import SALOME_NamingServicePy
448 ns = SALOME_NamingServicePy.SALOME_NamingServicePy_i(orb)
449 import SALOME #@UnresolvedImport @UnusedImport
450 session = ns.Resolve("/Kernel/Session")
455 status = session.GetStatSession()
457 # -- session is in naming service but has crash
461 if not status.activeGUI:
467 if __name__ == "__main__":
468 if len(sys.argv) < 2:
470 print " %s <port>" % os.path.basename(sys.argv[0])
472 print "Kills SALOME session running on specified <port>."
475 if sys.argv[1] == "--spy":
476 if len(sys.argv) > 3:
479 killMyPortSpy(pid, port)
484 from salomeContextUtils import setOmniOrbUserPath #@UnresolvedImport
489 for port in sys.argv[1:]: