Salome HOME
Merge branch 'rbe/evol-job-newparams'
[modules/kernel.git] / bin / killSalomeWithPort.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2014  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 os, sys, pickle, signal, commands,glob
35 from salome_utils import verbose
36 import salome_utils
37
38 def getPiDict(port,appname='salome',full=True,hidden=True,hostname=None):
39     """
40     Get file with list of SALOME processes.
41     This file is located in the user's home directory
42     and named .<user>_<host>_<port>_SALOME_pidict
43     where
44     <user> is user name
45     <host> is host name
46     <port> is port number
47
48     Parameters:
49     - port    : port number
50     - appname : application name (default is 'SALOME')
51     - full    : if True, full path to the file is returned, otherwise only file name is returned
52     - hidden  : if True, file name is prefixed with . (dot) symbol; this internal parameter is used
53     to support compatibility with older versions of SALOME
54     """
55     # bug fix: ensure port is an integer
56     # Note: this function is also called with port='#####' !!!
57     try:
58         port = int(port)
59     except:
60         pass
61
62     from salome_utils import generateFileName, getTmpDir
63     dir = ""
64     if not hostname:
65         hostname = os.getenv("NSHOST")
66         if hostname: hostname = hostname.split(".")[0]
67         pass
68     if full:
69         # full path to the pidict file is requested
70         if hidden:
71             # new-style dot-prefixed pidict files
72             # are in the system-dependant temporary diretory
73             dir = getTmpDir()
74         else:
75             # old-style non-dot-prefixed pidict files
76             # are in the user's home directory
77             dir = os.getenv("HOME")
78             pass
79         pass
80
81     return generateFileName(dir,
82                             suffix="pidict",
83                             hidden=hidden,
84                             with_username=True,
85                             with_hostname=hostname or True,
86                             with_port=port,
87                             with_app=appname.upper())
88
89 def appliCleanOmniOrbConfig(port):
90     """
91     Remove omniorb config files related to the port in SALOME application:
92     - ${OMNIORB_USER_PATH}/.omniORB_${USER}_${HOSTNAME}_${NSPORT}.cfg
93     - ${OMNIORB_USER_PATH}/.omniORB_${USER}_last.cfg
94     the last is removed only if the link points to the first file.
95     """
96     if verbose():
97         print "clean OmniOrb config for port %s"%port
98
99     from salome_utils import generateFileName, getUserName
100     omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
101     if omniorbUserPath is None:
102         #Run outside application context
103         pass
104     else:
105         omniorb_config      = generateFileName(omniorbUserPath, prefix="omniORB",
106                                                extension="cfg",
107                                                hidden=True,
108                                                with_username=True,
109                                                with_hostname=True,
110                                                with_port=port)
111         last_running_config = generateFileName(omniorbUserPath, prefix="omniORB",
112                                                with_username=True,
113                                                suffix="last",
114                                                extension="cfg",
115                                                hidden=True)
116         if os.access(last_running_config,os.F_OK):
117             if not sys.platform == 'win32':
118                 pointedPath = os.readlink(last_running_config)
119                 if pointedPath[0] != '/':
120                     pointedPath=os.path.join(os.path.dirname(last_running_config), pointedPath)
121                     pass
122                 if pointedPath == omniorb_config:
123                     os.unlink(last_running_config)
124                     pass
125                 pass
126             else:
127                 os.remove(last_running_config)
128                 pass
129             pass
130
131         if os.access(omniorb_config,os.F_OK):
132             os.remove(omniorb_config)
133             pass
134
135         if os.path.lexists(last_running_config):return
136
137         #try to relink last.cfg to an existing config file if any
138         files = glob.glob(os.path.join(omniorbUserPath,".omniORB_"+getUserName()+"_*.cfg"))
139         current_config=None
140         current=0
141         for f in files:
142           stat=os.stat(f)
143           if stat.st_atime > current:
144             current=stat.st_atime
145             current_config=f
146         if current_config:
147           if sys.platform == "win32":
148             import shutil
149             shutil.copyfile(os.path.normpath(current_config), last_running_config)
150             pass
151           else:
152             os.symlink(os.path.normpath(current_config), last_running_config)
153             pass
154           pass
155         pass
156     pass
157
158 ########## kills all salome processes with the given port ##########
159
160 def shutdownMyPort(port, cleanup=True):
161     """
162     Shutdown SALOME session running on the specified port.
163     Parameters:
164     - port - port number
165     """
166     if not port: return
167     # bug fix: ensure port is an integer
168     port = int(port)
169
170     try:
171         from PortManager import releasePort
172         releasePort(port)
173     except ImportError:
174         pass
175
176     from salome_utils import generateFileName
177
178     # set OMNIORB_CONFIG variable to the proper file
179     omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
180     kwargs = {}
181     if omniorbUserPath is not None:
182         kwargs["with_username"]=True
183     else:
184         omniorbUserPath = os.path.realpath(os.path.expanduser('~'))
185     omniorb_config = generateFileName(omniorbUserPath, prefix="omniORB",
186                                       extension="cfg",
187                                       hidden=True,
188                                       with_hostname=True,
189                                       with_port=port,
190                                       **kwargs)
191     os.environ['OMNIORB_CONFIG'] = omniorb_config
192     os.environ['NSPORT'] = str(port)
193
194     # give the chance to the servers to shutdown properly
195     try:
196         import time
197         from omniORB import CORBA
198
199         from LifeCycleCORBA import LifeCycleCORBA
200         # shutdown all
201         orb = CORBA.ORB_init([''], CORBA.ORB_ID)
202         lcc = LifeCycleCORBA(orb)
203         lcc.shutdownServers()
204         # give some time to shutdown to complete
205         time.sleep(1)
206         # shutdown omniNames
207         if cleanup:
208             lcc.killOmniNames()
209             time.sleep(1)
210             pass
211         pass
212     except:
213         pass
214     pass
215
216 def __killMyPort(port, filedict):
217     # bug fix: ensure port is an integer
218     if port:
219         port = int(port)
220
221     try:
222         with open(filedict, 'r') as fpid:
223             #
224             from salome_utils import generateFileName
225             if sys.platform == "win32":
226                 username = os.getenv( "USERNAME" )
227             else:
228                 username = os.getenv('USER')
229             path = os.path.join('/tmp/logs', username)
230             fpidomniNames = generateFileName(path,
231                                              prefix="",
232                                              suffix="Pid_omniNames",
233                                              extension="log",
234                                              with_port=port)
235             if not sys.platform == 'win32':
236                 cmd = 'pid=`ps -eo pid,command | egrep "[0-9] omniNames -start %s"` ; echo $pid > %s' % ( str(port), fpidomniNames )
237                 a = os.system(cmd)
238                 pass
239             try:
240                 with open(fpidomniNames) as fpidomniNamesFile:
241                     lines = fpidomniNamesFile.readlines()
242
243                 os.remove(fpidomniNames)
244                 for l in lines:
245                     try:
246                         pidfield = l.split()[0] # pid should be at the first position
247                         if sys.platform == "win32":
248                             import win32pm
249                             if verbose(): print 'stop process '+pidfield+' : omniNames'
250                             win32pm.killpid(int(pidfield),0)
251                         else:
252                             if verbose(): print 'stop process '+pidfield+' : omniNames'
253                             os.kill(int(pidfield),signal.SIGKILL)
254                             pass
255                         pass
256                     except:
257                         pass
258                     pass
259                 pass
260             except:
261                 pass
262             #
263             try:
264                 process_ids=pickle.load(fpid)
265                 for process_id in process_ids:
266                     for pid, cmd in process_id.items():
267                         if verbose(): print "stop process %s : %s"% (pid, cmd[0])
268                         if cmd[0] == "omniNames":
269                             if not sys.platform == 'win32':
270                                 import subprocess
271                                 import shlex
272                                 proc1 = subprocess.Popen(shlex.split('ps -eo pid,command'),stdout=subprocess.PIPE)
273                                 proc2 = subprocess.Popen(shlex.split('egrep "[0-9] omniNames -start"'),stdin=proc1.stdout, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
274                                 proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
275                                 out,err=proc2.communicate()
276                                 # out looks like: PID omniNames -start PORT <other args>
277
278                                 # extract omninames pid and port number
279                                 try:
280                                     import re
281                                     omniNamesPid, omniNamesPort = re.search('(.+?) omniNames -start (.+?) ', out).group(1, 2)
282                                     if omniNamesPort == port:
283                                         if verbose():
284                                             print "stop omniNames [pid=%s] on port %s"%(omniNamesPid, omniNamesPort)
285                                         appliCleanOmniOrbConfig(omniNamesPort)
286                                         from PortManager import releasePort
287                                         releasePort(omniNamesPort)
288                                         os.kill(int(omniNamesPid),signal.SIGKILL)
289                                 except (ImportError, AttributeError, OSError) as e:
290                                     pass
291                                 except:
292                                     import traceback
293                                     traceback.print_exc()
294
295                         try:
296                             if sys.platform == "win32":
297                                 import win32pm
298                                 win32pm.killpid(int(pid),0)
299                             else:
300                                 os.kill(int(pid),signal.SIGKILL)
301                                 pass
302                             pass
303                         except:
304                             if verbose(): print "  ------------------ process %s : %s not found"% (pid, cmd[0])
305                             pass
306                         pass # for pid, cmd ...
307                     pass # for process_id ...
308                 pass # try...
309             except:
310                 pass
311         # end with
312         #
313         os.remove(filedict)
314         cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
315         pid = commands.getoutput(cmd)
316         a = ""
317         while pid and len(a.split()) < 2:
318             a = commands.getoutput("kill -9 " + pid)
319             pid = commands.getoutput(cmd)
320             pass
321         pass
322     except:
323         print "Cannot find or open SALOME PIDs file for port", port
324         pass
325     #
326 #
327
328 def __guessPiDictFilename(port):
329     from salome_utils import getShortHostName, getHostName
330     filedicts = [
331         # new-style dot-prefixed pidict file
332         getPiDict(port, hidden=True),
333         # provide compatibility with old-style pidict file (not dot-prefixed)
334         getPiDict(port, hidden=False),
335         # provide compatibility with old-style pidict file (short hostname)
336         getPiDict(port, hidden=True, hostname=getShortHostName()),
337         # provide compatibility with old-style pidict file (not dot-prefixed, short hostname
338         getPiDict(port, hidden=False, hostname=getShortHostName()),
339         # provide compatibility with old-style pidict file (long hostname)
340         getPiDict(port, hidden=True, hostname=getHostName()),
341         # provide compatibility with old-style pidict file (not dot-prefixed, long hostname)
342         getPiDict(port, hidden=False, hostname=getHostName())
343         ]
344
345     log_msg = ""
346     for filedict in filedicts:
347         log_msg += "Trying %s..."%filedict
348         if os.path.exists(filedict):
349             log_msg += "   ... OK\n"
350             break
351         else:
352             log_msg += "   ... not found\n"
353
354     if verbose():
355         print log_msg
356
357     return filedict
358 #
359
360 def killMyPort(port):
361     """
362     Kill SALOME session running on the specified port.
363     Parameters:
364     - port - port number
365     """
366     print "Terminating SALOME on port %s..."%(port)
367
368     # bug fix: ensure port is an integer
369     if port:
370         port = int(port)
371
372     # try to shutdown session normally
373     import threading, time
374     threading.Thread(target=shutdownMyPort, args=(port,False)).start()
375     time.sleep(3) # wait a little, then kill processes (should be done if shutdown procedure hangs up)
376
377     try:
378         import PortManager
379         filedict = getPiDict(port)
380         #filedict = __guessPiDictFilename(port)
381         import glob
382         all_files = glob.glob("%s*"%filedict)
383         for f in all_files:
384             __killMyPort(port, f)
385     except ImportError:
386         filedict = __guessPiDictFilename(port)
387         __killMyPort(port, filedict)
388     #
389
390     appliCleanOmniOrbConfig(port)
391     pass
392
393 def cleanApplication(port):
394     """
395     Clean application running on the specified port.
396     Parameters:
397     - port - port number
398     """
399     # bug fix: ensure port is an integer
400     if port:
401         port = int(port)
402
403     try:
404         filedict=getPiDict(port)
405         os.remove(filedict)
406     except:
407       #import traceback
408       #traceback.print_exc()
409       pass
410
411     appliCleanOmniOrbConfig(port)
412
413 def killMyPortSpy(pid, port):
414     dt = 1.0
415     while 1:
416         if sys.platform == "win32":
417             from win32pm import killpid
418             if killpid(int(pid), 0) != 0:
419                 return
420         else:
421             from os import kill
422             try:
423                 kill(int(pid), 0)
424             except OSError, e:
425                 if e.errno != 3:
426                     return
427                 break
428             pass
429         from time import sleep
430         sleep(dt)
431         pass
432     filedict = getPiDict(port, hidden=True)
433     if not os.path.exists(filedict):
434         return
435     try:
436         import omniORB
437         orb = omniORB.CORBA.ORB_init(sys.argv, omniORB.CORBA.ORB_ID)
438         import SALOME_NamingServicePy
439         ns = SALOME_NamingServicePy.SALOME_NamingServicePy_i(orb)
440         import SALOME
441         session = ns.Resolve("/Kernel/Session")
442         assert session
443     except:
444         return
445     try:
446         status = session.GetStatSession()
447     except:
448         # -- session is in naming service but has crash
449         status = None
450         pass
451     if status:
452         if not status.activeGUI:
453             return
454         pass
455     killMyPort(port)
456     return
457
458 if __name__ == "__main__":
459     if len(sys.argv) < 2:
460         print "Usage: "
461         print "  %s <port>" % os.path.basename(sys.argv[0])
462         print
463         print "Kills SALOME session running on specified <port>."
464         sys.exit(1)
465         pass
466     if sys.argv[1] == "--spy":
467         if len(sys.argv) > 3:
468             pid = sys.argv[2]
469             port = sys.argv[3]
470             killMyPortSpy(pid, port)
471             pass
472         sys.exit(0)
473         pass
474     try:
475         from salomeContextUtils import setOmniOrbUserPath
476         setOmniOrbUserPath()
477     except Exception, e:
478         print e
479         sys.exit(1)
480     for port in sys.argv[1:]:
481         killMyPort(port)
482         pass
483     pass