1 #! /usr/bin/env python3
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2021 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 # @brief Forcibly stop %SALOME processes from given session(s).
28 # The sessions are indicated by their ports on the command line as in below example:
30 # killSalomeWithPort.py 2811 2815
34 Forcibly stop given SALOME session(s).
36 To stop one or more SALOME sessions, specify network ports they are bound to,
41 $ killSalomeWithPort.py 2811 2815
45 from killSalomeWithPort import killMyPort
46 killMyPort(2811, 2815)
50 # pragma pylint: disable=invalid-name
59 from contextlib import suppress
61 from threading import Thread
62 from time import sleep
66 from salome_utils import (generateFileName, getHostName, getLogDir, getShortHostName,
67 getUserName, killOmniNames, killPid, verbose)
69 def getPiDict(port, appname='salome', full=True, hidden=True, hostname=None):
71 Get path to the file that stores the list of SALOME processes.
73 This file is located in the user's home directory
74 and named .<user>_<host>_<port>_SALOME_pidict
79 - <port> is port number
81 :param port : port number
82 :param appname : application name (default: 'salome')
83 :param full : if True, full path to the file is returned,
84 otherwise only file name is returned
85 :param hidden : if True, file name is prefixed with . (dot) symbol;
86 this internal parameter is only used to support
87 compatibility with older versions of SALOME
88 :param hostname : host name (if not given, it is auto-detected)
89 :return pidict file's name or path
91 # ensure port is an integer
92 # warning: this function is also called with port='#####'!!!
93 with suppress(ValueError):
96 # hostname (if not specified via parameter)
97 with suppress(AttributeError):
98 hostname = hostname or os.getenv('NSHOST').split('.')[0]
100 # directory to store pidict file (if `full` is True)
101 # old style: pidict files aren't dot-prefixed, stored in the user's home directory
102 # new style: pidict files are dot-prefixed, stored in the system-dependant temporary directory
103 pidict_dir = getLogDir() if hidden else osp.expanduser('~')
105 return generateFileName(pidict_dir if full else '',
109 with_hostname=(hostname or True),
111 with_app=appname.upper())
113 def appliCleanOmniOrbConfig(port):
115 Remove omniorb config files related to given `port` in SALOME application:
116 - ${OMNIORB_USER_PATH}/.omniORB_${USER}_${HOSTNAME}_${NSPORT}.cfg
117 - ${OMNIORB_USER_PATH}/.omniORB_${USER}_last.cfg
118 the last is removed only if the link points to the first file.
119 :param port : port number
121 omniorb_user_path = os.getenv('OMNIORB_USER_PATH')
122 if not omniorb_user_path:
123 # outside application context
127 print("Cleaning OmniOrb config for port {}".format(port))
129 omniorb_config = generateFileName(omniorb_user_path,
136 last_running_config = generateFileName(omniorb_user_path,
143 if os.access(last_running_config, os.F_OK):
144 if sys.platform == 'win32' or osp.samefile(last_running_config, omniorb_config):
145 os.remove(last_running_config)
147 if os.access(omniorb_config, os.F_OK):
148 os.remove(omniorb_config)
150 if osp.lexists(last_running_config):
153 # try to relink last.cfg to an existing config file if any
154 cfg_files = [(cfg_file, os.stat(cfg_file)) for cfg_file in \
155 glob(osp.join(omniorb_user_path,
156 '.omniORB_{}_*.cfg'.format(getUserName())))]
157 next_config = next((i[0] for i in sorted(cfg_files, key=lambda i: i[1])), None)
159 if sys.platform == 'win32':
160 shutil.copyfile(osp.normpath(next_config), last_running_config)
162 os.symlink(osp.normpath(next_config), last_running_config)
164 def shutdownMyPort(port, cleanup=True):
166 Shutdown SALOME session running on the specified port.
167 :param port : port number
168 :param cleanup : perform additional cleanup actions (kill omniNames, etc.)
173 # ensure port is an integer
174 with suppress(ValueError):
178 with suppress(ImportError):
179 # DO NOT REMOVE NEXT LINE: it tests PortManager availability!
180 from PortManager import releasePort
183 # set OMNIORB_CONFIG variable to the proper file (if not set yet)
184 omniorb_user_path = os.getenv('OMNIORB_USER_PATH')
186 if omniorb_user_path is not None:
187 kwargs['with_username'] = True
189 omniorb_user_path = osp.realpath(osp.expanduser('~'))
190 omniorb_config = generateFileName(omniorb_user_path,
197 os.environ['OMNIORB_CONFIG'] = omniorb_config
198 os.environ['NSPORT'] = str(port)
200 # give the chance to the servers to shutdown properly
201 with suppress(Exception):
202 from omniORB import CORBA
203 from LifeCycleCORBA import LifeCycleCORBA
204 orb = CORBA.ORB_init([''], CORBA.ORB_ID)
205 lcc = LifeCycleCORBA(orb) # see (1) below
208 print("Terminating SALOME session on port {}...".format(port))
209 lcc.shutdownServers()
210 # give some time to shutdown to complete
215 __killMyPort(port, getPiDict(port))
216 # DO NOT REMOVE NEXT LINE: it tests PortManager availability!
217 from PortManager import releasePort
220 sys.exit(0) # see (1) below
222 # (1) If --shutdown-servers option is set to 1, session close procedure is
223 # called twice: first explicitly by salome command, second by automatic
224 # atexit to handle Ctrl-C. During second call, LCC does not exist anymore and
225 # a RuntimeError is raised; we explicitly exit this function with code 0 to
226 # prevent parent thread from crashing.
228 def __killProcesses(processes):
230 Terminate and kill all given processes (inernal).
231 :param processes : list of processes, each one is an instance of psutil.Process
233 # terminate processes
234 for process in processes:
236 # wait a little, then check for alive
237 _, alive = psutil.wait_procs(processes, timeout=5)
239 for process in alive:
242 def __killPids(pids):
244 Kill processes with given `pids`.
245 :param pids : processes IDs
250 processes.append(psutil.Process(pid))
251 except psutil.NoSuchProcess:
253 print(" ------------------ Process {} not found".format(pid))
254 __killProcesses(processes)
256 def __killMyPort(port, filedict):
258 Kill processes for given port (internal).
259 :param port : port number
260 :param filedict : pidict file
262 # ensure port is an integer
263 with suppress(ValueError):
266 # read pids from pidict file
267 with suppress(Exception), open(filedict, 'rb') as fpid:
268 pids_lists = pickle.load(fpid)
269 # note: pids_list is a list of tuples!
270 for pids in pids_lists:
273 # finally remove pidict file
276 def __guessPiDictFilename(port):
278 Guess and return pidict file for given `port` (internal).
279 :param port : port number
280 :return pidict file's path
282 # Check all possible versions of pidict file
283 # new-style - dot-prefixed pidict file: hidden is True, auto hostname
284 # old-style - not dot-prefixed pidict file: hidden is False, auto hostname
285 # old-style - dot-prefixed pidict file: hidden is True, short hostname
286 # old-style - not dot-prefixed pidict file: hidden is False, short hostname
287 # old-style - dot-prefixed pidict file: hidden is True, long hostname
288 # old-style - not dot-prefixed pidict file: hidden is False, long hostname
289 for hostname, hidden in itertools.product((None, getShortHostName(), getHostName()),
291 filedict = getPiDict(port, hidden=hidden, hostname=hostname)
292 if not osp.exists(filedict):
294 print('Trying {}... not found'.format(filedict))
297 print('Trying {}... OK'.format(filedict))
302 def killMyPort(*ports):
304 Kill SALOME session running on the specified port.
305 :param ports : port numbers
308 # ensure port is an integer
309 with suppress(ValueError):
312 with suppress(Exception):
313 # DO NOT REMOVE NEXT LINE: it tests PortManager availability!
314 from PortManager import releasePort
316 filedict = getPiDict(port)
317 if not osp.isfile(filedict): # removed by previous call, see (1) above
319 print("SALOME session on port {} is already stopped".format(port))
320 # remove port from PortManager config file
321 with suppress(ImportError):
323 print("Removing port from PortManager configuration file")
327 # try to shutdown session normally
328 Thread(target=shutdownMyPort, args=(port, True)).start()
331 # ... then kill processes (should be done if shutdown procedure hangs up)
333 # DO NOT REMOVE NEXT LINE: it tests PortManager availability!
334 import PortManager # pragma pylint: disable=unused-import
335 for file_path in glob('{}*'.format(getPiDict(port))):
336 __killMyPort(port, file_path)
338 __killMyPort(port, __guessPiDictFilename(port))
340 # clear-up omniOrb config files
341 appliCleanOmniOrbConfig(port)
343 def cleanApplication(port):
345 Clean application running on the specified port.
346 :param port : port number
348 # ensure port is an integer
349 with suppress(ValueError):
353 with suppress(Exception):
354 filedict = getPiDict(port)
357 # clear-up omniOrb config files
358 appliCleanOmniOrbConfig(port)
360 def killMyPortSpy(pid, port):
362 Start daemon process which watches for given process and kills session when process exits.
363 :param pid : process ID
364 :param port : port number (to kill)
367 ret = killPid(int(pid), 0) # 0 is used to check process existence without actual killing it
369 break # process does not exist: exit loop
371 return # something got wrong
374 filedict = getPiDict(port)
375 if not osp.exists(filedict):
378 # check Session server
381 orb = omniORB.CORBA.ORB_init(sys.argv, omniORB.CORBA.ORB_ID)
382 import SALOME_NamingServicePy
383 naming_service = SALOME_NamingServicePy.SALOME_NamingServicePy_i(orb, 3, True)
384 import SALOME #@UnresolvedImport @UnusedImport # pragma pylint: disable=unused-import
385 session = naming_service.Resolve('/Kernel/Session')
387 except: # pragma pylint: disable=bare-except
391 status = session.GetStatSession()
392 except: # pragma pylint: disable=bare-except
393 # -- session is in naming service but likely crashed
395 if status is not None and not status.activeGUI:
399 def __checkUnkilledProcesses():
401 Check all unkilled SALOME processes (internal).
402 :return: list of unkilled processes
404 def _checkUserName(_process):
405 # The following is a workaround for Windows on which
406 # psutil.Process().username() returns 'usergroup' + 'username'
407 return getUserName() == _process.username()
409 def _getDictfromOutput(_processes, _wildcard=None):
410 for _process in psutil.process_iter(['name', 'username']):
411 with suppress(psutil.AccessDenied):
412 if _checkUserName(_process) and re.match(_wildcard, _process.info['name']):
413 _processes.append(_process)
416 _getDictfromOutput(processes, '(SALOME_*)')
417 _getDictfromOutput(processes, '(omniNames)')
418 _getDictfromOutput(processes, '(ghs3d)')
419 _getDictfromOutput(processes, '(ompi-server)')
423 def killUnkilledProcesses():
425 Kill processes which could remain even after shutdowning SALOME sessions.
427 __killProcesses(__checkUnkilledProcesses())
433 from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
434 formatter = lambda prog: ArgumentDefaultsHelpFormatter(prog, max_help_position=50, width=120)
435 parser = ArgumentParser(description='Forcibly stop given SALOME session(s)',
436 formatter_class=formatter)
437 parser.add_argument('ports',
438 help='ports to kill',
440 group = parser.add_mutually_exclusive_group()
441 group.add_argument('-s', '--spy',
442 help='start daemon to watch PID and then kill given PORT',
443 nargs=2, type=int, metavar=('PID', 'PORT'))
444 group.add_argument('-l', '--list',
445 help='list unkilled SALOME processes',
447 args = parser.parse_args()
449 if args.ports and (args.spy or args.list):
450 print("{}: error: argument ports cannot be used with -s/--spy or -l/--list"
451 .format(parser.prog), file=sys.stderr)
455 killMyPortSpy(*args.spy)
459 processes = __checkUnkilledProcesses()
461 print("Unkilled processes: ")
462 print("---------------------")
463 print(" PID : Process name")
464 print("--------------------")
465 for process in processes:
466 print("{0:6} : {1}".format(process.pid, process.name()))
468 print("No SALOME related processed found")
472 from salomeContextUtils import setOmniOrbUserPath #@UnresolvedImport
474 except Exception as exc: # pragma pylint: disable=broad-except
479 killMyPort(*args.ports)
481 if __name__ == '__main__':