]> SALOME platform Git repositories - modules/kernel.git/blob - bin/killSalomeWithPort.py
Salome HOME
bos #16706 Improve procedure that kills booked TCP port
[modules/kernel.git] / bin / killSalomeWithPort.py
1 #! /usr/bin/env python3
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
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.
12 #
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.
17 #
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
21 #
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24
25 ## \file killSalomeWithPort.py
26 #  Stop all %SALOME servers from given sessions by killing them
27 #
28 #  The sessions are indicated by their ports on the command line as in :
29 #  \code
30 #  killSalomeWithPort.py 2811 2815
31 #  \endcode
32 #
33
34 import re, os, sys, pickle, signal, glob
35 import subprocess
36 import shlex
37 from salome_utils import verbose
38
39
40 def getPiDict(port,appname='salome',full=True,hidden=True,hostname=None):
41     """
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
45     where
46     <user> is user name
47     <host> is host name
48     <port> is port number
49
50     Parameters:
51     - port    : port number
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
56     """
57     # bug fix: ensure port is an integer
58     # Note: this function is also called with port='#####' !!!
59     try:
60         port = int(port)
61     except:
62         pass
63
64     from salome_utils import generateFileName, getLogDir
65     dir = ""
66     if not hostname:
67         hostname = os.getenv("NSHOST")
68         if hostname: hostname = hostname.split(".")[0]
69         pass
70     if full:
71         # full path to the pidict file is requested
72         if hidden:
73             # new-style dot-prefixed pidict files
74             # are in the system-dependant temporary directory
75             dir = getLogDir()
76         else:
77             # old-style non-dot-prefixed pidict files
78             # are in the user's home directory
79             dir = os.path.expanduser("~")
80             pass
81         pass
82
83     return generateFileName(dir,
84                             suffix="pidict",
85                             hidden=hidden,
86                             with_username=True,
87                             with_hostname=hostname or True,
88                             with_port=port,
89                             with_app=appname.upper())
90
91 def appliCleanOmniOrbConfig(port):
92     """
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.
97     """
98     if verbose():
99         print("clean OmniOrb config for port %s"%port)
100
101     from salome_utils import generateFileName, getUserName
102     omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
103     if omniorbUserPath is None:
104         #Run outside application context
105         pass
106     else:
107         omniorb_config      = generateFileName(omniorbUserPath, prefix="omniORB",
108                                                extension="cfg",
109                                                hidden=True,
110                                                with_username=True,
111                                                with_hostname=True,
112                                                with_port=port)
113         last_running_config = generateFileName(omniorbUserPath, prefix="omniORB",
114                                                with_username=True,
115                                                suffix="last",
116                                                extension="cfg",
117                                                hidden=True)
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)
123                     pass
124                 if pointedPath == omniorb_config:
125                     os.unlink(last_running_config)
126                     pass
127                 pass
128             else:
129                 os.remove(last_running_config)
130                 pass
131             pass
132
133         if os.access(omniorb_config,os.F_OK):
134             os.remove(omniorb_config)
135             pass
136
137         if os.path.lexists(last_running_config):return
138
139         #try to relink last.cfg to an existing config file if any
140         files = glob.glob(os.path.join(omniorbUserPath,".omniORB_"+getUserName()+"_*.cfg"))
141         current_config=None
142         current=0
143         for f in files:
144           stat=os.stat(f)
145           if stat.st_atime > current:
146             current=stat.st_atime
147             current_config=f
148         if current_config:
149           if sys.platform == "win32":
150             import shutil
151             shutil.copyfile(os.path.normpath(current_config), last_running_config)
152             pass
153           else:
154             os.symlink(os.path.normpath(current_config), last_running_config)
155             pass
156           pass
157         pass
158     pass
159
160 ########## kills all salome processes with the given port ##########
161
162 def shutdownMyPort(port, cleanup=True):
163     """
164     Shutdown SALOME session running on the specified port.
165     Parameters:
166     - port - port number
167     """
168     if not port: return
169     # bug fix: ensure port is an integer
170     port = int(port)
171
172     try:
173         from PortManager import releasePort
174         releasePort(port)
175     except ImportError:
176         pass
177
178     from salome_utils import generateFileName
179
180     # set OMNIORB_CONFIG variable to the proper file
181     omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
182     kwargs = {}
183     if omniorbUserPath is not None:
184         kwargs["with_username"]=True
185     else:
186         omniorbUserPath = os.path.realpath(os.path.expanduser('~'))
187     omniorb_config = generateFileName(omniorbUserPath, prefix="omniORB",
188                                       extension="cfg",
189                                       hidden=True,
190                                       with_hostname=True,
191                                       with_port=port,
192                                       **kwargs)
193     os.environ['OMNIORB_CONFIG'] = omniorb_config
194     os.environ['NSPORT'] = str(port)
195
196     # give the chance to the servers to shutdown properly
197     try:
198         import time
199         from omniORB import CORBA
200
201         from LifeCycleCORBA import LifeCycleCORBA
202         # shutdown all
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
208         time.sleep(1)
209         # shutdown omniNames
210         if cleanup:
211           from salome_utils import killOmniNames
212           killOmniNames(port)
213           filedict=getPiDict(port)
214           __killMyPort(port, filedict)
215           from PortManager import releasePort
216           releasePort(port)
217           time.sleep(1)
218           pass
219         pass
220     except:
221         pass
222     sys.exit(0) # see (1)
223     pass
224 # (1) If --shutdown-servers option is set to 1, session close procedure is
225 # called twice: first explicitly by salome command, second by automatic
226 # atexit to handle Ctrl-C. During second call, LCC does not exist anymore and
227 # a RuntimeError is raised; we explicitly exit this function with code 0 to
228 # prevent parent thread from crashing.
229
230 def __killMyPort(port, filedict):
231     # bug fix: ensure port is an integer
232     if port:
233         port = int(port)
234
235     try:
236         with open(filedict, 'rb') as fpid:
237             process_ids=pickle.load(fpid)
238             for process_id in process_ids:
239                 for pid, cmd in list(process_id.items()):
240                     pid = int(pid)
241                     if verbose(): print("stop process %s : %s"% (pid, cmd[0]))
242                     try:
243                         from salome_utils import killpid
244                         killpid(pid)
245                     except:
246                         if verbose(): print("  ------------------ process %s : %s not found"% (pid, cmd[0]))
247                         if pid in checkUnkilledProcess():
248                             try:
249                                 killpid(pid)
250                             except:
251                                 pass
252                         pass
253                     pass # for pid ...
254                 pass # for process_id ...
255             # end with
256     except:
257         print("Cannot find or open SALOME PIDs file for port", port)
258         pass
259     os.remove(filedict)
260     pass
261 #
262
263 def __guessPiDictFilename(port):
264     from salome_utils import getShortHostName, getHostName
265     filedicts = [
266         # new-style dot-prefixed pidict file
267         getPiDict(port, hidden=True),
268         # provide compatibility with old-style pidict file (not dot-prefixed)
269         getPiDict(port, hidden=False),
270         # provide compatibility with old-style pidict file (short hostname)
271         getPiDict(port, hidden=True, hostname=getShortHostName()),
272         # provide compatibility with old-style pidict file (not dot-prefixed, short hostname
273         getPiDict(port, hidden=False, hostname=getShortHostName()),
274         # provide compatibility with old-style pidict file (long hostname)
275         getPiDict(port, hidden=True, hostname=getHostName()),
276         # provide compatibility with old-style pidict file (not dot-prefixed, long hostname)
277         getPiDict(port, hidden=False, hostname=getHostName())
278         ]
279
280     log_msg = ""
281     for filedict in filedicts:
282         log_msg += "Trying %s..."%filedict
283         if os.path.exists(filedict):
284             log_msg += "   ... OK\n"
285             break
286         else:
287             log_msg += "   ... not found\n"
288
289     if verbose():
290         print(log_msg)
291
292     return filedict
293 #
294
295 def killMyPort(port):
296     """
297     Kill SALOME session running on the specified port.
298     Parameters:
299     - port - port number
300     """
301     # bug fix: ensure port is an integer
302     if port:
303         port = int(port)
304
305     try:
306         import PortManager # do not remove! Test for PortManager availability!
307         filedict = getPiDict(port)
308         if not os.path.isfile(filedict): # removed by previous call, see (1)
309             if verbose():
310                 print("SALOME on port %s: already removed by previous call"%port)
311             # Remove port from PortManager config file
312             try:
313                 from PortManager import releasePort
314                 if verbose():
315                     print("Removing port from PortManager configuration file")
316                 releasePort(port)
317             except ImportError:
318                 pass
319             return
320     except:
321         pass
322
323     # try to shutdown session normally
324     import threading, time
325     threading.Thread(target=shutdownMyPort, args=(port,True)).start()
326     time.sleep(3) # wait a little, then kill processes (should be done if shutdown procedure hangs up)
327
328     try:
329         import PortManager # do not remove! Test for PortManager availability!
330         filedict = getPiDict(port)
331         #filedict = __guessPiDictFilename(port)
332         import glob
333         all_files = glob.glob("%s*"%filedict)
334         for f in all_files:
335             __killMyPort(port, f)
336     except ImportError:
337         filedict = __guessPiDictFilename(port)
338         __killMyPort(port, filedict)
339     #
340
341     appliCleanOmniOrbConfig(port)
342     pass
343
344 def cleanApplication(port):
345     """
346     Clean application running on the specified port.
347     Parameters:
348     - port - port number
349     """
350     # bug fix: ensure port is an integer
351     if port:
352         port = int(port)
353
354     try:
355         filedict=getPiDict(port)
356         os.remove(filedict)
357     except:
358       #import traceback
359       #traceback.print_exc()
360       pass
361
362     appliCleanOmniOrbConfig(port)
363     pass
364
365 def killMyPortSpy(pid, port):
366     dt = 1.0
367     while 1:
368         from salome_utils import killpid
369         ret = killpid(pid, 0)
370         if ret == 0:
371             break
372         elif ret < 0:
373             return
374         from time import sleep
375         sleep(dt)
376         pass
377     filedict = getPiDict(port, hidden=True)
378     if not os.path.exists(filedict):
379         return
380     try:
381         import omniORB
382         orb = omniORB.CORBA.ORB_init(sys.argv, omniORB.CORBA.ORB_ID)
383         import SALOME_NamingServicePy
384         ns = SALOME_NamingServicePy.SALOME_NamingServicePy_i(orb)
385         import SALOME #@UnresolvedImport @UnusedImport
386         session = ns.Resolve("/Kernel/Session")
387         assert session
388     except:
389         return
390     try:
391         status = session.GetStatSession()
392     except:
393         # -- session is in naming service but has crash
394         status = None
395         pass
396     if status:
397         if not status.activeGUI:
398             return
399         pass
400     killMyPort(port)
401     return
402
403 def checkUnkilledProcess():
404     #check processes in system after kill
405     from salome_utils import getUserName
406     user = getUserName()
407     processes = dict()
408
409     if sys.platform != 'win32':
410
411         def _getDictfromOutput(_output, _dic, _cond = None):
412             _pids = dict(zip(list(map(int, _output[::2])), _output[1::2]))
413             if _cond:
414                 _pids = {pid:cmd for pid,cmd in _pids.items() if re.match(_cond, cmd)}
415             _dic.update(_pids)
416
417         # 1. SALOME servers plus omniNames
418         cmd = 'ps --noheading -U {user} -o pid,cmd | awk \'{{printf("%s %s\\n", $1, $2)}}\''.format(user=user)
419         _getDictfromOutput(subprocess.getoutput(cmd).split(), processes, '^(SALOME_|omniNames)')
420         # 2. ghs3d
421         cmd = 'ps -fea | grep \'{user}\' | grep \'ghs3d\' | grep \'f /tmp/GHS3D_\' | grep -v \'grep\' | awk \'{{print("%s %s\\n", $2, $8)}}\''.format(user=user)
422         _getDictfromOutput(subprocess.getoutput(cmd).split(), processes)
423         # 3. ompi-server
424         cmd = 'ps -fea | grep \'{user}\' | grep \'ompi-server\' | grep -v \'grep\' | awk \'{{print("%s %s\\n", $2, $8)}}\''.format(user=user)
425         _getDictfromOutput(subprocess.getoutput(cmd).split(), processes)
426     else:
427         cmd = 'tasklist /fo csv | findstr /i "SALOME_ omniNames"'
428         prc = subprocess.getoutput(cmd)
429         try:
430             prc = prc.split()
431             prc = [prc[i].split(',') for i in range(0, len(prc)) if i % 2 == 0]
432             prc = dict([(int(prc[j][1].replace('"', '')), prc[j][0].replace('"', '')) for j in range(0, len(prc))])
433             processes.update(prc)
434         except:
435             pass
436     return processes
437
438 if __name__ == "__main__":
439     if len(sys.argv) < 2:
440         print("Usage: ")
441         print("  %s <port>" % os.path.basename(sys.argv[0]))
442         print()
443         print("Kills SALOME session running on specified <port>.")
444         sys.exit(1)
445         pass
446     if sys.argv[1] == "--spy":
447         if len(sys.argv) > 3:
448             pid = sys.argv[2]
449             port = sys.argv[3]
450             killMyPortSpy(pid, port)
451             pass
452         sys.exit(0)
453         pass
454     elif sys.argv[1] == "--find":
455         processes = checkUnkilledProcess()
456         if processes:
457             print("Unkilled processes: ")
458             print(" --------------------")
459             print(" PID   : Process name")
460             print(" --------------------")
461             for pair in processes.items():
462                 print('%6d : %s' % pair)
463         sys.exit(0)
464     try:
465         from salomeContextUtils import setOmniOrbUserPath #@UnresolvedImport
466         setOmniOrbUserPath()
467     except Exception as e:
468         print(e)
469         sys.exit(1)
470     for port in sys.argv[1:]:
471         killMyPort(port)
472         pass
473     pass